]> git.sthu.org Git - shutils.git/commitdiff
Add nsd-dynipwatch
authorStefan Huber <shuber2@gmx.at>
Sun, 28 Oct 2012 19:34:26 +0000 (20:34 +0100)
committerStefan Huber <shuber2@gmx.at>
Sun, 28 Oct 2012 19:34:26 +0000 (20:34 +0100)
init.d/nsd-dynipwatch [new file with mode: 0755]
nsd-dynipwatch [new file with mode: 0755]
nsd-dynipwatch.sh [deleted file]
nsd-sample.zone
printPublicIp.sh [deleted file]

diff --git a/init.d/nsd-dynipwatch b/init.d/nsd-dynipwatch
new file mode 100755 (executable)
index 0000000..9696359
--- /dev/null
@@ -0,0 +1,42 @@
+#!/bin/sh -e
+### BEGIN INIT INFO
+# Provides:          nsd-dynipwatch
+# Required-Start:    $nsd3
+# Required-Stop:     $nsd3
+# Should-Start:      $local_fs
+# Should-Stop:       $local_fs
+# Default-Start:     2 3 4 5
+# Default-Stop:      0 1 6
+# Short-Description: Start nsd dynip update
+# Description:       Provides an updater for NSD3 zone files
+### END INIT INFO
+
+
+
+. /lib/lsb/init-functions
+
+PIDFILE=/var/run/nsd-dynipwatch.pid
+IPFILE=/tmp/euklid.ip
+ARGS="/etc/nsd3/sthu.org.zone euklid ${IPFILE}"
+
+case "$1" in
+start)
+        start-stop-daemon --start --quiet --background --pidfile $PIDFILE --make-pidfile --exec /usr/local/bin/nsd-dynipwatch -- $ARGS
+       ;;
+stop)
+        start-stop-daemon --stop --quiet --pidfile $PIDFILE
+        ;;
+restart)
+        start-stop-daemon --stop --retry 5 --quiet --pidfile $PIDFILE
+        start-stop-daemon --start --quiet --background --pidfile $PIDFILE --make-pidfile --exec /usr/local/bin/nsd-dynipwatch -- $ARGS
+        ;;
+status)
+       status_of_proc -p $PIDFILE /usr/local/bin/nsd-dynipwatch nsd-dynipwatch && exit 0 || exit $?
+       ;;
+*)
+       log_action_msg "Usage: /etc/init.d/cron {start|stop|restart}"
+        exit 2
+        ;;
+esac
+exit 0
+
diff --git a/nsd-dynipwatch b/nsd-dynipwatch
new file mode 100755 (executable)
index 0000000..fd064bb
--- /dev/null
@@ -0,0 +1,72 @@
+#!/bin/bash
+
+logfile=
+
+while true; do
+       case $1 in
+               --log) logfile=$2; shift ;;
+               -h|--help)
+                       echo "$0 [-f] [--log logfile] zonefile host watchfile"
+                       exit 0
+                       ;;
+               *) break ;;
+       esac
+       shift
+done
+
+
+zonefile=$1
+host=$2
+watchfile=$3
+[ -z "${logfile}" ] && logfile="/var/log/nsd-dynipwatch-${host}.log"
+
+echo "logfile: ${logfile}"
+
+
+
+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"
+}
+
+function run()
+{
+       while true; do
+               inotifywait -q -e close_write "${watchfile}"  > /dev/null
+               oldip=`getAddrFromZonefile "${host}" "${zonefile}"`
+               newip=`cat ${watchfile}`
+               echo "`date '+%a %F %T'` :: ${oldip} => ${newip}."
+
+               if ! echo "${newip}" | egrep "^([0-9]+\\.)+[0-9]+$"  ; then
+                       echo "Wrong format of new IP address."
+                       continue
+               fi
+
+               if ! [ "${oldip}" = "${newip}" ]; then
+                       serial=`getSerialFromZonefile "${zonefile}"`
+                       serial=$(( $serial + 1))
+                       echo " Update IP address. New serial ${serial}."
+                       setAddrInZonefile "${host}" "${zonefile}" "${newip}"
+                       setSerialInZonefile "${zonefile}" "${serial}"
+                       nsdc rebuild && nsdc reload && nsdc notify
+               fi
+       done
+}
+
+run >> $logfile
+
diff --git a/nsd-dynipwatch.sh b/nsd-dynipwatch.sh
deleted file mode 100755 (executable)
index 0698afc..0000000
+++ /dev/null
@@ -1,45 +0,0 @@
-#!/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
-
-
index a305ff81761e6299311fc25d3778a39e799deac2..d29699d6f7a926a72d10b32acdeff4ce0fddd278 100644 (file)
@@ -1,6 +1,6 @@
 $TTL    3h
 @       IN      SOA     ns1.sthu.org. shuber2.gmx.at. (
-                                2               ; serial number
+                                1               ; serial number
                                 28800           ; Refresh
                                 7200            ; Retry
                                 864000          ; Expire
@@ -13,7 +13,7 @@ $TTL    3h
 
 sthu.org.               IN      A       37.247.49.68
 ns1                     IN      A       37.247.49.68
-euklid                  IN      A       2.3.4.5
+euklid                  IN      A       1.2.3.42
 euklid2                 IN      A       1.2.3.43
 ns2                     IN      A       174.37.196.55   ; ns2.afraid.org
 www                     IN      CNAME   sthu.org.
diff --git a/printPublicIp.sh b/printPublicIp.sh
deleted file mode 100755 (executable)
index 8938f6c..0000000
+++ /dev/null
@@ -1,3 +0,0 @@
-#!/bin/sh
-
-curl www.sthu.org/whatismyip.html