Difference between revisions of "Scripts/dynsupdate"

From fakedWiki
Jump to: navigation, search
m (1 revision imported)
m (1 revision imported)
 
(No difference)

Latest revision as of 21:10, 26 August 2016

This script will update the A records for zones you host on your local Bind9 nameserver to your (external) dynamic IP. Make sure that nsupdate works with a key by testing it manually.

I'm running this script after every reconnect of my VDSL link through ppp's ip-up scripts by putting it here:

/etc/ppp/ip-up.d/dynsupdate

Notice: The here-document lines (between EndOfCommands) have to be indented with tabs, or not indented at all - spaces will not work.

#!/bin/bash

RECORDS=("example.com" "ns1.example.com" "ns2.example.com" "example.org" "example.net")
SERVER="192.168.0.XXX"
KEY="/etc/bind/Knsupdate.key"
TTL="300"

IP=`lynx -dump http://checkip.dyndns.org | awk '{print $4}'`

if [ -e "/tmp/nsup.IP" ]; then
  read OLD_IP < "/tmp/nsup.IP"
else
  OLD_IP="0.0.0.0"
fi

if [ "$IP" != "$OLD_IP" ]; then
  echo "IP address changed from $OLD_IP to $IP"
  for RECORD in ${RECORDS[@]}; do
    COMMANDS="$COMMANDS
        update delete ${RECORD}. A
        update add ${RECORD}. ${TTL} A ${IP}
        "
    echo "updating record: $RECORD"
  done
  nsupdate -v -k ${KEY} <<-EndOfCommands
        server ${SERVER}
        ${COMMANDS}
        send
        EndOfCommands
  echo "$IP" > /tmp/nsup.IP
  echo "IP updates done"
fi