Thursday, July 13, 2017

FreeDNS dynamic DNS update client (revisited)

Back in 2011 I posted a tweaked script from afraid.org (of FreeDNS) - named dnsactual.sh - to update your IP addresses dynamically in this DNS service provider.

Today I reviewed it, and I further tweaked it, removing dependencies for external web sites.
Before, a GET to http://ipecho.net/plain (or other similar site) was required to get you your public IP address.
But if the site is unavailable, you could get in trouble and end up shooting yourself on the foot, messing up your dynamic DNS entries.

Thanks to OpenDNS (or the omnipresent Google), there is a way to get your IP address with a simple 'dig' query :-)

dig +short myip.opendns.com @resolver1.opendns.com
dig TXT +short o-o.myaddr.l.google.com @ns1.google.com [ trackback ]

Now the updated script. Feel free to used, edit, magle, whatever!
This logs any updates to syslog, and appends to file, for historic reasons.
I know it could have a lot more health checks for error handling, but for me it's enough.

#!/bin/bash

FQDN="## your own FQDN on FreeDNS ##"
TOKENS="## get your own##"
PATH=/bin:/sbin:/usr/bin:/usr/sbin
LOGGER="logger -t FREEDNS"

CacheIP=$(dig +short $FQDN @ns1.afraid.org)
CurreIP=$(dig +short myip.opendns.com @resolver1.opendns.com)

if [ "$CurreIP" != "$CacheIP" ]
then
  for A in $TOKENS ; do
    $LOGGER `curl -k -s "https://freedns.afraid.org/dynamic/update.php?$A"`
  done
  echo "`date +%Y%m%d-%H%M`: New IP address $CurreIP (old was $CacheIP)" >> /var/log/freedns.log
else
  $LOGGER "Public IP hasn't changed..."
  # the 'else' section is just to validate if the script is actually running, not really needed once tested
fi