OpenBSD Scripts - dynamic updates mit bind9


The task At home the gateway to the internet receives a dynamic address every few weeks. So that the server, which is located at a provider, always knows this clients IP address, a mechanism to update is used here. The prerequisites are a bind9 nameserver (running any UNIX OS) and the client, which should run on UNIX as well, so that the korn shell script is working ok.

The nameserver must be configured to allow updates. Some useful document ist here

Some caveats here:
  The /var/named directory must be writable by the user that named is running. OpenBSD uses its own user named in a chroot environment.
  The key files you have generated (see link), you copy over to the client where the script below can access them.


On the client put this script into place. Let it automatically start on reboot (OpenBSD from /etc/rc.local)

# start named updater
( /usr/local/bin/named_updater.ksh & )



The Script on the client machine (anonymized):

nocino # cat /usr/local/bin/named_updater.ksh
#!/bin/ksh
#
# update the bind9 named on ns2.example.com
#
# by cmb 23/08/06
#

# set interace name to get initial ip address
if="rl1"

# set the hostname for extracting the dhcp info from the correct maschine from daemon log
servername="nocino"

# set the name server name ==> TODO name oder address??
nameserver="212.212.212.212"

# set the actual ip address of IF ==> when script is started, update name server once !
#ipaddress=`ifconfig $if | fgrep "inet " | awk '{print $2}'`
ipaddress="123.123.123.321"


while true
do
   dynamic_address=`tail -100 /var/log/daemon | grep "bound to " | fgrep $servername | tail -1 | awk '{print $8}'`

   if [ $dynamic_address"X" = "X" ] ; then
     # no dhcp info found in log - sleep some seconds
     sleep 5
   else

     if [ $ipaddress != $dynamic_address ] ; then
     # Address has changed

       logger "dhcp address changed - trying to update on name server"

       # send the new ip address to the name server

       nsupdate -k /var/named/etc/Kupdater1.+157+56271.private << EOF
server $nameserver 53
update delete nocino.example.com A
update add nocino.example.com 2700 A $dynamic_address
send
EOF
      if [ $? -eq 0 ] ; then
        logger "dhcp address change succeeded"
      else
        logger "dhcp address change failed"
      fi

      sleep 5
      # set the new ip address in the variable
      ipaddress=`ifconfig $if | fgrep "inet " | awk '{print $2}'`
    fi
  fi
  sleep 5
done