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

View File

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