Cacomania: Updating DynDNS via Bash

Cacomania

Updating DynDNS via Bash

Guido Krömer - 4. August 2012 - Tags: , , ,

Because of my old wireless router was not able to update DynDNS, I spend a lot annoying time configuring the ddclient daemon on my home server. But the stability of this tool was really disappointing. After being blocked two times by dyndns, I decided to write my own script which upates DynDNS frequently. With a little potion of bash, curl and grep an own update script for this kind of job is achieved very easy, as you can see below.

#!/bin/bash

USERNAME="user"
PASSWORD="123456"
HOSTNAME="foobar.dyndns-server.com"
IP=`curl -s http://www.whatismyip.de | grep -o '\([0-9]\{1,3\}\.\)\{3\}[0-9]\{1,3\}'`

ABUSE_LOCK_FILE="/tmp/dyndns.abuse"
LAST_IP_FILE="/tmp/lastip"
LAST_IP=`cat $LAST_IP_FILE`

#lockfile check, lockfile is only used if a abuse result appears
if [ -e "$ABUSE_LOCK_FILE" ]; then
     echo "Dyndns abuse lockfile exisits: $ABUSE_LOCK_FILE"  
     exit 1
fi
#end of lockfile check

if [ "$IP" != "$LAST_IP" ]; then
   echo "Current IP: $IP"
   RESULT=`curl -s "http://$USERNAME:$PASSWORD@members.dyndns.org/nic/update?hostname=$HOSTNAME&myip=$IP" | grep -o -E "good|nochg|abuse|badauth|notfqdn|nohost|abuse|dnserr"`
   echo "Dyndns.org says: $RESULT!"
else
   echo "IP is still the same: $LAST_IP"
fi

if [ "$RESULT" == "abuse" ]; then
   echo "Abuse lock!"
   echo "Abuse lock!" > "$ABUSE_LOCK_FILE"
fi

echo "$IP" > "$LAST_IP_FILE"

This script is executed frequently as a cron, the second crontab entry clears the log file once a day.

#Dyndns updater
* * * * *  /scripts/dyndns.sh >> /tmp/dyndns.log
0 0 * * *  /bin/echo "" > /tmp/dyndns.log