+#!/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
+
+