Update nsupdate script to be more discerning about IP addresses

This commit is contained in:
Timothy Allen 2025-03-05 10:32:14 +02:00
parent 33107098f3
commit d811e15f36

View File

@ -5,15 +5,34 @@ server=ns1.example.com
zone=dyn.example.com zone=dyn.example.com
host=desktop host=desktop
secret="" secret=""
iface=eth0 iface=$( ip route get $( dig +short ${server} | grep [0-9] ) | grep -Po '(?<=(dev ))(\S+)' )
# TODO trigger not via cron, but using ip monitor in a while/sleep loop:
#ip monitor address dev ${iface} | while read event; do
# case "$event" in
# 'Deleted default'*)
# ...
# ;;
# 'local '*)
# ...
# ;;
# ...)
# ...
# ;;
# ...
# esac
#done
# This has to be retrieved externally
new_ip4=$( wget -4 -q -O - https://treehouse.org.za/ip ) new_ip4=$( wget -4 -q -O - https://treehouse.org.za/ip )
new_ip6=$( ip -6 addr show ${iface} | grep global | grep -oP '(?<=inet6\s)[\da-f:]+' ) # If the iface call fails, we will get several interfaces, so pipe through head to get the first (default) interface
new_ip6=$( ip -6 addr show ${iface} scope global | grep -vE '(mngtmpaddr|deprecated)' | grep -oP '(?<=inet6\s)[\da-f:]+' | grep -vE '^fc' | head -1 )
cur_ip4=$( nslookup -type=a ${host}.${zone} | grep -oP '(?<=^Address:\ )\d+(\.\d+){3}$' ) cur_ip4=$( nslookup -type=a ${host}.${zone} | grep -oP '(?<=^Address:\ )\d+(\.\d+){3}$' | head -1 )
cur_ip6=$( nslookup -type=aaaa ${host}.${zone} | grep -oP '(?<=^Address:\ )[\da-f:]+$' ) cur_ip6=$( nslookup -type=aaaa ${host}.${zone} | grep -oP '(?<=^Address:\ )[\da-f:]+$' | head -1 )
if [ -z ${new_ip4} ] && [ -z ${new_ip6} ]; then #if [ -z ${new_ip4} ] && [ -z ${new_ip6} ]; then
if [ -z ${new_ip4} ]; then
exit; exit;
fi fi
@ -46,12 +65,12 @@ else
fi fi
ipv6_line="" ipv6_line=""
if [ -n "${new_ip6}" ]; then if [ -n "${new_ip6}" ] && [ "${cur_ip6}" != "${new_ip6}" ]; then
ipv6_line="update add ${host}.${zone}. 60 AAAA ${new_ip6}" ipv6_line="update add ${host}.${zone}. 60 AAAA ${new_ip6}"
fi fi
update=$( cat <<-EOF update=$( cat <<-EOF
server ${server} server ${server}.
zone ${zone}. zone ${zone}.
key ${host}.${zone} ${secret} key ${host}.${zone} ${secret}
update delete ${host}.${zone}. update delete ${host}.${zone}.