#!/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 # { 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 run() { touch ${watchfile} 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 echo " Update IP address. Increment serial." setAddrInZonefile "${host}" "${zonefile}" "${newip}" nsd-incrserial "${zonefile}" /etc/nsd3/signzone.sh sthu.org nsdc rebuild && nsdc reload && nsdc notify fi done } run >> $logfile