Skip to content

Instantly share code, notes, and snippets.

@ZE3kr
Created February 25, 2017 00:07
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 ZE3kr/15e3e4354a9be3ac21eb0f51eb183346 to your computer and use it in GitHub Desktop.
Save ZE3kr/15e3e4354a9be3ac21eb0f51eb183346 to your computer and use it in GitHub Desktop.
DDNS.sh for Rage4, Hurricane Electric DNS and Hurricane Electric Tunnel Broker
#!/bin/sh
# A simple DDNS tool written in shell, support Rage4, Hurricane Electric DNS and Hurricane Electric Tunnel Broker.
# @version 0.1.2
# Global Settings, set to ture to enable
use_ipv4=false;
use_ipv6=false;
logfile="/var/log/ddns.log"; # Set it to "/dev/null" to disable log.
get_ip_service="http://api.tlo.xyz:8081/myip/ip.php"; # Need to support IPv4 and IPv6
# Specific Settings, set to ture to enable
## Hurricane Electric DNS IPv4
hedns_ipv4=false;
hedns_ipv4_auth=""; # Password
hedns_ipv4_hostname="";
## Hurricane Electric DNS IPv6
hedns_ipv6=false;
hedns_ipv6_auth=""; # Password
hedns_ipv6_hostname="";
## Hurricane Electric Tunnel Broker
tunnelbroker=false;
tunnelbroker_auth=""; # "user:pass"
tunnelbroker_hostname="";
## Rage4 IPv4
rage4_ipv4=false;
rage4_ipv4_auth=""; # Dynamic DNS Key for your IPv4 record
## Rage4 IPv6
rage4_ipv6=false;
rage4_ipv6_auth=""; # Dynamic DNS Key for your IPv6 record
# Stop editing.
log="tee -a $logfile";
date=`date +"%m-%d-%Y %T"`;
echo "[$date] Start DDNS demon" | $log;
ipv4="";
ipv6="";
ceol=`tput el`;
# Start as a loop
while [ 1 ]
do
if [ "$use_ipv4" = true ]; then
date=`date +"%m-%d-%Y %T"`;
echo -n "[$date] Getting new IPv4 address…";
until sleep 1 ; ipv4_new=`curl -s -m 15 -4 $get_ip_service` ; do :; done
echo -ne "\r${ceol}";
if [ "$ipv4_new" != "$ipv4" ]; then
ipv4=$ipv4_new;
date=`date +"%m-%d-%Y %T"`;
echo "[$date] IPv4 changed to $ipv4" | $log;
if [ "$hedns_ipv4" = true ]; then
date=`date +"%m-%d-%Y %T"`;
echo -n "[$date] Updating HE DNS IPv4…";
hedns_ipv4_status=`curl -m 15 -s -o /dev/null -w "%{http_code}" -k "https://dyn.dns.he.net/nic/update?hostname=$hedns_ipv4_hostname&password=$hedns_ipv4_auth&myip=$ipv4"`;
echo -ne "\r${ceol}";
echo " Updated HE DNS IPv4 ($hedns_ipv4_status)" | $log;
fi
if [ "$tunnelbroker" = true ]; then
date=`date +"%m-%d-%Y %T"`;
echo -n "[$date] Updating Tunnel Broker…";
tunnelbroker_status=`curl -m 15 -s -o /dev/null -w "%{http_code}" "https://$tunnelbroker_auth@ipv4.tunnelbroker.net/nic/update?hostname=$tunnelbroker_hostname&myip=$ipv4"`;
echo -ne "\r${ceol}";
echo " Updated Tunnel Broker ($tunnelbroker_status)" | $log;
fi
if [ "$rage4_ipv4" = true ]; then
date=`date +"%m-%d-%Y %T"`;
echo -n "[$date] Updating Rage4 IPv4…";
rage4_ipv4_status=`curl -m 15 -s -o /dev/null -w "%{http_code}" "https://secure.rage4.com/secure/dynamic/$rage4_ipv4_auth?ip=$ipv4"`;
echo -ne "\r${ceol}";
echo " Updated Rage4 IPv4 ($rage4_ipv4_status)" | $log;
fi
fi
fi
if [ "$use_ipv6" = true ]; then
date=`date +"%m-%d-%Y %T"`;
echo -n "[$date] Getting new IPv6 address…";
until ipv6_new=`curl -s -m 5 -6 $get_ip_service`; do :; done
echo -ne "\r${ceol}";
if [ "$ipv6_new" != "$ipv6" ]; then
ipv6=$ipv6_new;
date=`date +"%m-%d-%Y %T"`;
echo "[$date] IPv6 changed to $ipv6" | $log;
if [ "$hedns_ipv6" = true ]; then
date=`date +"%m-%d-%Y %T"`;
echo -n "[$date] Updating HE DNS IPv6…";
hedns_ipv6_status=`curl -m 15 -s -o /dev/null -w "%{http_code}" -k "https://dyn.dns.he.net/nic/update?hostname=$hedns_ipv6_hostname&password=$hedns_ipv6_auth&myip=$ipv6"`;
echo -ne "\r${ceol}";
echo " Updated HE DNS IPv6 ($hedns_ipv6_status)" | $log;
fi
if [ "$rage4_ipv6" = true ]; then
date=`date +"%m-%d-%Y %T"`;
echo -n "[$date] Updating Rage4 IPv6…";
rage4_ipv6_status=`curl -m 15 -s -o /dev/null -w "%{http_code}" "https://secure.rage4.com/secure/dynamic/$rage4_ipv6_auth?ip=$ipv6"`;
echo -ne "\r${ceol}";
echo " Updated Rage4 IPv6 ($rage4_ipv6_status)" | $log;
fi
fi
fi
date=`date +"%m-%d-%Y %T"`;
echo -n "[$date] Sleeping for 60 second…";
sleep 60;
echo -ne "\r${ceol}";
# Retry if status code is not 200
if [ "$use_ipv4" = true ]; then
if [ "$hedns_ipv4" = true ]; then
if [ "$hedns_ipv4_status" != "200" ]; then
date=`date +"%m-%d-%Y %T"`;
echo -n "[$date] Retrying Rage4 IPv6…";
hedns_ipv4_status=`curl -m 15 -s -o /dev/null -w "%{http_code}" -k "https://dyn.dns.he.net/nic/update?hostname=$hedns_ipv4_hostname&password=$hedns_ipv4_auth&myip=$ipv4"`;
echo -ne "\r${ceol}";
echo " Retried HE DNS IPv4 ($hedns_ipv4_status)" | $log;
fi
fi
if [ "$tunnelbroker" = true ]; then
if [ "$tunnelbroker_status" != "200" ]; then
date=`date +"%m-%d-%Y %T"`;
echo -n "[$date] Retrying Tunnel Broker…";
tunnelbroker_status=`curl -m 15 -s -o /dev/null -w "%{http_code}" "https://$tunnelbroker_auth@ipv4.tunnelbroker.net/nic/update?hostname=$tunnelbroker_hostname&ipv4b=$ipv4"`;
echo -ne "\r${ceol}";
echo " Retried Tunnel Broker ($tunnelbroker)" | $log;
fi
fi
if [ "$rage4_ipv4" = true ]; then
if [ "$rage4_ipv4_status" != "200" ]; then
date=`date +"%m-%d-%Y %T"`;
echo -n "[$date] Retrying Rage4 IPv4";
rage4_ipv4_status=`curl -m 15 -s -o /dev/null -w "%{http_code}" "https://secure.rage4.com/secure/dynamic/$rage4_ipv4_auth?ip=$ipv4"`;
echo -ne "\r${ceol}";
echo " Retried Rage4 IPv4 ($rage4_ipv4_status)" | $log;
fi
fi
fi
if [ "$use_ipv6" = true ]; then
if [ "$hedns_ipv6" = true ]; then
if [ "$hedns_ipv6_status" != "200" ]; then
date=`date +"%m-%d-%Y %T"`;
echo -n "[$date] Retrying HE DNS IPv6";
hedns_ipv6_status=`curl -m 15 -s -o /dev/null -w "%{http_code}" -k "https://dyn.dns.he.net/nic/update?hostname=$hedns_ipv6_hostname&password=$hedns_ipv6_auth&myip=$ipv6"`;
echo -ne "\r${ceol}";
echo " Retried HE DNS IPv6: ($hedns_ipv6_status)" | $log;
fi
fi
if [ "$rage4_ipv6" = true ]; then
if [ "$rage4_ipv6_status" != "200" ]; then
date=`date +"%m-%d-%Y %T"`;
echo -n "[$date] Retrying Rage4 IPv6";
rage4_ipv6_status=`curl -m 15 -s -o /dev/null -w "%{http_code}" "https://secure.rage4.com/secure/dynamic/$rage4_ipv6_auth?ip=$ipv6"`;
echo -ne "\r${ceol}";
echo " Retried Rage4 IPv6: ($rage4_ipv6_status)" | $log;
fi
fi
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment