wan_ip_retrieval_and_dynamic_dns_mdns
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revision | |||
wan_ip_retrieval_and_dynamic_dns_mdns [2021/03/22 06:54] – hc9 | wan_ip_retrieval_and_dynamic_dns_mdns [2021/03/22 06:58] (current) – hc9 | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== WAN IP Retrieval and Dynamic DNS (mdns) ====== | ||
+ | |||
+ | There are myriad ways to automatically keep your **mdns** updated on SDF. Below are a couple: | ||
+ | |||
+ | The first script depends on passwordless SSH logons. Also, make sure the file permissions for your scripts are executable. For more info, see the SDF tutorial on [[permissions|File Permissions]]. | ||
+ | |||
+ | === Script 1 === | ||
+ | |||
+ | Using the variables $SSH_CLIENT and $SSH_CONNECTION, | ||
+ | |||
+ | < | ||
+ | echo $SSH_CLIENT | ||
+ | </ | ||
+ | |||
+ | < | ||
+ | echo $SSH_CONNECTION | ||
+ | </ | ||
+ | | ||
+ | We will take advantage of this simply by issuing a remote SSH command from our home computer. | ||
+ | |||
+ | First, somewhere in your SDF directory (ex: ~/bin), create a file called **ip** and put the following lines in this file: | ||
+ | |||
+ | < | ||
+ | #!/bin/sh | ||
+ | IP=$(echo $SSH_CLIENT | awk ' | ||
+ | OLDIP=$(cat ~/bin/.ip) | ||
+ | if [ " | ||
+ | then | ||
+ | echo " | ||
+ | mdns " | ||
+ | fi | ||
+ | </ | ||
+ | |||
+ | This will check your connecting IP against the local file ** .ip,** if the IP is the same it does nothing. If it has changed (or if the file .ip does not exist) it updates the mdns record and the **.ip** file. | ||
+ | |||
+ | Next, execute that remote script with a SSH command from your home computer using the following code (Example filename: **ipset**): | ||
+ | |||
+ | < | ||
+ | #!/bin/ksh | ||
+ | ssh me@remote.host ' | ||
+ | </ | ||
+ | |||
+ | Standard output is directed to /dev/null because we will add this script to cron and it's not necessary to have the output mailed to you if cron is set up that way. If desired, redirect standard output to a logfile like so: | ||
+ | |||
+ | < | ||
+ | #!/bin/ksh | ||
+ | ssh me@remote.host ' | ||
+ | </ | ||
+ | |||
+ | Add the script to your home machine' | ||
+ | |||
+ | < | ||
+ | * */24 * * * $HOME/ | ||
+ | </ | ||
+ | |||
+ | === Note === | ||
+ | |||
+ | The script could again be modified to echo back your IP address. Name it **ipecho** or something like that. Here is the script: | ||
+ | |||
+ | < | ||
+ | IP=$(ssh me@remote.host 'echo $SSH_CLIENT' | ||
+ | echo $IP | ||
+ | </ | ||
+ | |||
+ | === Script 2 === | ||
+ | |||
+ | === CGI script, usable with VHOST membership === | ||
+ | |||
+ | If you have a VHOST membership (or MetaARPA), you can use the following CGI script to set the IP via an HTTP request. The advantage is that you do not need to set up ssh keys for this; the disadvantage is that somebody sniffing on your traffic could update your mdns to some other address - so you should not use this script for security critical applications where you rely on authorized mdns setting (but feel free to improve the script as an exercise to include some authentication). | ||
+ | |||
+ | Copy the script into some accessible directory of your VHOSTed site, change the keyword as you like, and call the script with any browser or applications like wget with something like | ||
+ | |||
+ | < | ||
+ | wget http:// | ||
+ | </ | ||
+ | |||
+ | where you set appropriate paths and replace the keyword according to your choice (it is currently set to ' | ||
+ | |||
+ | If everything is ok, the script will respond with the output of the mdns command; otherwise, it will simply echo your query. All calls are logged, including the IP of the calling host and the submitted query string. The script keeps track of the IP and runs the mdns command only if the IP has changed since the last call. | ||
+ | |||
+ | It is based on the fact that your user id is the first part of the address for VHOSTed sites; as the HOME directory is normally not set for a CGI script, but needed for mdns, the script then gets this information by locally ' | ||
+ | |||
+ | == CGI script == | ||
+ | |||
+ | < | ||
+ | #!/bin/sh | ||
+ | # GPL 2007,2009 Yargo C Bonetti | ||
+ | # Use however you like, at your own risk! | ||
+ | OLDIP=./ | ||
+ | DNSCOM=mdns | ||
+ | LOGF=./ | ||
+ | KEYWORD=setmdns | ||
+ | LOGNAME=${LOGNAME: | ||
+ | HOME=`finger $LOGNAME|awk '/ | ||
+ | export LOGNAME HOME | ||
+ | echo "`date -u +%c` | ||
+ | chmod 600 $LOGF | ||
+ | cat <&1 >/ | ||
+ | if [ -d " | ||
+ | if [ " | ||
+ | echo " | ||
+ | else | ||
+ | | ||
+ | echo $REMOTE_ADDR >$OLDIP | ||
+ | fi | ||
+ | else echo "no $DNSCOM due to bad HOME=$HOME" | ||
+ | fi | ||
+ | else | ||
+ | echo $QUERY_STRING | ||
+ | fi | ||
+ | </ | ||
+ | |||
+ | $Id: mdns-tutorial.html, | ||