From df8fcbcb881d31123406e7199266c602958076e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexander=20Sch=C3=A4ferdiek?= Date: Sun, 8 Sep 2019 14:45:57 +0200 Subject: [PATCH] Add dynv6 as user service --- etc/systemd/user/dynv6.service | 10 ++ etc/systemd/user/dynv6.timer | 9 ++ usr/local/bin/dynv6 | 121 ++++++++++++++++++++++++ usr/share/doc/dynv6/.dynv6.conf.example | 7 ++ 4 files changed, 147 insertions(+) create mode 100644 etc/systemd/user/dynv6.service create mode 100644 etc/systemd/user/dynv6.timer create mode 100755 usr/local/bin/dynv6 create mode 100644 usr/share/doc/dynv6/.dynv6.conf.example diff --git a/etc/systemd/user/dynv6.service b/etc/systemd/user/dynv6.service new file mode 100644 index 0000000..28a9bf1 --- /dev/null +++ b/etc/systemd/user/dynv6.service @@ -0,0 +1,10 @@ +[Unit] +Description=Run dynv6 +#OnFailure=systemd_failure_notify@%n.service + +[Service] +Type=oneshot +ExecStart=dynv6 + +[Install] +WantedBy=default.target diff --git a/etc/systemd/user/dynv6.timer b/etc/systemd/user/dynv6.timer new file mode 100644 index 0000000..4ba6be1 --- /dev/null +++ b/etc/systemd/user/dynv6.timer @@ -0,0 +1,9 @@ +[Unit] +Description=Run dynv6 + +[Timer] +OnCalendar=hourly +Persistent=true + +[Install] +WantedBy=timers.target diff --git a/usr/local/bin/dynv6 b/usr/local/bin/dynv6 new file mode 100755 index 0000000..86edc00 --- /dev/null +++ b/usr/local/bin/dynv6 @@ -0,0 +1,121 @@ +#!/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 diff --git a/usr/share/doc/dynv6/.dynv6.conf.example b/usr/share/doc/dynv6/.dynv6.conf.example new file mode 100644 index 0000000..79a97f1 --- /dev/null +++ b/usr/share/doc/dynv6/.dynv6.conf.example @@ -0,0 +1,7 @@ +#!/usr/bin/env bash +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_DEVICE="" // the (optional) dynv6 device +DYNV6_NETMASK="" // the (optional) netmask \ No newline at end of file