From: Stefan Huber Date: Sun, 28 Oct 2012 19:34:26 +0000 (+0100) Subject: Add nsd-dynipwatch X-Git-Url: https://git.sthu.org/?a=commitdiff_plain;h=bd37dc4848205495b7fe116d83bb391a0cbf981b;p=shutils.git Add nsd-dynipwatch --- diff --git a/init.d/nsd-dynipwatch b/init.d/nsd-dynipwatch new file mode 100755 index 0000000..9696359 --- /dev/null +++ b/init.d/nsd-dynipwatch @@ -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 index 0000000..fd064bb --- /dev/null +++ b/nsd-dynipwatch @@ -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 # +{ + 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" +} + +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 index 0698afc..0000000 --- a/nsd-dynipwatch.sh +++ /dev/null @@ -1,45 +0,0 @@ -#!/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 index a305ff8..d29699d 100644 --- a/nsd-sample.zone +++ b/nsd-sample.zone @@ -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 index 8938f6c..0000000 --- a/printPublicIp.sh +++ /dev/null @@ -1,3 +0,0 @@ -#!/bin/sh - -curl www.sthu.org/whatismyip.html