#!/usr/bin/env bash set -e; usage() { USAGE=$(cat < /dev/null || { echo "Requiring 'hostname' but it's not installed"; exit 1; } type ip &> /dev/null || { echo "Requiring 'ip' but it's not installed"; exit 1; } type grep &> /dev/null || { echo "Requiring 'grep' but it's not installed"; exit 1; } type curl &> /dev/null || { echo "Requiring 'curl' but it's not installed"; exit 1; } } # check requirements FALLBACK_CONFIG_FILE="${HOME}/.dynv6.conf"; FALLBACK_CONFIG_GLOBAL_FILE="/etc/dynv6.conf"; source_config "${FALLBACK_CONFIG_FILE}" "${FALLBACK_CONFIG_GLOBAL_FILE}"; apply_defaults; check_required; # DO NOT CHANGE token=${DYNV6_TOKEN} hostname=${DYNV6_HOSTNAME} echo "Checking ${hostname}" # check via service # ipv6 if [ "${DYNV6_IPV6_ENABLED}" = "true" ]; then echo "Testing IPv6"; ipv6Address=$(LC_ALL=C /usr/bin/ip -c=never -6 -o a s dev ${DYNV6_IPV6_INTERFACE} | grep --color=never -Po '(?<=inet6 )(2.*?)(?=\/64 scope global dynamic mngtmpaddr)') ipv6AddressLocalFile="/tmp/dynv6_ip6"; if [ ! -f "${ipv6AddressLocalFile}" ]; then touch "${ipv6AddressLocalFile}"; fi if grep --color=never -Fxq "${ipv6Address}" "${ipv6AddressLocalFile}"; then echo "IPv6 is still ${ipv6Address}"; else echo "IPv6 changed" echo "${ipv6Address}" > "${ipv6AddressLocalFile}"; ipv6Res=$(curl -s -v "https://ipv6.dynv6.com/api/update?hostname=${hostname}&ipv6=${ipv6Address}&token=${token}") ipv6Changed=true; if [ "${ipv6Res}" = "addresses unchanged" ]; then ipv6Changed=false; fi if [ "${ipv6Changed}" = "true" ]; then HOSTMACHINE=$(hostname) SUBJECT="[dynv6 IPv6 ${HOSTMACHINE}] IPv6 changed for ${hostname}" MESSAGE="IPv6 for ${hostname} on host ${HOSTMACHINE} changed to ${ipv6Address}" notifier "${SUBJECT}" "${MESSAGE}" fi fi fi # ipv4 if [ "${DYNV6_IPV4_ENABLED}" = "true" ]; then echo "Testing IPv4"; ipv4Address=$(curl -s "https://api.ipify.org/?format=text") ipv4Res=$(curl -fsS "https://ipv4.dynv6.com/api/update?hostname=${hostname}&ipv4=${ipv4Address}&token=${token}") ipv4Changed=true; if [ "$ipv4Res" = "addresses unchanged" ]; then ipv4Changed=false; fi if [ "${ipv4Changed}" = "true" ]; then echo "IPv4 changed" HOSTMACHINE=$(hostname) SUBJECT="[dynv6 IPv4 ${HOSTMACHINE}] IPv4 changed for ${hostname}" MESSAGE="IPv4 for ${hostname} on host ${HOSTMACHINE} changed to ${ipv4Address}" notifier "${SUBJECT}" "${MESSAGE}" fi fi