Add nsd-dynipwatch.sh
authorStefan Huber <shuber2@gmx.at>
Sun, 28 Oct 2012 17:28:10 +0000 (18:28 +0100)
committerStefan Huber <shuber2@gmx.at>
Sun, 28 Oct 2012 17:28:10 +0000 (18:28 +0100)
nsd-dynipwatch.sh [new file with mode: 0755]
nsd-sample.zone [new file with mode: 0644]

diff --git a/nsd-dynipwatch.sh b/nsd-dynipwatch.sh
new file mode 100755 (executable)
index 0000000..0698afc
--- /dev/null
@@ -0,0 +1,45 @@
+#!/bin/bash
+
+zonefile=$1
+host=$2
+watchfile=$3
+
+
+function getAddrFromZonefile # <host> <zonefile>
+{
+       egrep "^$1\\s.*\\sA\\s.*" $2 | egrep -o "([0-9]+\.)+[0-9]+"
+}
+
+function setAddrInZonefile # <host> <zonefile> <addr>
+{
+       sed -i -e "s_^\\($1\\s.*A\\s.*\\s\\)\\([0-9]\\+\\.\\)\\+[0-9]\\+_\\1$3_g" "$2"
+}
+
+function getSerialFromZonefile # <zonefile>
+{
+       awk '/; serial/{print$1}' "$1"
+}
+
+function setSerialInZonefile # <zonefile> <serial>
+{
+       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 (file)
index 0000000..a305ff8
--- /dev/null
@@ -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.