I wrote a script (my first one) that aims to replace an IP address in a bind configfile whenever it detects that the IP that appears in the file is not the same as the current external IP. I have my own domain but no static IP address, so this script kind of solves things.
The script:
#!/bin/bash
###
### Obtains the current external IP, compares it against the defined
### IPs in the bind config file and, if they do not match,
### it modifies them
###
##
## Variables
##
# File to be modified
currfile=/etc/bind/zones/db.nahue.com.ar
# Current external IP
currextip=$(wget http://ipinfo.io/ip -qO -)
# Current bind config file IP
currbindip=$(cut -f6 $currfile | head -15 | tail -1)
# Current serial number
currbindser=$(cut -f 4 $currfile | head -6 | tail -1)
# Current serial number substring
currbindsersub=$(expr substr $currbindser 1 8)
# Same date serial plus one
newserial1=$(expr $currbindser + 1)
# Current date YYYYMMDD
currdate=$(date +%Y%m%d)
# Current date serial format YYYYMMDDXX
newserial=$(date +%Y%m%d)01
if [ "$currextip" != "$currbindip" ]
then
sed -i -e "s:$currbindip:$currextip:g" "$currfile"
if [ "$currbindsersub" = "$currdate" ]
then
sed -i -e "s:$currbindser:$newserial1:g" "$currfile"
else
sed -i -e "s:$currbindser:$newserial:g" "$currfile"
fi
service bind9 restart
exit
else
exit
fi
Apparently, at some point it misbehaves and leave the bind config file with no IP addresses at all.
Here the config file I'm trying to modify:
;
; bind file for nahue.com.ar
;
$TTL 900
nahue.com.ar. IN SOA ns1.nahue.com.ar. hostmaster.nahue.com.ar. (
2016010403 ;Serial
300 ; Refresh
60 ; Retry
2419200 ; Expire
900 ) ; Negative Cache TTL
; Name servers
@ IN NS dns1-npastorale.no-ip.org.
@ IN NS dns2-npastorale.no-ip.org.
@ IN A 190.245.154.174 ; Script control line
a IN A 190.245.154.174
b IN A 190.245.154.174
c IN A 190.245.154.174
@ IN MX 10 a.nahue.com.ar.
I'm hoping you can help me to figure this out, and I hope I have explained the issue correctly.
Thanks in advance!
expr
with bash:currbindsersub=${currbindser:0:8}
andnewserial1=$((currbindser + 1))
– glenn jackman Jan 04 '16 at 14:34