[clug] Running my own dynamic DNS?

Tony Breeds tony at bakeyournoodle.com
Tue Feb 25 16:03:16 MST 2014


On Tue, Feb 25, 2014 at 11:26:17PM +1100, Paul Wayper wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
> 
> On 25/02/14 23:10, Andrew Steele wrote:
> > I can't see how you could run your own dynamic dns as you have a bit of
> > a catch-22. Where is your SOA going to reside?  I would imagine you'd
> > need to have that hosted somewhere with a fixed IP address (not at home),
> > and then that would be where you're dynamic dns update server (for want
> > of a better name) would reside.
> 
> Well, I do have mabula.net already, as a static IP address which hosts my
> main website.  So "home.mabula.net" as a dynamic DNS record would be
> perfectly reasonable there.

That should be pretty trivial assuming mabula.net is running bind 8 or 9
I've been doing this for some time but I can't actually recall how I set
it up.

Googling for bind9 nsupdate seems to provide some plausible looking
pages.

http://www.bind9.net/manual/bind/9.3.1/Bv9ARM.html
https://www.erianna.com/nsupdate-dynamic-dns-updates-with-bind9

My system that dos the actual ns update is NOT my ppp link so I run the
following from cron.  If you have a "ppp interface up" event you canhang
off of then it's a little easier.

---
#!/bin/bash

host=$( /bin/hostname )
domain="XXXXX"
hostname="${host}.${domain}"
domains="${domain}"

dns_server_name="XXXXX"
# FIXME: Should use current_dns_ip, but this requires moving stuff around.
dns_server_ip="XXXXX"

url="http://whatismyip.bakeyournoodle.com/"
keyfile="XXXXX"

function log() {
        prg=$( /usr/bin/basename "$0" )

        #FIXME: find dynamic way of deteching cron
        if [ 0 -eq 1 ] ; then
                /bin/echo "${@}"
        else
                /usr/bin/logger -p local0.notice -t ${prg} "${@}"
        fi
}

function current_dns_ip() {
        name=$1
        ip=$( /usr/bin/host -r -t A  ${name} ${dns_server_name} 2>&1 | /usr/bin/awk '/has address/ {print $4}' 2>&1 )

        if [ -z "${ip}" ] ; then
                ip=0.0.0.0
        fi

        /bin/echo "$ip"
}

function current_ip() {
        ip=$( /usr/bin/curl -s "${url}" 2>&1 | /usr/bin/awk '/^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$/ {print $0}' 2>&1 )

        if [ -z "${ip}" ] ; then
                ip=0.0.0.0
        fi

        /bin/echo "$ip"
}

function update_dns() {
        host=$1
        ip=$2

        for domain in ${domains}; do
        (
        cat <<HERE
server ${dns_server_ip}
zone ${domain}
update delete ${host}.${domain} A
update add ${host}.${domain} 3600 A ${ip}
send
HERE
        ) |  /usr/bin/nsupdate -k ${keyfile}

        done
}

export PATH=$HOME/bin:/usr/local/bin:/usr/bin:/bin:/usr/bin/X11:/usr/games

ip=$( current_ip )
if [ "${ip}" == "0.0.0.0" ] ; then
        log "Checking external IP failed cowardly refusing to update DNS"
        exit 1
fi

dns=$( current_dns_ip "${hostname}" )
if [ "${dns}" == "0.0.0.0" ] ; then
        log "Checking IP via DNS failed cowardly refusing to update DNS"
        exit 1
fi

if [ "${ip}" != "${dns}" ] ; then
        log "Updating DNS"
        update_dns ${host} ${ip}
else
        log "IP records match, not updating DNS"
fi

---

Yours Tony
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 819 bytes
Desc: not available
URL: <http://lists.samba.org/pipermail/linux/attachments/20140226/26f94e8f/attachment.pgp>


More information about the linux mailing list