Fix dynv6 and just call every time
This commit is contained in:
parent
f9bfc4e106
commit
dfea4a8bb9
2 changed files with 36 additions and 56 deletions
|
@ -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_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
|
||||
|
@ -51,9 +49,6 @@ apply_defaults() {
|
|||
[[ -z ${DYNV6_MAIL_ADDRESS} ]] && DYNV6_MAIL_ADDRESS="";
|
||||
[[ -z ${DYNV6_TOKEN} ]] && DYNV6_TOKEN="";
|
||||
[[ -z ${DYNV6_HOSTNAME} ]] && DYNV6_HOSTNAME="";
|
||||
[[ -z ${DYNV6_DEVICE} ]] && DYNV6_DEVICE="";
|
||||
[[ -z ${DYNV6_NETMASK} ]] && DYNV6_NETMASK="";
|
||||
|
||||
}
|
||||
check_required() {
|
||||
if [[ -z ${DYNV6_TOKEN} ]]; then
|
||||
|
@ -64,64 +59,51 @@ check_required() {
|
|||
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;
|
||||
#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`
|
||||
echo "Checking ${hostname}"
|
||||
|
||||
if [ -z "$netmask" ]; then
|
||||
netmask=128
|
||||
# check via service
|
||||
# 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 [ -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
|
||||
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}"
|
||||
echo -e "IPv6 for ${hostname} on host ${HOSTMACHINE} changed" | mailx -s "[dynv6 IPv6 ${HOSTMACHINE}] IPv6 changed for ${hostname}" "${DYNV6_MAIL_ADDRESS}"
|
||||
fi
|
||||
fi
|
||||
|
||||
# ipv4
|
||||
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"
|
||||
|
||||
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
|
||||
fi
|
|
@ -3,5 +3,3 @@ 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
|
Loading…
Reference in a new issue