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
# update the bind9 named on
# by cmb 23/08/06

# set interace name to get initial ip address

# set the hostname for extracting the dhcp info from the correct maschine from daemon log

# set the name server name ==> TODO name oder address??

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

while true
   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

     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 A
update add 2700 A $dynamic_address
      if [ $? -eq 0 ] ; then
        logger "dhcp address change succeeded"
        logger "dhcp address change failed"

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