Skip to content

Instantly share code, notes, and snippets.

Created October 27, 2017 11:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/b99388a56a78f3dbeba2fcb6259419f8 to your computer and use it in GitHub Desktop.
Save anonymous/b99388a56a78f3dbeba2fcb6259419f8 to your computer and use it in GitHub Desktop.
spdyn.de update script
#!/bin/bash
# spdyn.de external ip update script
# usage:
# bash spdyn_update
# Check external ips and update spdyn host entries only if necessary.
# Can be run any time, as it does not trigger unnecessary updates.
# bash spdyn_update force
# Forced update of spdyn host entries. Does not care about unchanged public ips.
# Keep in mind: Frequent unnecessary updates might get you blocked by spdyn.
# Function url encodes given string
# usage:
# testvar="2003:45:4320:6705:4a4d:7eff:fedb:7d6c"
# testresult=`rawurlencode "$testvar"`
# echo "$testresult"
rawurlencode()
{
local string="${1}"
local strlen=${#string}
local encoded=""
local pos c o
for (( pos=0 ; pos<strlen ; pos++ )); do
c=${string:$pos:1}
case "$c" in
[-_.~a-zA-Z0-9] ) o="${c}" ;;
* ) printf -v o '%%%02x' "'$c"
esac
encoded+="${o}"
done
echo "${encoded}"
}
# Function updates spdyn host entry. Depends on function rawurlencode.
# usage:
# dns_host="somehost.spdns.de"
# dns_token="aaa-bbb-ccc"
# external_ip="10.10.10.10"
# response=`update_spdyn "$dns_host" "$dns_token" "$external_ip"`
# echo "$response"
update_spdyn()
{
local loc_dns_host="$1"
local loc_dns_host_urlencoded=`rawurlencode "$loc_dns_host"`
local loc_dns_token="$2"
local loc_ext_ip="$3"
local loc_ext_ip_urlencoded=`rawurlencode "$loc_ext_ip"`
local loc_spdns_response=`wget --user=$loc_dns_host --password=$loc_dns_token "https://update.spdyn.de/nic/update?hostname=$loc_dns_host_urlencoded&myip=$loc_ext_ip_urlencoded" -O "-" --quiet`
echo $loc_spdns_response
}
### START: edit host parameters ###
# set host vars to empty string to skip specific check
ipv4_dns_host="somehostforipv4.spdns.de"
ipv4_dns_token="aaaa-bbbb-cccc"
ipv6_dns_host="somehostforipv6.spdns.de"
ipv6_dns_token="dddd-eeee-ffff"
# if IPv6 should be skipped, it would look like this
# ipv4_dns_host="somehostforipv4.spdns.de"
# ipv4_dns_token="aaaa-bbbb-cccc"
# ipv6_dns_host=""
# ipv6_dns_token=""
### END: edit host parameters ###
# define response file (derived from script file)
response_file_v4="$(readlink -f $0)_v4.response"
response_file_v6="$(readlink -f $0)_v6.response"
# get external ips
[[ ! "`/usr/bin/tty`" == "not a tty" ]] && echo -e "\nspdyn.de update: Checking external ips ..."
if [ ! -z "$ipv4_dns_host" ]; then
external_ip_v4=`wget "http://checkip4.spdyn.de" -O "-" --quiet`
else
[[ ! "`/usr/bin/tty`" == "not a tty" ]] && echo -e "\nSkipped IPv4 check (no IPv4 host defined)."
fi
if [ ! -z "$ipv6_dns_host" ]; then
external_ip_v6=`wget "http://checkip6.spdyn.de" -O "-" --quiet`
else
[[ ! "`/usr/bin/tty`" == "not a tty" ]] && echo -e "\nSkipped IPv6 check (no IPv6 host defined)."
fi
# determine operation mode
force_update="no"
if [ $# -ne 0 ]; then
if [ $1 == "force" ]; then
force_update="yes"
[[ ! "`/usr/bin/tty`" == "not a tty" ]] && echo -e "\nForced spdns update requested ..."
else
[[ ! "`/usr/bin/tty`" == "not a tty" ]] && echo -e "\nInvalid parameter! Defaulting to standard mode ..."
fi
fi
# report and save results
if [ -z "$external_ip_v4" ] && [ -z "$external_ip_v6" ]; then
# no public ip was obtained
[[ ! "`/usr/bin/tty`" == "not a tty" ]] && echo -e "\nNo internet connection detected."
# write failed response files
# only if checks were not skipped intentionally - otherwise remove existing response files
if [ ! -z "$ipv4_dns_host" ]; then
echo "OPERATION FAILED TO FETCH EXTERNAL IP V4" > $response_file_v4
else
rm -f "$response_file_v4"
fi
if [ ! -z "$ipv6_dns_host" ]; then
echo "OPERATION FAILED TO FETCH EXTERNAL IP V6" > $response_file_v6
else
rm -f "$response_file_v6"
fi
elif [ -z "$external_ip_v6" ] && [ ! -z "$external_ip_v4" ]; then
# only ip v4 was obtained
[[ ! "`/usr/bin/tty`" == "not a tty" ]] && echo -e "\nIPv4 connection detected."
[[ ! "`/usr/bin/tty`" == "not a tty" ]] && echo -e "Current external ip v4: $external_ip_v4"
# write failed response file v6
# only if v6 check was not skipped intentionally - otherwise remove existing response file
if [ ! -z "$ipv6_dns_host" ]; then
echo "OPERATION FAILED TO FETCH EXTERNAL IP V6" > $response_file_v6
else
rm -f "$response_file_v6"
fi
elif [ ! -z "$external_ip_v6" ] && [ -z "$external_ip_v4" ]; then
# only ip v6 was obtained
[[ ! "`/usr/bin/tty`" == "not a tty" ]] && echo -e "\nIPv6 connection detected."
[[ ! "`/usr/bin/tty`" == "not a tty" ]] && echo -e "Current external ip v6: $external_ip_v6"
# write failed response file v4
# only if v4 check was not skipped intentionally - otherwise remove existing response file
if [ ! -z "$ipv4_dns_host" ]; then
echo "OPERATION FAILED TO FETCH EXTERNAL IP V4" > $response_file_v4
else
rm -f "$response_file_v4"
fi
elif [ ! -z "$external_ip_v6" ] && [ ! -z "$external_ip_v4" ]; then
[[ ! "`/usr/bin/tty`" == "not a tty" ]] && echo -e "\nIPv6 Dual Stack connection detected."
[[ ! "`/usr/bin/tty`" == "not a tty" ]] && echo -e "Current external ip v6: $external_ip_v6"
[[ ! "`/usr/bin/tty`" == "not a tty" ]] && echo -e "Current external ip v4: $external_ip_v4"
fi
# update ip v4 host
if [ ! -z "$external_ip_v4" ]; then
# read ip from last response file
if [ ! -f "$response_file_v4" ]; then
last_ip_v4=""
[[ ! "`/usr/bin/tty`" == "not a tty" ]] && echo -e "\nNo previous response file v4 found ..."
else
last_ip_v4=$(head -n 1 "$response_file_v4")
last_ip_v4=`echo $last_ip_v4 | cut -d \ -f 2`
[[ ! "`/usr/bin/tty`" == "not a tty" ]] && echo -e "\nPrevious response file v4 reports: $last_ip_v4"
fi
# check if ip has changed or forced update was requested
if [ ! "$external_ip_v4" = "$last_ip_v4" ] || [ "$force_update" = "yes" ]; then
# send update request
[[ ! "`/usr/bin/tty`" == "not a tty" ]] && echo -e "Updating IPv4 spdyn host $ipv4_dns_host ..."
response_v4=`update_spdyn "$ipv4_dns_host" "$ipv4_dns_token" "$external_ip_v4"`
# save result into file
echo "$response_v4" > $response_file_v4
# display result
[[ ! "`/usr/bin/tty`" == "not a tty" ]] && echo "spdyn response: $response_v4"
else
[[ ! "`/usr/bin/tty`" == "not a tty" ]] && echo "No update required. Public ip is still correct."
fi
fi
# update ip v6 host
if [ ! -z "$external_ip_v6" ]; then
# read ip from last response file
if [ ! -f "$response_file_v6" ]; then
last_ip_v6=""
[[ ! "`/usr/bin/tty`" == "not a tty" ]] && echo -e "\nNo previous response file v6 found ..."
else
last_ip_v6=$(head -n 1 "$response_file_v6")
last_ip_v6=`echo $last_ip_v6 | cut -d \ -f 2`
[[ ! "`/usr/bin/tty`" == "not a tty" ]] && echo -e "\nPrevious response file v6 reports: $last_ip_v6"
fi
# check if ip has changed or forced update was requested
if [ ! "$external_ip_v6" = "$last_ip_v6" ] || [ "$force_update" = "yes" ]; then
# send update request
[[ ! "`/usr/bin/tty`" == "not a tty" ]] && echo -e "Updating IPv6 spdyn host $ipv6_dns_host ..."
response_v6=`update_spdyn "$ipv6_dns_host" "$ipv6_dns_token" "$external_ip_v6"`
# save result into file
echo "$response_v6" > $response_file_v6
# display result
[[ ! "`/usr/bin/tty`" == "not a tty" ]] && echo "spdyn response: $response_v6"
else
[[ ! "`/usr/bin/tty`" == "not a tty" ]] && echo "No update required. Public ip is still correct."
fi
fi
[[ ! "`/usr/bin/tty`" == "not a tty" ]] && echo -e "\n"
# done
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment