zshrc: Move ipv6-tunnel to its own function file
[grml-etc-core.git] / usr_share_grml / zsh / functions / ipv6-tunnel
diff --git a/usr_share_grml/zsh/functions/ipv6-tunnel b/usr_share_grml/zsh/functions/ipv6-tunnel
new file mode 100644 (file)
index 0000000..b0fc2c6
--- /dev/null
@@ -0,0 +1,42 @@
+# set up an ipv6 tunnel
+emulate -L zsh
+
+case $1 in
+start)
+    if ifconfig sit1 2>/dev/null | grep -q 'inet6 addr: 2002:.*:1::1' ; then
+        print 'ipv6 tunnel already set up, nothing to be done.'
+        print 'execute: "ifconfig sit1 down ; ifconfig sit0 down" to remove ipv6-tunnel.' ; return 1
+    else
+        [[ -n "$PUBLIC_IP" ]] || \
+            local PUBLIC_IP=$(ifconfig $(route -n | awk '/^0\.0\.0\.0/{print $8; exit}') | \
+            awk '/inet addr:/ {print $2}' | tr -d 'addr:')
+
+        [[ -n "$PUBLIC_IP" ]] || {
+            print 'No $PUBLIC_IP set and could not determine default one.'
+            return 1
+        }
+        local IPV6ADDR=$(printf "2002:%02x%02x:%02x%02x:1::1" $(print ${PUBLIC_IP//./ }))
+        print -n "Setting up ipv6 tunnel $IPV6ADDR via ${PUBLIC_IP}: "
+        ifconfig sit0 tunnel ::192.88.99.1 up
+        ifconfig sit1 add "$IPV6ADDR" && print done || print failed
+    fi
+    ;;
+status)
+    if ifconfig sit1 2>/dev/null | grep -q 'inet6 addr: 2002:.*:1::1' ; then
+        print 'ipv6 tunnel available' ; return 0
+    else
+        print 'ipv6 tunnel not available' ; return 1
+    fi
+    ;;
+stop)
+    if ifconfig sit1 2>/dev/null | grep -q 'inet6 addr: 2002:.*:1::1' ; then
+        print -n 'Stopping ipv6 tunnel (sit0 + sit1): '
+        ifconfig sit1 down ; ifconfig sit0 down && print done || print failed
+    else
+        print 'No ipv6 tunnel found, nothing to be done.' ; return 1
+    fi
+    ;;
+*)
+    print "Usage: ipv6-tunnel [start|stop|status]">&2 ; return 1
+    ;;
+esac