50 lines
1.3 KiB
Bash
Executable File
50 lines
1.3 KiB
Bash
Executable File
#!/bin/bash
|
|
|
|
source variables.conf
|
|
|
|
KEY="$(pwd)/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
|
|
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
|