Last active
August 29, 2015 14:14
-
-
Save awaddell/9a148d3cdf97b2239396 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# this script does not read or change the local interface at all | |
# its for use with a host behind NAT where the NAT device can do 6rd or 6in4 but can't update the tunnel broker | |
# as tunnelbroker.net don't want us to brute force their API, we leave the API update until we're sure our IPv4 address has changed | |
# http://www.linuxjournal.com/content/validating-ip-address-bash-script | |
function valid_ip() | |
{ | |
local ip=$1 | |
local stat=1 | |
if [[ $ip =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then | |
OIFS=$IFS | |
IFS='.' | |
ip=($ip) | |
IFS=$OIFS | |
[[ ${ip[0]} -le 255 && ${ip[1]} -le 255 \ | |
&& ${ip[2]} -le 255 && ${ip[3]} -le 255 ]] | |
stat=$? | |
fi | |
return $stat | |
} | |
# If run directly, execute some tests. | |
if [[ "$(basename $0 .sh)" == 'valid_ip' ]]; then | |
ips=' | |
4.2.2.2 | |
a.b.c.d | |
192.168.1.1 | |
0.0.0.0 | |
255.255.255.255 | |
255.255.255.256 | |
192.168.0.1 | |
192.168.0 | |
1234.123.123.123 | |
' | |
for ip in $ips | |
do | |
if valid_ip $ip; then stat='good'; else stat='bad'; fi | |
printf "%-20s: %s\n" "$ip" "$stat" | |
done | |
fi | |
read_dom () { | |
local IFS=\> | |
read -d \< ENTITY CONTENT | |
} | |
#HE_USER should be in ENV | |
#HE_PASS should be in ENV | |
#HE_TUNNEL should be in ENV | |
HE_PASS_ENC=`perl -MURI::Escape -e "print uri_escape('$HE_PASS')"` | |
HE_CURRENT_IPV4_FILE="/var/tmp/my_tunnelbroker_values" | |
HE_CURRENT_IPV4="" | |
# https://stackoverflow.com/questions/6118948/bash-loop-ping-successful?rq=1 | |
((count = 10)) # Maximum number to try. | |
while [[ $count -ne 0 ]] ; do | |
# as with good ole 8.8.8.8 we want an IPv6 address and not a hostname | |
ping6 -q -c 1 2001:4860:4860::8888 > /dev/null # Try once. | |
rc=$? | |
if [[ $rc -eq 0 ]] ; then | |
((count = 1)) # If okay, flag to exit loop. | |
fi | |
((count = count - 1)) # So we don't go forever. | |
done | |
if [[ $rc -eq 0 ]] ; then # Make final determination. | |
echo "ipv6 is up" | |
# only run if ipv6 appears down | |
exit 0 | |
else | |
echo `say IPv6 timed out.` | |
fi | |
# update / make sure we have the he.net current tunnelbroker values (XML output) | |
curl -k -s -o $HE_CURRENT_IPV4_FILE "https://$HE_USER:$HE_PASS_ENC@tunnelbroker.net/tunnelInfo.php?tid=$HE_TUNNEL" | |
# https://stackoverflow.com/questions/893585/how-to-parse-xml-in-bash | |
while read_dom; do | |
if [[ $ENTITY = "clientv4" ]]; then | |
if valid_ip $CONTENT; then | |
echo "he.net IPV4 is $CONTENT" | |
HE_CURRENT_IPV4="$CONTENT" | |
fi | |
#exit | |
fi | |
done < $HE_CURRENT_IPV4_FILE | |
# this could be more sophisticated with a list to walk through if one fails | |
MY_CURRENT_IPV4=`curl -s "http://ipv4.whatsmyip.reliable-ict.de/"` | |
if [[ "$MY_CURRENT_IPV4" == "$HE_CURRENT_IPV4" ]]; then | |
echo "ips are the same" | |
else | |
echo "not matched so updating tunnelbroker" | |
curl -k -s "https://ipv4.tunnelbroker.net/nic/update?username=$HE_USER&password=$HE_PASS&hostname=$HE_TUNNEL" | |
#sleep 1 | |
#echo "External IPv6:`curl -s 'http://ipv6.whatsmyip.reliable-ict.de/'`." | |
#echo "External IPv4:`curl -s 'http://whatsmyip.reliable-ict.de/'`." | |
fi | |
# TODO Launch shell script on login in Mac OS (OS X) http://stackoverflow.com/questions/22842016/launch-shell-script-on-login-in-mac-os-os-x | |
# current data is available with | |
# curl -k -s "https://$HE_USER:$HE_PASS_ENC@tunnelbroker.net/tunnelInfo.php?tid=$HE_TUNNEL" | |
# returns | |
# <?xml version="1.0" encoding="UTF-8"?> | |
# <tunnels> | |
# <tunnel id="{tunnel_id}> | |
# <description></description> | |
# <serverv4>{Server IPv4 Address}</serverv4> | |
# <clientv4>{Client IPv4 Address}</clientv4> | |
# <serverv6>{Server IPv6 Address}</serverv6> | |
# <clientv6>{Client IPv6 Address}</clientv6> | |
# <routed64>{Routed /64}</routed64> | |
# </tunnel> | |
# </tunnels> | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment