User Tools

Site Tools


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. 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

if [ "$(md5sum /root/currentip.txt|awk '{print $1}')" != "$(md5sum /root/lastip.txt|awk '{print $1}')" ]
	echo "$(hostname) IP has changed. old=$(cat /root/lastip.txt); new=$(cat /root/currentip.txt)"
	scp /root/currentip.txt$(hostname).current

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

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

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


	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/ > $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/ $TMP

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

	cp $TMP /etc/bind/

	service bind9 restart

- Reload the slaves

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

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

Step 4: Profit! with a beer.

en/ressources/articles/diy_dyn_dns.txt · Last modified: 2024/04/17 10:19 by