From: Stefan Huber Date: Sun, 28 Oct 2012 17:28:10 +0000 (+0100) Subject: Add nsd-dynipwatch.sh X-Git-Url: https://git.sthu.org/?p=shutils.git;a=commitdiff_plain;h=62d133b5f78c3b113e964ac0635ee137de2afa05 Add nsd-dynipwatch.sh --- diff --git a/nsd-dynipwatch.sh b/nsd-dynipwatch.sh new file mode 100755 index 0000000..0698afc --- /dev/null +++ b/nsd-dynipwatch.sh @@ -0,0 +1,45 @@ +#!/bin/bash + +zonefile=$1 +host=$2 +watchfile=$3 + + +function getAddrFromZonefile # +{ + egrep "^$1\\s.*\\sA\\s.*" $2 | egrep -o "([0-9]+\.)+[0-9]+" +} + +function setAddrInZonefile # +{ + sed -i -e "s_^\\($1\\s.*A\\s.*\\s\\)\\([0-9]\\+\\.\\)\\+[0-9]\\+_\\1$3_g" "$2" +} + +function getSerialFromZonefile # +{ + awk '/; serial/{print$1}' "$1" +} + +function setSerialInZonefile # +{ + sed -i -e "s_^\\(.*\\s\\)[0-9]\+\\(\\s\\+; serial.*\\)_\\1$2\\2_g" "$1" +} + +while true; do + inotifywait -q -e close_write "${watchfile}" + oldip=`getAddrFromZonefile "${host}" "${zonefile}"` + newip=`cat ${watchfile}` + echo -n "`date '+%a %F %T'` :: ${oldip} => ${newip}." + + if ! [ "${oldip}" = "${newip}" ]; then + serial=`getSerialFromZonefile "${zonefile}"` + serial=$(( $serial + 1)) + echo -n " Update IP address. New serial ${serial}." + setAddrInZonefile "${host}" "${zonefile}" "${newip}" + setSerialInZonefile "${zonefile}" "${serial}" + nsdc rebuild && nscd reload && nscd notify + fi + echo "" +done + + diff --git a/nsd-sample.zone b/nsd-sample.zone new file mode 100644 index 0000000..a305ff8 --- /dev/null +++ b/nsd-sample.zone @@ -0,0 +1,19 @@ +$TTL 3h +@ IN SOA ns1.sthu.org. shuber2.gmx.at. ( + 2 ; serial number + 28800 ; Refresh + 7200 ; Retry + 864000 ; Expire + 86400 ; Min TTL + ) + + NS ns1.sthu.org. + NS ns2.sthu.org. + + +sthu.org. IN A 37.247.49.68 +ns1 IN A 37.247.49.68 +euklid IN A 2.3.4.5 +euklid2 IN A 1.2.3.43 +ns2 IN A 174.37.196.55 ; ns2.afraid.org +www IN CNAME sthu.org.