0

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!

Nahuel
  • 3
  • 4

1 Answers1

0

It is possible http://ipinfo.io/ip sometime fails and returns nothing. Anyway, you should at least do a minimum check on $currextip value before passing it to sed.

Adding something like this should be enough:

if [ -z $currextip ]; then
    echo "Something went wrong with ipinfo.io!"
    exit
fi

I don't know if this is really the cause of your problem, but you should always check for failure cases.

As a side note, you should not use the ":" separator with sed in this context, because if you get an IPv6 at some point things could get really wrong.

  • I have 5 more options to obtain my external IP, at first I wanted to use some kind of timeout for each (they are curl commands to some webpage) and then assign the value (if it returned something) to the variable, unfortunately this is way past my scripting abilities, so I wrote something that (I thought) just worked. – Nahuel Jan 04 '16 at 15:03
  • Your script is fine, no need to do something complicated when the problem is simple :-) You should just check for errors before modifying your zone file and simply retry in case something wrong happened. – Nicolas Delvaux Jan 04 '16 at 15:05
  • I'll be using the code you wrote for me and may be "logging" the output of everything. Thanks a lot Nicolas! =) – Nahuel Jan 04 '16 at 15:12