2022-03-13 13:05:35 +01:00
#!/usr/bin/env bash
2022-02-11 20:24:15 +01:00
2022-09-22 18:51:37 +02:00
run_directory = $( dirname $( readlink -f " $0 " ) )
source " $run_directory /variables.conf "
2022-02-11 20:24:15 +01:00
2022-09-22 18:51:37 +02:00
KEY = " $run_directory /dns.key "
2022-02-11 20:24:15 +01:00
TTL = 604800
FQDN = $( hostname)
MYIP = $( ip route get 8.8.8.8 | grep -oP 'src \K[^ ]+' )
MYNUMBER = $( echo $MYIP | cut -d '.' -f 4) # last byte of IP
#MYIP=$(dig +short myip.opendns.com @resolver1.opendns.com)
# if $MYIP is not a valid IP, exit
if [ [ ! " $MYIP " = ~ ^( [ 1-9] ?[ 0-9] | 1[ 0-9] { 2} | 2[ 0-4] [ 0-9] | 25[ 0-5] ) \. ( [ 1-9] ?[ 0-9] | 1[ 0-9] { 2} | 2[ 0-4] [ 0-9] | 25[ 0-5] ) \. ( [ 1-9] ?[ 0-9] | 1[ 0-9] { 2} | 2[ 0-4] [ 0-9] | 25[ 0-5] ) \. ( [ 1-9] ?[ 0-9] | 1[ 0-9] { 2} | 2[ 0-4] [ 0-9] | 25[ 0-5] ) $ ] ] ; then
echo "Invalid IP, aborting"
exit 1
fi
# fetch current record with dig, and choose what to do
2022-02-15 18:01:25 +01:00
# TODO we should probably improve this choice: current implementation prevents hosts that do not have a record yet to register themselves; We'd need to differentiate cases:
# * correct ip, not the same -> OK, need to update
# * correct ip, the same -> OK, nothing to do
# * no result, name server was reachable -> OK, need to register
# * no result, name server was not reachable -> NOK, abort
# * result which is not a correct ip -> NOK, abort
2022-02-11 20:24:15 +01:00
current_ip_record = $( dig $FQDN +short)
if [ [ " $current_ip_record " = ~ ^( [ 1-9] ?[ 0-9] | 1[ 0-9] { 2} | 2[ 0-4] [ 0-9] | 25[ 0-5] ) \. ( [ 1-9] ?[ 0-9] | 1[ 0-9] { 2} | 2[ 0-4] [ 0-9] | 25[ 0-5] ) \. ( [ 1-9] ?[ 0-9] | 1[ 0-9] { 2} | 2[ 0-4] [ 0-9] | 25[ 0-5] ) \. ( [ 1-9] ?[ 0-9] | 1[ 0-9] { 2} | 2[ 0-4] [ 0-9] | 25[ 0-5] ) $ ] ] ; then
if [ [ ! " $MYIP " = " $current_ip_record " ] ] ; then
echo "Updating record"
else
echo "Record is already up to date, exiting."
exit 0
fi
else
echo " Server unreachable or no record for $FQDN , exiting. "
exit 0
fi
nsupdate -k $KEY -v << EOF
server $NS
zone $MAINZONE
update delete $FQDN . A
update add $FQDN . $TTL A $MYIP
send
EOF
nsupdate -k $KEY -v << EOF
server $NS
zone $REVERSEZONE
update delete $MYNUMBER .$REVERSEZONE PTR
update add $MYNUMBER .$REVERSEZONE $TTL PTR $FQDN .
send
EOF
exit 0