Skip to content

Instantly share code, notes, and snippets.

@skiold
Created June 3, 2011 10:46
Show Gist options
  • Save skiold/1006164 to your computer and use it in GitHub Desktop.
Save skiold/1006164 to your computer and use it in GitHub Desktop.
hurricane electric ipv6 tunneling script for macosx
#!/bin/bash
#######################################################################
# Update the HE (Hurricane Electric) ipv6-tunnel
#######################################################################
# original at: http://pugio.net/2009/01/enable-ipv6-on-mac-os-x-the-tu.html
# Interfaces to try, in order: en1 = Airport, en0 = Ethernet
MYIFS="en0 en1"
# leave as is
IPCACHE="/Library/Caches/ipv6scriptIP"
# Your Tunnel settings start here
# 1. get HEUSER hash from the website, "UserID"
# 2. get HEPASS hash: echo -n YourPass|md5
# 3. get HETUNNEL from the website, "Global Tunnel ID"
# 4. get other settings from the website
HEUSER=YOURUSERID
HEPASS=YOURUSERPASS
HETUNNEL=YOURTUNNELID
HETUNEND=HEIPV4TUNNELEND
HEYOUR6END=HEIPV6TUNNELEND
HETHEIR6END=YOURIPV6TUNNELEND
HEPREFIX=64
# This is some IP from the "Routed /64" pool, used for outgoing connections from your Mac.
# Should it get blocked by anyone, you can simply change it to any other IP from the pool
# without having to apply for a new tunnel. e.g. if your Routed /64 pool is
# 2001:0123:123b:1234::/64, you can use this for your IP:
HEMY64IP=2001:0123:123b:1234::0bad:cafe
#######################################################################
# Config end
#######################################################################
delete_tunnel() {
# let's delete a pre-existing gif0, ignore any errors
ifconfig gif0 deletetunnel
ifconfig gif0 down
ifconfig gif0 inet6 delete $HEYOUR6END
ifconfig gif0 inet6 delete $HEMY64IP
route delete -inet6 default -interface gif0
}
case "$1" in
stop)
delete_tunnel
exit 0
;;
*)
echo "start by default"
;;
esac
# begin by deletting link-local address on ifaces
ifconfig en1 inet6 delete fe80::20d:93ff:fe86:6902%en1
ifconfig lo0 inet6 delete fe80::1%lo0
# sometimes this script will get executed twice at the same time, not good, so:
if [ -f $IPCACHE.lock ] ; then
echo A copy already running!
exit 0
else
touch $IPCACHE.lock
fi
# This is faster if your router sets a dyndns entry:
#NEW_IP=`dig mycomp.myvnc.com|grep "^mycomp"| grep -Eo "\<[[:digit:]]{1,3}(\.[[:digit:]]{1,3}){3}\>"`
NEW_IP=`curl -s "http://www.networksecuritytoolkit.org/nst/cgi-bin/ip.cgi"`
# Wait for the network...
while [ ! -n "$NEW_IP" ]
do
sleep 10
#NEW_IP=`dig mycomp.myvnc.com|grep "^mycomp"| grep -Eo "\<[[:digit:]]{1,3}(\.[[:digit:]]{1,3}){3}\>"`
NEW_IP=`curl -s "http://www.networksecuritytoolkit.org/nst/cgi-bin/ip.cgi"`
done
OLD_IP=`cat $IPCACHE`
if [ "$NEW_IP" = "$OLD_IP" ] ; then
CURCONF=`ifconfig |grep $HETUNEND`
if [ -n "$CURCONF" ] ; then
echo Nothing to do
rm $IPCACHE.lock
exit 0
fi
fi
echo -n $NEW_IP > $IPCACHE
# if you need to use your public ip address, use LOCAL_IP=$NEW_IP instead
for myif in $MYIFS; do
LOCAL_IP=`ifconfig $myif |grep -E 'inet.[0-9]' | grep -v '127.0.0.1' | awk '{ print $2}'`
if [ -n "$LOCAL_IP" ]; then break; fi
done
delete_tunnel
# update the tunnel
curl -k -s "https://ipv4.tunnelbroker.net/ipv4_end.php?ipv4b=$NEW_IP&pass=$HEPASS&user_id=$HEUSER&tunnel_id=$HETUNNEL"
echo " "
sleep 1
ifconfig gif0 tunnel $LOCAL_IP $HETUNEND
ifconfig gif0 inet6 $HEMY64IP/64 alias
ifconfig gif0 inet6 $HEYOUR6END $HETHEIR6END prefixlen /$HEPREFIX
route -n add -inet6 default $HETHEIR6END
rm $IPCACHE.lock
exit 0
@skiold
Copy link
Author

skiold commented Jun 3, 2011

Add manual stop using 'ipv6script stop'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment