[ SDF Public Access UNIX System .. Est. 1987 ]

join welcome faq status members projects store tour gopher abuse dialup dsl minecraft social
tilde nihongo europa webmail gallery usermap teach irc tutorials software telnet ssh
WAN IP Retrieval and Dynamic DNS (mdns)

WAN IP Retrieval and Dynamic DNS (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/sh
IP=$(echo $SSH_CLIENT | awk '{print $1}')
OLDIP=$(cat ~/bin/.ip)
if [ "$IP" != "$OLDIP" ] ;
then
        echo "$IP" > ~/bin/.ip
        mdns "$IP"
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 '~/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'). Note also that on SDF-EU, the command to set the dynamic address is zdns, not mdns.

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.18 2015/02/02 03:55:52 grobe0ba Exp $

©1987-2065 SDF Public Access UNIX System, Inc. 501(c)(7)
(this page was generated using ksh, sed and awk)