122 lines
2.9 KiB
Text
122 lines
2.9 KiB
Text
|
#!/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_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>'.
|
||
|
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
|