Skip to content

Instantly share code, notes, and snippets.

@alick
Created April 14, 2016 20:05
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alick/e22e2bf49c4ea457d16df2959720aa75 to your computer and use it in GitHub Desktop.
Save alick/e22e2bf49c4ea457d16df2959720aa75 to your computer and use it in GitHub Desktop.
Bring up IPv6 through ISATAP tunnel.
#!/bin/bash
# vim730: set tw=80 cc=+1:
# Bring up IPv6 through ISATAP tunnel.
version="1.0"
usage() {
cat <<EOF
USAGE:
$0 [-d -i interface -s solution]
$0 -h/--help/-v/--version
When -d is specified, it will delete sit1 interface. Otherwise it will set up
(possibly override exsiting) isatap interface.
When no interface is specified, it will use the first global IPv4 address found
in 'ip addr'.
The solution of isatap service can be one of: thu/wlan/sjtu. When no solution is
specified, it will default to thu.
EOF
}
# Default values.
iface=""
sln="thu"
action="add"
args=$(getopt -o "dhvi:s:" -l "help,version" -- "$@")
if [ $? -ne 0 ]; then
usage
exit 1
fi
eval set -- "$args"
while [ ! -z "$1" ]; do
case "$1" in
-d)
action="delete"
;;
-i)
shift
iface="$1"
;;
-s)
shift
sln="$1"
;;
-v|--version)
echo "$(basename $0) $version"
exit 0
;;
-h|--help)
usage
exit 0
;;
--)
shift
break
;;
esac
shift
done
if [ $# -ne 0 ]; then
printf "Error: unrecognized arguments $@.\n" >&2
usage
exit 1
fi
# Get IPv4 address.
if [ -n "$iface" ]; then
line="`ip addr show dev "$iface" | grep 'inet .*scope global' \
| head -n 1`"
else
line="`ip addr | grep 'inet .*scope global' | head -n 1`"
fi
# Get global ipv4 address in 'addr/prefix' format.
if [ -z "$line" ]; then
if [ -z "$iface" ]; then
printf "Error: no valid IPv4 address found.\n" >&2
else
printf "Error: no valid IPv4 address found on $iface.\n" >&2
fi
exit 5
fi
yourip="`echo $line | awk '{print $2}'`"
# Strip trailing "/prefix" part.
yourip="${yourip%/*}"
if [ -z "$iface" ]; then
iface="`echo $line | awk '{print $NF}'`"
fi
echo "Given local ip $yourip on interface $iface."
echo "Given solution $sln."
echo "Given action $action."
do_delete ()
{
if sudo ip tunnel show sit1 >/dev/null 2>&1; then
sudo ip tunnel del sit1
fi
}
if [ "$action" = "delete" ]; then
do_delete
exit $?
fi
if [ "$sln" = "thu" ]; then
IsatapIPv4=59.66.4.50
IsatapIPv6Prefix=2001:da8:200:900e:0:5efe
IsatapIPv6Router=2001:da8:200:900e::1
elif [ "$sln" = "wlan" ]; then
IsatapIPv4=166.111.21.1 # isatap.tsinghua.edu.cn
IsatapIPv6Prefix=2001:da8:200:900e:0:5efe
IsatapIPv6Router=2001:da8:200:900e::1
elif [ "$sln" = "sjtu" ]; then
# This works!
# From: http://bbs.sjtu.edu.cn/bbstcon?board=SJTUnet&reid=1212259140
IsatapIPv4=202.120.58.150
# IPv4 addr of isatap.sjtu.edu.cn
# do not work...
#IsatapIPv4=202.112.26.246
IsatapIPv6Prefix=2001:da8:8000:d010:0:5efe
IsatapIPv6Router=2001:da8:8000:d010::1
else
echo "Unkown solution: $sln"
exit 1
fi
# Add new isatap interface.
do_delete
echo "Adding new sit1..."
sudo ip tunnel add sit1 mode sit remote $IsatapIPv4 local $yourip
sudo ifconfig sit1 up
sudo ifconfig sit1 add $IsatapIPv6Prefix:$yourip/64
# Make sure to remove default route through original interface.
while [ -n "$(sudo ip -6 route show ::/0 dev ${iface})" ]; do
sudo ip route del ::/0 dev ${iface}
sleep 1
done
sudo ip route add ::/0 via $IsatapIPv6Router metric 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment