Fix dynv6 and just call every time

This commit is contained in:
Alexander Schäferdiek 2019-09-27 22:17:21 +02:00
parent f9bfc4e106
commit dfea4a8bb9
2 changed files with 36 additions and 56 deletions

View file

@ -16,8 +16,6 @@ The following are at least required for the script to work:
- DYNV6_MAIL_ADDRESS="" // (optional) mail.rc has to be configured - DYNV6_MAIL_ADDRESS="" // (optional) mail.rc has to be configured
- DYNV6_TOKEN="" // the dynv6 token - DYNV6_TOKEN="" // the dynv6 token
- DYNV6_HOSTNAME="" // the dynv6 hostname - DYNV6_HOSTNAME="" // the dynv6 hostname
- DYNV6_DEVICE="" // the (optional) dynv6 device
- DYNV6_NETMASK="" // the (optional) netmask
You can copy this script to '/usr/local/bin' and use create a custom CONFIG_FILE as user. Examples can be found in '/usr/share/doc/<scriptname>'. You can copy this script to '/usr/local/bin' and use create a custom CONFIG_FILE as user. Examples can be found in '/usr/share/doc/<scriptname>'.
EOF EOF
@ -51,77 +49,61 @@ apply_defaults() {
[[ -z ${DYNV6_MAIL_ADDRESS} ]] && DYNV6_MAIL_ADDRESS=""; [[ -z ${DYNV6_MAIL_ADDRESS} ]] && DYNV6_MAIL_ADDRESS="";
[[ -z ${DYNV6_TOKEN} ]] && DYNV6_TOKEN=""; [[ -z ${DYNV6_TOKEN} ]] && DYNV6_TOKEN="";
[[ -z ${DYNV6_HOSTNAME} ]] && DYNV6_HOSTNAME=""; [[ -z ${DYNV6_HOSTNAME} ]] && DYNV6_HOSTNAME="";
[[ -z ${DYNV6_DEVICE} ]] && DYNV6_DEVICE="";
[[ -z ${DYNV6_NETMASK} ]] && DYNV6_NETMASK="";
} }
check_required() { check_required() {
if [[ -z ${DYNV6_TOKEN} ]]; then if [[ -z ${DYNV6_TOKEN} ]]; then
echo "DYNV6_TOKEN is required" echo "DYNV6_TOKEN is required"
exit 1; exit 1;
fi fi
if [[ -z ${DYNV6_HOSTNAME} ]]; then if [[ -z ${DYNV6_HOSTNAME} ]]; then
echo "DYNV6_HOSTNAME is required" echo "DYNV6_HOSTNAME is required"
exit 1; exit 1;
fi 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 # check requirements
CONFIG_FILE="$1"; CONFIG_FILE="$1";
FALLBACK_CONFIG_FILE="${HOME}/.dynv6.conf"; FALLBACK_CONFIG_FILE="${HOME}/.dynv6.conf";
source_config "${CONFIG_FILE}" "${FALLBACK_CONFIG_FILE}"; source_config "${CONFIG_FILE}" "${FALLBACK_CONFIG_FILE}";
apply_defaults; #apply_defaults;
check_required; #check_required;
# DO NOT CHANGE # DO NOT CHANGE
token=${DYNV6_TOKEN} token=${DYNV6_TOKEN}
hostname=${DYNV6_HOSTNAME} hostname=${DYNV6_HOSTNAME}
device=${DYNV6_DEVICE}
netmask=${DYNV6_NETMASK}
file=$HOME/.dynv6.addr6 echo "Checking ${hostname}"
[ -e $file ] && old=`cat $file`
if [ -z "$netmask" ]; then # check via service
netmask=128 # ipv6
echo "Testing IPv6";
ipv6Res=$(curl -fsS "http://dynv6.com/api/update?hostname=${hostname}&token=${token}")
ipv6Changed=true;
if [ "$ipv6Res" = "addresses unchanged" ]; then
ipv6Changed=false;
fi
if [ "$ipv6Changed" = "true" ]; then
echo "IPv6 changed"
if [ "${DYNV6_MAIL_ENABLED}" = true ]; then
HOSTMACHINE=$(hostname)
echo -e "IPv6 for ${hostname} on host ${HOSTMACHINE} changed" | mailx -s "[dynv6 IPv6 ${HOSTMACHINE}] IPv6 changed for ${hostname}" "${DYNV6_MAIL_ADDRESS}"
fi
fi fi
if [ -n "$device" ]; then # ipv4
device="dev $device" echo "Testing IPv4";
ipv4Res=$(curl -fsS "http://ipv4.dynv6.com/api/update?hostname=${hostname}&ipv4=auto&token=${token}")
ipv4Changed=true;
if [ "$ipv4Res" = "addresses unchanged" ]; then
ipv4Changed=false;
fi 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 [ "$ipv4Changed" = "true" ]; then
echo "IPv4 changed"
if [ -z "$address" ]; then if [ "${DYNV6_MAIL_ENABLED}" = true ]; then
echo "no IPv6 address found" HOSTMACHINE=$(hostname)
exit 1 echo -e "IPv4 for ${hostname} on host ${HOSTMACHINE} changed" | mailx -s "[dynv6 IPv4 ${HOSTMACHINE}] IPv4 changed for ${hostname}" "${DYNV6_MAIL_ADDRESS}"
fi fi
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

View file

@ -2,6 +2,4 @@
DYNV6_MAIL_ENABLED= // if notify via mail DYNV6_MAIL_ENABLED= // if notify via mail
DYNV6_MAIL_ADDRESS="" // (optional) mail.rc has to be configured DYNV6_MAIL_ADDRESS="" // (optional) mail.rc has to be configured
DYNV6_TOKEN="" // the dynv6 token DYNV6_TOKEN="" // the dynv6 token
DYNV6_HOSTNAME="" // the dynv6 hostname DYNV6_HOSTNAME="" // the dynv6 hostname
DYNV6_DEVICE="" // the (optional) dynv6 device
DYNV6_NETMASK="" // the (optional) netmask