Dynamic DNS Updates

Okay, so my OpenDNS configuration wasn’t perfect. OpenDNS liked it just fine, but DynDNS was another story. They tagged me as an abuser and blocked my host name. Oops.

When I built my script using cURL to call the DNS-O-Matic API, I didn’t build in any intelligence. I created a cron job that ran every half hour and call my script. The script simply passed my current IP address to DNS-O-Matic who, in turn, passed it to DynDNS. My approach was the obvious one and at the same time, a bit brutish. If nothing has changed, then no harm, no foul…right?

Understandably, DynDNS didn’t think so. They consider it abuse if their system is getting pinged too often without a change actually being made. By those rules, I was most definitely playing the role of abuser. Today I made a few changes to my script so that it’s a bit more intelligent, while maintaining a degree of simplicity. I also added some output so I could track what was happening if I ever need to do so.

 touch updatedns.log

 echo “”
 echo “========================= `date` =====================”
 echo “”

 # 
 # If a file named updatedns.current doesn’t exist, then retrieve
 # the current network IP address and write it to that file.  Then
 # set the $lastip value to something that we know won’t match.
 # 
 # If the file does exist, then read its contents into the value of 
 # $lastip.
 # 
 if [ ! -e “updatedns.current” ]; then
   echo “updatedns.current does not exist.”
   curl -s -m 60 http://myip.dnsomatic.com/ > updatedns.current
   echo “updatedns.current created.”

   lastip=“UNKNOWN”
 else
   lastip=`cat updatedns.current`
 fi

 # 
 # Retrieve the network IP as it exists right this moment.
 # 
 currentip=`curl -s -m 60 http://myip.dnsomatic.com/`

 echo “Last known IP was $lastip.”
 echo “Current IP verified as $currentip.”

 # 
 # If the last and current IPs are different, then tell
 # DNS-O-Matic to broadcast an update.
 # 
 if [ “$currentip” != “$lastip” ]; then
   echo “$currentip != $lastip.  Updating DNS-O-Matic.”
   curl -m 60 -k -u myusername:mycrazystrongpassword \
        https://updates.dnsomatic.com/nic/update
        ?hostname=all.dnsomatic.com&myip=$ip
        &wildcard=NOCHG&mx=NOCHG&backmx=NOCHG
   echo $currentip > updatedns.current
 else
   echo “IP address has not changed. No action was taken.”
 fi

 exit 0

Note that the URI in the curl statement must be on one line. It’s split here due to space limitations. And the cron job? It looks like this:

*/5 * * * * /usr/local/bin/updatedns.sh >> /usr/local/bin/updatedns.log

Since I’m now determining whether an update needs to be done, I’m running the script every 5 minutes instead of every half hour. Might as well have less lag time when an update is needed, right?

Subscribe0 Comments on Dynamic DNS Updates