Table of Contents

Do It Yourself Dyn Dns

Dyndns does not propose its free service anymore. This is sad, because my verizon fios connection has a bad tendency to change its IP more often that I'd like. So I scripted a couple of cron jobs and worked around the problem with an automated root dns update.

linuxwall.info DNS servers run on bind. Which mean we control the zone with a zone file on the root dns. That root DNS can be updated manually, or, as I'm going to show, with a couple of sed commands.

- Get the IP to the root DNS

Step 1 is to have the server discover its new IP, and push it to the root DNS server. This is very simple to do in bash:

#!/usr/bin/env bash

cp /root/currentip.txt /root/lastip.txt

curl -s -o /root/currentip.txt http://ip.cow.org

if [ "$(md5sum /root/currentip.txt|awk '{print $1}')" != "$(md5sum /root/lastip.txt|awk '{print $1}')" ]
then
	echo "$(hostname) IP has changed. old=$(cat /root/lastip.txt); new=$(cat /root/currentip.txt)"
	scp /root/currentip.txt dnsupdater@rootdns.linuxwall.info:ips/$(hostname).current
fi

Get the new IP, and SCP it to the dns server.

- Update the Root DNS

Step 2 is a bit more tricky, because we need to parse the zone file with sed. Nothing tremendously difficult, but it's sed so be careful.

#!/usr/bin/env bash

MAILTO=notification@linuxwall.info

somehostip=$(cat /home/dnsupdater/ips/somehost.current)

if [ "$(grep -E "^somehost[[:space:]]+1h[[:space:]]+IN[[:space:]]+A[[:space:]]+$somehostip" /etc/bind/linuxwall.info.db)" = "" ]
then
	echo "replacing somehost IP with new one"

	TMP=$(mktemp)

	sed -r "s|somehost\t\t1h\tIN\tA\t(\b[0-9]{1,3}\.){3}[0-9]{1,3}\b|somehost\t\t1h\tIN\tA\t$somehostip|" /etc/bind/linuxwall.info.db > $TMP

	sed -ri "s|\t[0-9]{10};numero de serie de la zone. Format : YYYYMMDDnn|\t$(date +%Y%m%d%H);numero de serie de la zone. Format : YYYYMMDDnn|" $TMP

	diff /etc/bind/linuxwall.info.db $TMP

	cp /etc/bind/linuxwall.info.db{,.bkp$(date +%Y%m%d%H)}

	cp $TMP /etc/bind/linuxwall.info.db

	service bind9 restart
fi

- Reload the slaves

Step 3 is really straighforward, simply run rndc reload on all the slaves.

#!/usr/bin/env bash
/usr/sbin/rndc reload linuxwall.info 2>&1 1>/dev/null

Step 4: Profit! with a beer.