Update for IPv6 addresses...

... along with various debugging cleanups.
This commit is contained in:
Timothy Allen 2021-08-10 21:00:08 +02:00
parent 36989f346b
commit 783baf723f
1 changed files with 48 additions and 20 deletions

View File

@ -6,32 +6,60 @@ zone=dyn.example.com
host=desktop
secret=""
ip=$(wget -4 -q -O - http://treehouse.org.za/ip)
#set_ip=$(dig +short -t a ${host}.${zone})
set_ip=$(nslookup -type=a ${host}.${zone} | awk '/^Address: / { print $2 }')
new_ip4=$( wget -4 -q -O - https://treehouse.org.za/ip )
new_ip6=$( ip -6 addr show eth0 | grep global | grep -oP '(?<=inet6\s)[\da-f:]+' )
if [ -z ${ip} ] || [ -z ${cache} ]; then
exit;
fi
cur_ip4=$( nslookup -type=a ${host}.${zone} | grep -oP '(?<=^Address:\ )\d+(\.\d+){3}$' )
cur_ip6=$( nslookup -type=aaaa ${host}.${zone} | grep -oP '(?<=^Address:\ )[\da-f:]+$' )
if [ -z ${new_ip4} ] && [ -z ${new_ip6} ]; then
exit;
fi
if [ -z ${cache} ]; then
exit;
fi
if [ -f "${HOME}/${cache}" ]; then
old_ip=$(cat "${HOME}/${cache}")
old_ip4=$( cat "${HOME}/${cache}" | grep "\." )
old_ip6=$( cat "${HOME}/${cache}" | grep "\:" )
fi
if [ "${set_ip}" = "${ip}" ] && [ "${ip}" = "${old_ip}" ]; then
exit;
if [ "${cur_ip4}" = "${new_ip4}" ] && [ "${new_ip4}" = "${old_ip4}" ] &&
[ "${cur_ip6}" = "${new_ip6}" ] && [ "${new_ip6}" = "${old_ip6}" ]; then
exit;
else
rm -f "${HOME}/${cache}"
echo "${ip}" > "${HOME}/${cache}"
rm -f "${HOME}/${cache}"
fi
cat <<EOF | nsupdate
server ${server}
zone ${zone}.
key ${host}.${zone} ${secret}
update delete ${host}.${zone}. A
update delete ${host}.${zone}. TXT
update add ${host}.${zone}. 60 A $ip
update add ${host}.${zone}. 60 TXT "Updated on $(date)"
send
if [ -f "${HOME}/${cache}" ]; then
old_ip4=$( cat "${HOME}/${cache}" | grep "\." )
old_ip6=$( cat "${HOME}/${cache}" | grep "\:" )
fi
if [ "${cur_ip4}" = "${new_ip4}" ] && [ "${new_ip4}" = "${old_ip4}" ] &&
[ "${cur_ip6}" = "${new_ip6}" ] && [ "${new_ip6}" = "${old_ip6}" ]; then
exit;
else
rm -f "${HOME}/${cache}"
fi
update=$( cat <<-EOF
server ${server}
zone ${zone}.
key ${host}.${zone} ${secret}
update delete ${host}.${zone}.
update add ${host}.${zone}. 60 A ${new_ip4}
update add ${host}.${zone}. 60 AAAA ${new_ip6}
update add ${host}.${zone}. 60 TXT "Updated on $( date )"
send
EOF
)
if echo "${update}" | nsupdate ; then
echo "${new_ip4}" >> "${HOME}/${cache}"
echo "${new_ip6}" >> "${HOME}/${cache}"
else
echo "Request was:"
echo "${update}"
fi