nsd: add nsd-incrserial
[shutils.git] / nsd / nsd-dynipwatch
1 #!/bin/bash
2
3 logfile=
4 watchfile=
5
6 while true; do
7 case $1 in
8 --log) logfile=$2; shift ;;
9 --watchfile) watchfile=$2; shift ;;
10 -h|--help)
11 echo "$0 [--log logfile] [--watchfile /tmp/\${host}.ip] zonefile host"
12 exit 0
13 ;;
14 *) break ;;
15 esac
16 shift
17 done
18
19
20 zonefile=$1
21 host=$2
22 [ -z "${watchfile}" ] && watchfile=/tmp/${host}.ip
23 [ -z "${logfile}" ] && logfile="/var/log/nsd-dynipwatch-${host}.log"
24
25
26 echo "Watch ${watchfile} for host ${host} and update ${zonefile}. Logfile: ${logfile}"
27
28
29 function getAddrFromZonefile # <host> <zonefile>
30 {
31 egrep "^$1\\s.*\\sA\\s.*" $2 | egrep -o "([0-9]+\\.)+[0-9]+"
32 }
33
34 function setAddrInZonefile # <host> <zonefile> <addr>
35 {
36 sed -i -e "s_^\\($1\\s.*A\\s.*\\s\\)\\([0-9]\\+\\.\\)\\+[0-9]\\+_\\1$3_g" "$2"
37 }
38
39
40 function run()
41 {
42 touch ${watchfile}
43 while true; do
44 inotifywait -q -t 30 -e close_write "${watchfile}" > /dev/null
45 ret=$?
46 [ "$ret" = "0" ] || continue
47
48 oldip=`getAddrFromZonefile "${host}" "${zonefile}"`
49 newip=`cat ${watchfile}`
50 echo "`date '+%a %F %T'` :: ${oldip} => ${newip}."
51
52 if ! echo "${newip}" | egrep -q "^([0-9]+\\.)+[0-9]+$" ; then
53 echo "Wrong format of new IP address."
54 continue
55 fi
56
57 if ! [ "${oldip}" = "${newip}" ]; then
58 echo " Update IP address. Increment serial."
59 setAddrInZonefile "${host}" "${zonefile}" "${newip}"
60 nsd-incrserial "${zonefile}"
61
62 /etc/nsd3/signzone.sh sthu.org
63 nsdc rebuild && nsdc reload && nsdc notify
64 fi
65 done
66 }
67
68
69 run >> $logfile
70