diff --git a/update_ip.sh b/update_ip.sh index e58b495..70c25f4 100755 --- a/update_ip.sh +++ b/update_ip.sh @@ -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 <> "${HOME}/${cache}" + echo "${new_ip6}" >> "${HOME}/${cache}" +else + echo "Request was:" + echo "${update}" +fi