commit 0a87e9e81d4ff9a1c5cb32b71144a57125e8da08 parent 235ce245069f6057dae5be3ee08998ae421feca8 Author: sin <sin@2f30.org> Date: Mon, 23 Apr 2018 20:55:37 +0100 Add dyndns-update script (requires a zone file template) Diffstat:
A | dyndns-update | | | 81 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1 file changed, 81 insertions(+), 0 deletions(-)
diff --git a/dyndns-update b/dyndns-update @@ -0,0 +1,81 @@ +#!/bin/sh + +BASE=/home/dyndns + +LOCK_FILE=$BASE/dyndns.lock +HOSTS_FILE=$BASE/hosts +ZONE_FILE=$BASE/dyn.2f30.org +ZONE_TEMPLATE_FILE=$ZONE_FILE.template +SERIAL_FILE=$BASE/serial +SERIAL=$(cat $SERIAL_FILE) + +HOST=$1 +ADDR=$(echo $SSH_CLIENT | awk '{print $1}') + +if test -z $HOST; then + echo "missing hostname" 1>&2 + exit 1 +fi + +if test -z $ADDR; then + echo "missing address" 1>&2 + exit 1 +fi + +# only IPv4 addresses are supported +expr "$ADDR" : '[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*$' >/dev/null +if ! test $? -eq 0; then + echo "invalid address" 1>&2 + exit 1 +fi + +umask 077 + +( + flock -w 30 8 + + test -e $HOSTS_FILE || touch $HOSTS_FILE + test -e $SERIAL_FILE || echo 0 > $SERIAL_FILE + + # check if host needs updating or not + while read line; do + H=$(echo $line | awk '{print $1}') + A=$(echo $line | awk '{print $2}') + if test "$H" = "$HOST" -a "$A" = "$ADDR"; then + echo "$H.dyn.2f30.org is up to date" + exit 0 + fi + done < $HOSTS_FILE + + # update host entry in hosts file + FOUND=0 + while read line; do + H=$(echo $line | awk '{print $1}') + A=$(echo $line | awk '{print $2}') + if test "$H" = "$HOST"; then + echo $HOST $ADDR >> $HOSTS_FILE.tmp + FOUND=1 + else + echo $H $A >> $HOSTS_FILE.tmp + fi + done < $HOSTS_FILE + + # if host was not found in the hosts file, append it to the end of the file + if test $FOUND -eq 0; then + echo $HOST $ADDR >> $HOSTS_FILE.tmp + fi + + mv $HOSTS_FILE.tmp $HOSTS_FILE + + SERIAL=$((SERIAL+1)) + echo $SERIAL > $SERIAL_FILE + sed "s/%SERIAL%/$SERIAL/g" $ZONE_TEMPLATE_FILE > $ZONE_FILE + + while read line; do + H=$(echo $line | awk '{print $1}') + A=$(echo $line | awk '{print $2}') + echo "$H\tIN\tA\t$A" >> $ZONE_FILE + done < $HOSTS_FILE + + echo "$HOST.dyn.2f30.org updated to $ADDR" +) 8>$LOCK_FILE