Azmawee 127.0.0.1

"Mauiwagen. Das Server." – pub 4096R/712DAA69

Dynamic DNS auto-update script for nsupdate (bind)

** UPDATED 19 Jan 2015 ** – Script updated to version 1.9 (Fix for dual stack ipv6/ipv4 connection), do get it from the direct fetch link same as inside this article.

This is my custom dynamic DNS auto-update script for nsupdate in bind, well this is for pppoe connection such as streamyx/unifi (in Malaysia). The script support FreeBSD userland ppp and mpd5 (or any other compatible device), just link this script to your ip-up script in ppp or mpd5.

Updated the script to version 1.8, to support bind 9.9.x and some code optimization. – 29 Jan 2014.

Direct link for fetch HERE

#!/bin/sh
# FreeBSD dynamic dns auto-update script for nsupdate in bind.
# Version 1.8 – maui[AT]mybsd.org.my ; azmawee[AT]azmawee.com – “I don’t ask permission, I set it using chmod”.
# This script can update one, two or three A records at once for one domain/zone to your dns server using nsupdate.
# Change Log :-
# * Version 1.8 – Bugs fixed and support for Bind 9.9.x.
# * Version 1.7 – Bugs fixed – script not updating all A records properly.
# * Version 1.6 – Fixed bug when using more then one script in one time.
# * Version 1.5 – Added ability to update one, two or three A records in one go (can add more, depend on request)
# * Version 1.4 – Changed default ttl to a lower value, we need fast dns update on dynamic IP.
# * Version 1.3 – Fixed issue for “A record” that have multiple ip’s, please email me if you find any bug.
# * Version 1.2 – Logs all activities (almost).
# * Version 1.1 – Fixed minor bug.
# * version 1.0 – Script born.

# Edit to suite your taste.
# Wan interface, normally, “tun0” for userland ppp and “ng0″ for mpd5
if=”ng0″

# Log file, to rotate/limit the log, you can add it to /etc/newsyslog.conf
log=”/var/log/nsupdate.log”

# Private Key location
key=”/etc/namedb/<your private key>”

# Path to nsupdate
nsupdate=”/usr/bin/nsupdate”

# Your master DNS server, hostname or ip
dnsserver=”billgates.microsof.com”

# Your Top-Level-Domain name (TLD)/zone
zone=”domain.com”

# Set value for how many domain/subdomain you want to update in one time for this script.
# Set “1” to update one A record,
# Set “2” to update two A records in one go,
# Set “3” to update three A records in one go.
update=”2″

# First A record to update (for update=”1″, “2” or/and “3”), domain/subdomain and TTL (in second)
# Low ttl value is recommended for fast IP update.
domain=”azmawee.com”
ttl=”60″

# Second A record to update (for update=”2″ or/and “3”), domain/subdomain and TTL (in second)
domain2=”sub2.domain.com”
ttl2=”60″

# Third A record to update (for update=”3″), domain/subdomain and TTL (in second)
domain3=”sub3.comain.com”
ttl3=”60″

# The script, you don’t need to edit below this, unless you know what you are doing. :)
if [ ! -r “$key” ]; then
echo “`date` Key file not found, please set Key file location.” >> $log
echo “Key file not found, please set Key file location.”
exit 1
fi
if [ ! -r “$nsupdate” ]; then
echo “`date` nsupdate command not found, please set nsupdate path.” >> $log
echo “nsupdate command not found, please set nsupdate path.”
exit 1
fi
wanip=`ifconfig $if | grep inet | cut -f2 | awk ‘{ print $2 }’`
if [ -z $wanip ]; then
echo “`date` No wanip detected on $if.” >> $log
echo “No wanip detected on $if.”
exit 1
fi
# Checking record(s).
if [ $update = 1 ] || [ $update = 2 ] || [ $update = 3 ]; then
arec=`dig “$domain” +noall +answer | tail -n 1 | awk ‘{ print $5 }’`
if [ -z $arec ]; then
echo “`date` Cannot resolved your first A record of $domain” >> $log
echo “Cannot resolved your first A record of $domain.”
exit 1
fi
fi
if [ $update = 2 ] || [ $update = 3 ]; then
arec2=`dig “$domain2” +noall +answer | tail -n 1 | awk ‘{ print $5 }’`
if [ -z $arec2 ]; then
echo “`date` Cannot resolved your second A record of $domain2” >> $log
echo “Cannot resolved your second A record of $domain2.”
exit 1
fi
fi
if [ $update = 3 ]; then
arec3=`dig “$domain3” +noall +answer | tail -n 1 | awk ‘{ print $5 }’`
if [ -z $arec3 ]; then
echo “`date` Cannot resolved your third A record of $domain3” >> $log
echo “Cannot resolved your third A record of $domain3.”
exit 1
fi
fi
# Check and create temporary config file in /tmp, you may see double file name like /tmp/mauix.com-mauix.com,
# it’s normal, need a unique temporary file name, to properly update all A records when using more then one script in one time. :)
case “$update” in
1)
if [ $wanip = $arec ]; then
echo “`date` First A record of $domain ($arec) still same as your wanip ($wanip), no update needed.” >> $log
exit 0
else
echo “`date` New IP detected, updating A record…” >> $log
echo “server $dnsserver” > /tmp/$zone-$domain
echo “zone $zone” >> /tmp/$zone-$domain
echo “update delete $domain. A” >> /tmp/$zone-$domain
echo “update add $domain. $ttl A $wanip” >> /tmp/$zone-$domain
fi
;;
2)
if [ $wanip = $arec ]; then
echo “`date` First A record of $domain ($arec) still same as your wanip ($wanip), no update needed.” >> $log
fi
if [ $wanip = $arec2 ]; then
echo “`date` Second A record of $domain2 ($arec2) still same as your wanip ($wanip), no update needed.” >> $log
exit 0
else
echo “`date` New IP detected, updating A record(s)…” >> $log
echo “server $dnsserver” > /tmp/$zone-$domain
echo “zone $zone” >> /tmp/$zone-$domain
if [ $wanip != $arec ]; then
echo “update delete $domain. A” >> /tmp/$zone-$domain
echo “update add $domain. $ttl A $wanip” >> /tmp/$zone-$domain
fi
echo “update delete $domain2. A” >> /tmp/$zone-$domain
echo “update add $domain2. $ttl2 A $wanip” >> /tmp/$zone-$domain
fi
;;
3)
if [ $wanip = $arec ]; then
echo “`date` First A record of $domain ($arec) still same as your wanip ($wanip), no update needed.” >> $log
fi
if [ $wanip = $arec2 ]; then
echo “`date` Second A record of $domain2 ($arec2) still same as your wanip ($wanip), no update needed.” >> $log
fi
if [ $wanip = $arec3 ]; then
echo “`date` Third A record of $domain3 ($arec3) still same as your wanip ($wanip), no update needed.” >> $log
exit 0
else
echo “`date` New IP detected, updating A record(s)…” >> $log
echo “server $dnsserver” > /tmp/$zone-$domain
echo “zone $zone” >> /tmp/$zone-$domain
if [ $wanip != $arec ]; then
echo “update delete $domain. A” >> /tmp/$zone-$domain
echo “update add $domain. $ttl A $wanip” >> /tmp/$zone-$domain
fi
if [ $wanip != $arec2 ]; then
echo “update delete $domain2. A” >> /tmp/$zone-$domain
echo “update add $domain2. $ttl2 A $wanip” >> /tmp/$zone-$domain
fi
echo “update delete $domain3. A” >> /tmp/$zone-$domain
echo “update add $domain3. $ttl3 A $wanip” >> /tmp/$zone-$domain
fi
;;
*)
echo “`date` Script stoped! please select update types correctly.” >> $log
echo “Script stoped! please select update type correctly.”
exit 1
;;
esac
# Updating record(s)
if [ $wanip != $arec ] || [ $wanip != $arec2 ] || [ $wanip != $arec3 ]; then
echo “show” >> /tmp/$zone-$domain
echo “send” >> /tmp/$zone-$domain
sleep 1
$nsupdate -k $key -v /tmp/$zone-$domain >> $log
echo “`date` Finished updating A record(s), successfully updated if no error above.” >> $log
fi

, , , , , , , , , , , ,

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Azmawee 127.0.0.1
MaUi^ - pub 4096R/712DAA69
Fingerprint 8BF7 D0AF CA45 5313 A3BF ACFB B90B 66C7 712D AA69
Website Security Test
0110010101100001011100110111010001100101011100100010000001100101011001110110011100100000011011100110111100101110001000000011000000110110