WAN IP Retrieval and Dynamic DNS (mdns)

Note that on SDF-EU, the command to set the dynamic address is "zdns", not mdns.

There are a myriad of 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 File Permissions.

Script 1

Using the variables $SSH_CLIENT and $SSH_CONNECTION, after logging into a remote site (any remote site), you can learn your local WAN IP address by executing either of the following two commands:

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/ksh
  IP=$(echo $SSH_CLIENT | awk '{print $1}')
  mdns $IP

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 '~/bin/ip' >/dev/null

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 '~/bin/ip' >>$HOME/logs/ip_update

Add the script to your home machine's cron and execute every 24 hours or so. Ex:

  * */24 * * * $HOME/bin/ipset

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' | awk '{print $1}')
  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://yourlogin.freeshell.org/setmdns.cgi?keyword

where you set appropriate paths and replace the keyword according to your choice (it is currently set to 'setmdns').

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 'finger' the user id. This looks like a good way to find the home directory based on the user id. The script then changes to that directory and checks for the keyword in the query string, and if found, executes mdns with the IP of the calling client.

CGI script
  #!/bin/sh
  # GPL 2007,2009 Yargo C Bonetti
  # Use however you like, at your own risk!
  OLDIP=./.oldip
  DNSCOM=mdns
  LOGF=./.setmdns.log
  KEYWORD=setmdns
  LOGNAME=${LOGNAME:-${HTTP_HOST%%.*}}
  HOME=`finger $LOGNAME|awk '/^Directory:/{print $2}'`
  export LOGNAME HOME
  echo "`date -u +%c`     $REMOTE_ADDR    $QUERY_STRING" >>$LOGF
  chmod 600 $LOGF
  cat <&1 >/dev/null ; then
   if [ -d "$HOME" ] ; then
    if [ "$REMOTE_ADDR" = "`cat $OLDIP`" ] ; then
     echo "Keeping ip at $REMOTE_ADDR"
    else
     $DNSCOM $REMOTE_ADDR
     echo $REMOTE_ADDR >$OLDIP
    fi
   else echo "no $DNSCOM due to bad HOME=$HOME"
   fi
  else
   echo $QUERY_STRING
  fi

$Id: mdns-tutorial.html,v 1.16 2012/03/09 01:12:11 jpetre Exp $