A custom bind updater in bash

edited August 2017 in Programming
Hello every one! I have just made this bind updater in bash and I would like to share it out to the world. I have a bind server running on my home network, as many people know, home networks usually have a dynamic ip address. This script allows the admin to constantly monitor the ever changing ip address with a cron job. Some notes:

The useip variable reads from a file called useip. Of which that file contains the current in use ip address. It is very important to copy the current dynamic ip address into this file.

Please make any adjustments to the code as my server runs on OpenIndiana (Solaris). Like for example if you want to use this on a systemd system you should change the following:
svcadm restart /network/dns/server:default    svcs -xv  /network/dns/server:default

to

systemctl restart bind    systemctl status bind

I will love any feed back, as I will like to make this more useful for every one.
# Bind Updater for Solaris, Linux, and any thing that is bash!
# Updates your Dynamic IP address in your A record!

#Change directory for log destination
log=bind_update.log

#Get used ip address
useip=`cat useip`
#Get current Public IP
ip_address=`wget http://ipecho.net/plain -O - -q ; echo`

#Get the ip address to replace and save in varible
old_ip=`cat db.bind | grep example.org. | grep "IN A"`

if [ "$useip" != "$ip_address" ]; 
   then
      #
      #Change ip address and update bind serial
      #
      echo $ip_address > useip

      #Get the serial line to replace in sed
       replace_serial=`cat db.bind | grep Serial`
       echo Replace Serial: $replace_serial

      #Generate new serial
       new_serial=`date +"%m%d%Y"`

      #Add Serial prefix to end of Serial
       new_serial="$new_serial ; Serial"
       echo New Serial: $new_serial

      #Replace the serial with the updated Serial number
       sed -i "/$replace_serial/ c\      $new_serial" db.bind

      #Replace the old ip address with the new ip address
       sed -i "/$old_ip/ c\be-rad.duckdns.org. IN A $ip_address " db.bind

      #Restart command for solaris/Open Indaina
       svcadm restart svc:/network/dns/server:default

     #Check to see if its online
       service_check=`svcs -xv  /network/dns/server:default`

      #
      #LOG features
      #

       echo ON [`date | cut -b 1-23`] >> $log
       echo The following has been changed:
       echo Old IP: $useip >> $log
       echo New IP: $ip_address >> $log
       echo "Service Check:

$service_check
" >> $log

   else  

     #
     #LOG features
     #

     echo ON [`date | cut -b 1-23`] >> $log
     echo The ip address did not change >> $log
     echo "
" >> $log

fi

Comments

  • Neat, I can see this being used for a home server. I was wanting to make a server, and this is much better than GoDaddy.
  • robobox wrote:
    Neat, I can see this being used for a home server. I was wanting to make a server, and this is much better than GoDaddy.

    Yeah, I used to be part of blue host, but later on I got more interested in creating my own server. If your looking for something that will track your ip address to an actual domain, I would suggest DuckDNS, as they provide a free sub-domian. Although there are others as best described in this list here.

    Just out of curiosity, what OS do you plan on using as a server?
Sign In or Register to comment.