#!/usr/bin/env bash set -e; usage() { USAGE=$(cat <'. EOF ) echo "$USAGE"; } # check for config file source_config() { local config=$1; local configFallback=$2; if [[ ! -f "$config" ]]; then if [[ ! -f "$configFallback" ]]; then echo "No config file specified and could not find default in '$configFallback'!"; echo ""; usage; exit 1; else config=$configFallback; fi fi set -a; source "$config"; set +a; } apply_defaults() { [[ -z ${DYNV6_MAIL_ENABLED} ]] && DYNV6_MAIL_ENABLED=false; } check_required() { if [[ -z ${DYNV6_TOKEN} ]]; then echo "DYNV6_TOKEN is required" exit 1; fi if [[ -z ${DYNV6_HOSTNAME} ]]; then echo "DYNV6_HOSTNAME is required" exit 1; fi if [ -e /usr/bin/curl ]; then bin="curl -fsS" elif [ -e /usr/bin/wget ]; then bin="wget -O-" else echo "neither curl nor wget found" exit 1 fi } # check requirements CONFIG_FILE="$1"; FALLBACK_CONFIG_FILE="${HOME}/.dynv6.conf"; source_config "${CONFIG_FILE}" "${FALLBACK_CONFIG_FILE}"; apply_defaults; check_required; # DO NOT CHANGE token=${DYNV6_TOKEN} hostname=${DYNV6_HOSTNAME} device=${DYNV6_DEVICE} netmask=${DYNV6_NETMASK} file=$HOME/.dynv6.addr6 [ -e $file ] && old=`cat $file` if [ -z "$netmask" ]; then netmask=128 fi if [ -n "$device" ]; then device="dev $device" fi address=$(ip -6 addr list scope global $device | grep -v " fd" | sed -n 's/.*inet6 \([0-9a-f:]\+\).*/\1/p' | head -n 1) if [ -z "$address" ]; then echo "no IPv6 address found" exit 1 fi # address with netmask current=$address/$netmask if [ "$old" = "$current" ]; then echo "IPv6 address unchanged" exit fi # send addresses to dynv6 $bin "http://dynv6.com/api/update?hostname=$hostname&ipv6=$current&token=$token" $bin "http://ipv4.dynv6.com/api/update?hostname=$hostname&ipv4=auto&token=$token" # save current address echo $current > $file # sent mail that IP changed if [ "${DYNV6_MAIL_ENABLED}" = true ]; then HOSTMACHINE=$(hostname) echo -e "IP for ${hostname} changed from '${old}' to '${address}' on host ${HOSTMACHINE}" | mailx -s "[dynv6 ${HOSTMACHINE}] IP changed for ${hostname}" "${DYNV6_MAIL_ADDRESS}" fi