2015-06-15 00:06:23 +00:00
|
|
|
#!/bin/sh
|
|
|
|
|
|
|
|
cache=.update_ip.cache
|
|
|
|
server=ns1.example.com
|
|
|
|
zone=dyn.example.com
|
|
|
|
host=desktop
|
|
|
|
secret=""
|
2021-09-17 07:58:18 +00:00
|
|
|
iface=eth0
|
2015-06-15 00:06:23 +00:00
|
|
|
|
2021-08-10 19:00:08 +00:00
|
|
|
new_ip4=$( wget -4 -q -O - https://treehouse.org.za/ip )
|
2021-09-17 07:58:18 +00:00
|
|
|
new_ip6=$( ip -6 addr show ${iface} | grep global | grep -oP '(?<=inet6\s)[\da-f:]+' )
|
2015-06-15 00:06:23 +00:00
|
|
|
|
2021-08-10 19:00:08 +00:00
|
|
|
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
|
2015-06-15 00:06:23 +00:00
|
|
|
|
|
|
|
if [ -f "${HOME}/${cache}" ]; then
|
2021-08-10 19:00:08 +00:00
|
|
|
old_ip4=$( cat "${HOME}/${cache}" | grep "\." )
|
|
|
|
old_ip6=$( cat "${HOME}/${cache}" | grep "\:" )
|
2015-06-15 00:06:23 +00:00
|
|
|
fi
|
|
|
|
|
2021-08-10 19:00:08 +00:00
|
|
|
if [ "${cur_ip4}" = "${new_ip4}" ] && [ "${new_ip4}" = "${old_ip4}" ] &&
|
|
|
|
[ "${cur_ip6}" = "${new_ip6}" ] && [ "${new_ip6}" = "${old_ip6}" ]; then
|
|
|
|
exit;
|
2015-06-15 00:06:23 +00:00
|
|
|
else
|
2021-08-10 19:00:08 +00:00
|
|
|
rm -f "${HOME}/${cache}"
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ -f "${HOME}/${cache}" ]; then
|
|
|
|
old_ip4=$( cat "${HOME}/${cache}" | grep "\." )
|
|
|
|
old_ip6=$( cat "${HOME}/${cache}" | grep "\:" )
|
2015-06-15 00:06:23 +00:00
|
|
|
fi
|
|
|
|
|
2021-08-10 19:00:08 +00:00
|
|
|
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
|
|
|
|
|
2021-09-17 07:58:18 +00:00
|
|
|
ipv6_line=""
|
|
|
|
if [ -n "${new_ip6}" ]; then
|
|
|
|
ipv6_line="update add ${host}.${zone}. 60 AAAA ${new_ip6}"
|
|
|
|
fi
|
|
|
|
|
2021-08-10 19:00:08 +00:00
|
|
|
update=$( cat <<-EOF
|
|
|
|
server ${server}
|
|
|
|
zone ${zone}.
|
|
|
|
key ${host}.${zone} ${secret}
|
|
|
|
update delete ${host}.${zone}.
|
|
|
|
update add ${host}.${zone}. 60 A ${new_ip4}
|
2021-09-17 07:58:18 +00:00
|
|
|
${ipv6_line}
|
2021-08-10 19:00:08 +00:00
|
|
|
update add ${host}.${zone}. 60 TXT "Updated on $( date )"
|
|
|
|
send
|
2015-06-15 00:06:23 +00:00
|
|
|
EOF
|
2021-08-10 19:00:08 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
if echo "${update}" | nsupdate ; then
|
|
|
|
echo "${new_ip4}" >> "${HOME}/${cache}"
|
|
|
|
echo "${new_ip6}" >> "${HOME}/${cache}"
|
|
|
|
else
|
|
|
|
echo "Request was:"
|
|
|
|
echo "${update}"
|
|
|
|
fi
|