system-helpers/usr/local/bin/dynv6

145 lines
3.7 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
Updates dynv6 (an external service) and notifies about changes of IP addresses. Please see man dynv6.
2019-09-08 12:45:57 +00:00
EOF
)
echo "$USAGE";
}
# check for config file
2022-07-10 16:29:44 +00:00
apply_config() {
2019-09-08 12:45:57 +00:00
local config=$1;
if [[ ! -f "$config" ]]; then
2022-07-10 16:29:44 +00:00
echo "No config file specified";
2019-09-08 12:45:57 +00:00
echo "";
usage;
exit 1;
fi
set -a;
# shellcheck disable=SC1090
2019-09-08 12:45:57 +00:00
source "$config";
set +a;
}
2022-07-10 16:29:44 +00:00
source_config() {
local configFallback=$1;
local configGlobalFallback=$2;
2022-07-10 16:29:44 +00:00
if [[ -f "$configFallback" ]]; then
apply_config "$configFallback";
2022-07-10 16:33:00 +00:00
return;
2022-07-10 16:29:44 +00:00
fi
if [[ -f "$configGlobalFallback" ]]; then
apply_config "$configGlobalFallback";
2022-07-10 16:33:00 +00:00
return;
2022-07-10 16:29:44 +00:00
fi
}
2019-09-08 12:45:57 +00:00
apply_defaults() {
2022-07-10 16:35:26 +00:00
if [ -z "${DYNV6_TOKEN}" ]; then
DYNV6_TOKEN="";
fi
2022-07-10 16:35:26 +00:00
if [ -z "${DYNV6_HOSTNAME}" ]; then
DYNV6_HOSTNAME="";
fi
2022-07-10 16:35:26 +00:00
if [ -z "${DYNV6_IPV4_ENABLED}" ]; then
DYNV6_IPV4_ENABLED=true;
fi
2022-07-10 16:35:26 +00:00
if [ -z "${DYNV6_IPV6_ENABLED}" ]; then
DYNV6_IPV6_ENABLED=true;
fi
2022-07-10 16:35:26 +00:00
if [ -z "${DYNV6_IPV6_INTERFACE}" ]; then
DYNV6_IPV6_INTERFACE="eth0";
fi
2019-09-08 12:45:57 +00:00
}
2019-09-08 12:45:57 +00:00
check_required() {
2022-07-10 16:35:26 +00:00
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
2022-07-10 16:35:26 +00:00
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
type hostname &> /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; }
2019-09-27 20:17:21 +00:00
}
2019-09-08 12:45:57 +00:00
# check requirements
2022-07-10 16:36:16 +00:00
FALLBACK_CONFIG_FILE="${HOME}/.dynv6.conf";
2022-07-10 16:29:44 +00:00
FALLBACK_CONFIG_GLOBAL_FILE="/etc/dynv6.conf";
source_config "${FALLBACK_CONFIG_FILE}" "${FALLBACK_CONFIG_GLOBAL_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";
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)')
2020-06-18 16:40:58 +00:00
ipv6AddressLocalFile="/tmp/dynv6_ip6";
2019-09-08 12:45:57 +00:00
2020-06-18 16:40:58 +00:00
if [ ! -f "${ipv6AddressLocalFile}" ]; then
touch "${ipv6AddressLocalFile}";
fi
2019-09-08 12:45:57 +00:00
if grep --color=never -Fxq "${ipv6Address}" "${ipv6AddressLocalFile}"; then
2020-06-18 16:40:58 +00:00
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}"
2020-06-18 16:40:58 +00:00
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";
2021-02-11 17:09:52 +00:00
ipv4Address=$(curl -s "https://api.ipify.org/?format=text")
2020-03-21 20:14:27 +00:00
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"
2019-09-08 12:45:57 +00:00
HOSTMACHINE=$(hostname)
SUBJECT="[dynv6 IPv4 ${HOSTMACHINE}] IPv4 changed for ${hostname}"
MESSAGE="IPv4 for ${hostname} on host ${HOSTMACHINE} changed to ${ipv4Address}"
notifier "${SUBJECT}" "${MESSAGE}"
2019-09-27 20:17:21 +00:00
fi
2020-03-21 20:14:27 +00:00
fi