Update a Dynamic DNS IP with BIND

I wrote the following nsupdate-ddns.sh script to update the dynamic DNS entry for my laptop when switching network locations. There are several ways to execute a script like this automatically (cronjob, startup script, launcher, etc.) — I chose to use Sidekick for Mac OS X, which allows me to execute it when switching locations (either network or physical). This script can also create the private authentication key needed by the DDNS BIND server, and will display some sample configuration values. If you’re setting up a new DDNS BIND server, you can use the examples to configure your dynamic zone file.

You’ll need to edit the value of “ddns_domain” to reflect your dynamic domain name. The default value uses an FQDN in the form of hostname.ddns.domainname.com. The script also assumes the secret key file will reside in /etc/named.d/ and checkip.dyndns.org will be used to get the public (NAT’ed) IP. You may need to adjust the regex if you use another website.

Generally you would execute the nsupdate-ddns.sh script without any parameters, but you can add “–force” to force an update, “–visual” to see what the script has done, and “–keygen” to generate a new secret key file. If you already have a secret key file, the script will exit with an error when you try to overwrite it — you’ll have to remove the old key before generating a new one.

When executed, the script checks to see if a network interface is available, does a DNS lookup to find out the current DDNS IP value, retrieves the public IP from checkip.dyndns.org, and if the two IPs are different, it updates the DDNS zone. The interface availability check uses ifconfig and works on Mac OS X (status: active) and Linux (inet addr:.*Bcast:.*Mask:). You may have to adjust the regex if your ifconfig command output is different.

Download the nsupdate-ddns.sh script.

Find this content useful? Share it with your friends!