On 03/18/2010 08:52 PM, Brad Tilley wrote:
There are ports that do this with more features, but I thought others
might like to do it in base with no added software. I've been using this
script since 4.2 and it works OK:

#!/bin/ksh

# Cron this script to run every X minutes. Written for OpenBSD.

# Get Current IP
lynx -dump http://checkip.dyndns.org:8245/ | awk '{print $4}' | sed
'/^$/d'>  ip_new.txt

# compare new with old
diff ip_new.txt ip_old.txt

# if different, send update
if [ $? -ne 0 ]
then
   #echo "The IP has changed"
   ip=$(cat ip_new.txt)
   # Following two lines are optional. Log date of change and IP history.
   date>>  ip_date.txt
   cat ip_old.txt>>  ip_history.txt
   curl --insecure
   
"https://user:p...@members.dyndns.org/nic/update?hostname=host.xxx&myip=$ip&wildcard=NOCHG&mx=NOCHG&backmx=NOCHG";
fi

# Whether a change has occurred or not, overwrite old with new
cp ip_new.txt ip_old.txt

Did you ever have a problem with not updating in a certain period of time, and DynDNS expiring your account? I did, probably because I have one of the free accounts, and I had to add code to my own updater script to account for that (i.e., send an update every 30 days whether my IP had changed or not).

Mine runs hourly out of cron, too. I tried invoking it from dhclient-script, but never could get it to work reliably, and I didn't want to hack dhclient-script itself. I maybe ought to have another look at that, though -- some things probably have changed there since the last time I tried.

Corey

P.S. Here's mine, if anyone's interested.

#!/bin/sh
# Update dyndns.com record if IP address changed (run on bootup and
# out of cron)

#set -x

INTFC=vr0
INFO=/var/run/dyndns-update.dat
MAX_INTERVAL=30 #days

function log {
   # Log activity to syslog (daemon facility)
   logger -p daemon.info -t ddns-update "$*"
}

function dns-update {
   # dyndns.org update site
   url='http://members.dyndns.org/nic/update?'
   url=${url}'system=dyndns&hostname=clingo.dyndns.org'
   url=${url}"myip=$1"
   log "Server response: `lynx -dump -auth user:pass $url`"
}


# Main program
today=`date '+%j'`

# Get current external IP address. If it's not set, don't update.
current_ip=`ifconfig $INTFC | awk '/inet / { print $2 }' | \
   grep -v '192.168.1'`
if [ -z $current_ip ]; then
   log "No IP address for $INTFC, exiting"
   exit 1
fi

# Get last run's IP and day from file.  If there's no file, don't update,
# but try to create one for next time
if [ -r $INFO ]; then
   last_ip=`cat $INFO | awk '{ print $1 }'`
   last_day=`cat $INFO | awk '{ print $2 }'`
else
   log "Data file $INFO not found, exiting"
   echo $current_ip $today > $INFO
   exit 2
fi
# Write this run's data to file
echo $current_ip $today > $INFO

# If current IP does not match last IP, then update
if [ "$current_ip" != "$last_ip" ]; then
   log "IP changed to $current_ip, updating"
   dns-update $current_ip
   exit 0
else
   msg="No change in IP, exiting" # and see if time triggers an update
fi

# If it has been more than xx days since last update, force one. They
# act like if the address hasn't changed it's "abuse", yet I get emails
# every so often wanting me to confirm continued activity
if [ ! -z $last_day ]; then
   interval=$(( $today - $last_day ))
else
   interval=0
fi

[ $interval -lt 0 ] && interval=$(( $interval + 366 )) # year rollover
if [ $interval -gt $MAX_INTERVAL ]; then
   log "More than $MAX_INTERVAL days since last update, updating"
   dns-update $current_ip
   exit 0
fi

# fallthru from above
log $msg

exit 0

Reply via email to