OS X下在NAT后连接清华IPv6的脚本

由于IPv6是不支持NAT的,因此,默认情况下,宿舍里装上路由器后,就没法再使用学校提供的IPv6服务了。不过难道真的没有办法了么?答案是否定的。将下面的脚本保存后,并执行,就可以在NAT后利用isatap隧道来使用IPv6啦~脚本中的隧道服务器是清华的。

#!/bin/bash
LAN_IP=`/sbin/ifconfig en0 | grep inet | grep -v inet6 | awk '{print $2}'`

myip() {
    echo >&2 "Getting your public IPv4 address"
    if type wget >/dev/null 2>/dev/null; then
        wget -qO- 'http://ipv4.icanhazip.com'
    elif type curl >/dev/null 2>/dev/null; then
        curl 'http://ipv4.icanhazip.com'
    else
        echo >&2 "Neither of wget and curl found. Install one of them. Abort."
        exit 1
    fi
}

WAN_IP=`myip`

/sbin/ifconfig gif0 destroy
/sbin/ifconfig gif0 create inet6 2001:da8:200:900e:0:5efe:$WAN_IP prefixlen 64
/sbin/ifconfig gif0 tunnel $LAN_IP 59.66.4.50
/sbin/route delete -inet6 default
/sbin/route add -inet6 default 2001:da8:200:900e::1