57 lines
1.8 KiB
Bash
Executable File
57 lines
1.8 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
|
|
run_directory=$(dirname $(readlink -f "$0"))
|
|
source "$run_directory/variables.conf"
|
|
|
|
KEY="$run_directory/dns.key"
|
|
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
|
|
# 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
|
|
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
|