--- /dev/null
+#!/bin/bash
+
+logfile=
+watchfile=
+
+while true; do
+ case $1 in
+ --log) logfile=$2; shift ;;
+ --watchfile) watchfile=$2; shift ;;
+ -h|--help)
+ echo "$0 [--log logfile] [--watchfile /tmp/\${host}.ip] zonefile host"
+ exit 0
+ ;;
+ *) break ;;
+ esac
+ shift
+done
+
+
+zonefile=$1
+host=$2
+[ -z "${watchfile}" ] && watchfile=/tmp/${host}.ip
+[ -z "${logfile}" ] && logfile="/var/log/nsd-dynipwatch-${host}.log"
+
+
+echo "Watch ${watchfile} for host ${host} and update ${zonefile}. 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 -t 30 -e close_write "${watchfile}" > /dev/null
+ ret=$?
+ [ "$ret" = "0" ] || continue
+
+ oldip=`getAddrFromZonefile "${host}" "${zonefile}"`
+ newip=`cat ${watchfile}`
+ echo "`date '+%a %F %T'` :: ${oldip} => ${newip}."
+
+ if ! echo "${newip}" | egrep -q "^([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
+