system-helpers/usr/local/bin/dynv6

130 lines
3.3 KiB
Text
Raw Normal View History

2019-09-08 12:45:57 +00:00
#!/usr/bin/env bash
set -e;
usage() {
USAGE=$(cat <<EOF
Usage: dynv6 [CONFIG_FILE (absolute path)]
If no CONFIG_FILE is given, HOME/.dynv6.conf is used. This fallback option
has to exist or the script will exit.
Configuration can be done in any file and any pre-defined variable can be overwritten.
The following are at least required for the script to work:
- DYNV6_MAIL_ENABLED= // if notify via mail
- DYNV6_MAIL_ADDRESS="" // (optional) mail.rc has to be configured
- DYNV6_TOKEN="" // the dynv6 token
- DYNV6_HOSTNAME="" // the dynv6 hostname
- DYNV6_IPV4_ENABLED=true // if should update ipv4
- DYNV6_IPV6_ENABLED=true // if should update ipv6
2019-09-08 12:45:57 +00:00
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
)
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() {
if [ -z ${DYNV6_MAIL_ENABLED} ]; then
DYNV6_MAIL_ENABLED=false;
fi
if [ -z ${DYNV6_MAIL_ADDRESS} ]; then
DYNV6_MAIL_ADDRESS="";
fi
if [ -z ${DYNV6_TOKEN} ]; then
DYNV6_TOKEN="";
fi
if [ -z ${DYNV6_HOSTNAME} ]; then
DYNV6_HOSTNAME="";
fi
if [ -z ${DYNV6_IPV4_ENABLED} ]; then
DYNV6_IPV4_ENABLED=true;
fi
if [ -z ${DYNV6_IPV6_ENABLED} ]; then
DYNV6_IPV6_ENABLED=true;
fi
2019-09-08 12:45:57 +00:00
}
2019-09-08 12:45:57 +00:00
check_required() {
if [ -z ${DYNV6_TOKEN} ]; then
2019-09-08 12:45:57 +00:00
echo "DYNV6_TOKEN is required"
2019-09-27 20:17:21 +00:00
exit 1;
2019-09-08 12:45:57 +00:00
fi
if [ -z ${DYNV6_HOSTNAME} ]; then
2019-09-08 12:45:57 +00:00
echo "DYNV6_HOSTNAME is required"
2019-09-27 20:17:21 +00:00
exit 1;
2019-09-08 12:45:57 +00:00
fi
2019-09-27 20:17:21 +00:00
}
2019-09-08 12:45:57 +00:00
# check requirements
CONFIG_FILE="$1";
FALLBACK_CONFIG_FILE="${HOME}/.dynv6.conf";
source_config "${CONFIG_FILE}" "${FALLBACK_CONFIG_FILE}";
apply_defaults;
check_required;
2019-09-08 12:45:57 +00:00
# DO NOT CHANGE
token=${DYNV6_TOKEN}
hostname=${DYNV6_HOSTNAME}
2019-09-27 20:17:21 +00:00
echo "Checking ${hostname}"
2019-09-08 12:45:57 +00:00
2019-09-27 20:17:21 +00:00
# check via service
# ipv6
if [ "${DYNV6_IPV6_ENABLED}" = "true" ]; then
echo "Testing IPv6";
ipv6Res=$(curl -fsS "http://dynv6.com/api/update?hostname=${hostname}&token=${token}")
ipv6Changed=true;
2019-09-08 12:45:57 +00:00
if [ "${ipv6Res}" = "addresses unchanged" ]; then
ipv6Changed=false;
fi
if [ "${ipv6Changed}" = "true" ]; then
echo "IPv6 changed"
2019-09-08 12:45:57 +00:00
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
2019-09-27 20:17:21 +00:00
fi
2019-09-08 12:45:57 +00:00
fi
2019-09-27 20:17:21 +00:00
# ipv4
if [ "${DYNV6_IPV4_ENABLED}" = "true" ]; then
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
if [ "${ipv4Changed}" = "true" ]; then
echo "IPv4 changed"
2019-09-08 12:45:57 +00:00
if [ "${DYNV6_MAIL_ENABLED}" = "true" ]; then
HOSTMACHINE=$(hostname)
echo -e "IPv4 for ${hostname} on host ${HOSTMACHINE} changed" | mailx -s "[dynv6 IPv4 ${HOSTMACHINE}] IPv4 changed for ${hostname}" "${DYNV6_MAIL_ADDRESS}"
fi
2019-09-27 20:17:21 +00:00
fi
fi