printPublicIp: remove .sh
[shutils.git] / nsd-dynipwatch.sh
1 #!/bin/bash
2
3 zonefile=$1
4 host=$2
5 watchfile=$3
6
7
8 function getAddrFromZonefile # <host> <zonefile>
9 {
10 egrep "^$1\\s.*\\sA\\s.*" $2 | egrep -o "([0-9]+\.)+[0-9]+"
11 }
12
13 function setAddrInZonefile # <host> <zonefile> <addr>
14 {
15 sed -i -e "s_^\\($1\\s.*A\\s.*\\s\\)\\([0-9]\\+\\.\\)\\+[0-9]\\+_\\1$3_g" "$2"
16 }
17
18 function getSerialFromZonefile # <zonefile>
19 {
20 awk '/; serial/{print$1}' "$1"
21 }
22
23 function setSerialInZonefile # <zonefile> <serial>
24 {
25 sed -i -e "s_^\\(.*\\s\\)[0-9]\+\\(\\s\\+; serial.*\\)_\\1$2\\2_g" "$1"
26 }
27
28 while true; do
29 inotifywait -q -e close_write "${watchfile}"
30 oldip=`getAddrFromZonefile "${host}" "${zonefile}"`
31 newip=`cat ${watchfile}`
32 echo -n "`date '+%a %F %T'` :: ${oldip} => ${newip}."
33
34 if ! [ "${oldip}" = "${newip}" ]; then
35 serial=`getSerialFromZonefile "${zonefile}"`
36 serial=$(( $serial + 1))
37 echo -n " Update IP address. New serial ${serial}."
38 setAddrInZonefile "${host}" "${zonefile}" "${newip}"
39 setSerialInZonefile "${zonefile}" "${serial}"
40 nsdc rebuild && nscd reload && nscd notify
41 fi
42 echo ""
43 done
44
45