Thursday, November 16, 2006

UNIX script to update DynDNS.org

I'm not sure the folks at Dynamic Network Solutions (DynDNS) want you doing this, but there is an UNIX shell script I wrote to update dynamic DNS entries using the Lynx text-only Internet browser.

In case you were not aware, DynDNS offers a fantastic, free dynamic DNS service.

I couldn't find a SCO UNIX updater for DynDNS.org, so I wrote it myself. So... here it is for the rest of the world. Just save the following as /usr/bin/dyndns.sh
#!
# Script to update DynDNS address at DynDNS.org
# LJJ 11/23/2005 - original script
# LJJ 01/04/2007 - added forced updates every X days
# LJJ 01/04/2007 - changed log to /usr/adm/dyndns.log
# LJJ 03/03/2008 - added IP source of WhatIsMyIP.com
# - added DLOG variable
# LJJ 08/20/2009 - set initial NEXT forced update

# reference URLs
# http://www.dyndns.com/developers/specs/syntax.html
# http://www.dyndns.com/developers/specs/policies.html
# http://www.dyndns.com/developers/specs/return.html

# must first sign up for an account and create a host at DynDNS.org (free)
# some firewalls will block, so use firewall dyndns client when possible

# to install, create /etc/rc2.d/S99dyndns with the following line:
# /usr/bin/dyndns.sh &

# do not make SLEEP less than 600 (every 10 minutes)

DLOG=/usr/adm/dyndns.log

FORCE=21
SLEEP=727

DUSR=test
DPWD=test
DHST=test.mine.nu

DADR=
CKIP=
UPDT=YES
NOW=`date +%j`
NEXT=`expr $NOW + $FORCE`

while :
do
# use one or the other
# CKIP=`lynx -dump http://www.whatismyip.com/automation/n09230945.asp |grep "."`
CKIP=`lynx -dump http://checkip.dyndns.org | grep "Current" | awk '{ print $4}'`

NOW=`date +%j`


if [ "$DADR" != "$CKIP" ] ; then
DADR=$CKIP
UPDT=YES
fi

if [ "$NOW" = "$NEXT" ] ; then
DADR=$CKIP
UPDT=YES
fi

if [ $UPDT = "YES" ] ; then
DRES=`lynx --dump -auth $DUSR:$DPWD "http://members.dyndns.org/nic/update?system=dyndns&hostname=$DHST&myip=$DADR&wildcard=OFF&mx=mail.exchanger.ext&backmx=NO&offline=NO"`
echo $0\: `date '+%D %T'` IP Update of $DHST returned $DRES >> $DLOG

NEXT=`expr $NOW + $FORCE`
if [ "$NEXT" -ge 366 ] ; then
NEXT=`expr $NEXT - 365`
fi
sleep $SLEEP
fi

UPDT=NO
sleep $SLEEP

done
exit
UPDATE: I added a "forced update" feature that I found out I needed for dynamic IP addresses which change less often than every 30 days.
UPDATE: Minor tweaks -- makes the script force an updated on day 21 if needed.

5 comments:

  1. Oh, this is for the geeks out there. If you host your own server or want to FTP into your home computer, something like that, the Dynamic DNS is essential. This is a script for updating the service if you are really geeky and have a UNIX server. :-)

    ReplyDelete
  2. Lee, thanks for this. I've got it updating a SCO OSR5 box. Will it continue to run in the background, or should it be cronjobed? I can't find it via ps....

    ReplyDelete
  3. @Patrick - if it starts up in /etc/rc2.d, it will run in background. When you run 'ps -ef' you'll want to grep for 'sleep' and look for the 660 seconds (or whatever you set).

    To test the script, run the command directly in one window and look for the sleep command in another window.

    @Patrick - if it starts up in /etc/rc2.d, it will run in background. When you run 'ps -ef' you'll want to grep for 'sleep' and look for the 660 seconds (or whatever you set).

    To keep the script running without rebooting, use the 'nohup' command (e.g. "nohup /etc/dyndns.sh > /tmp/dyndns.out &").

    ReplyDelete
  4. UPDATE: Makes the script force an updated on day 21 if needed. Had a site that changed only every 60 days or so and the hostname expired.

    ReplyDelete