Skip to content

Instantly share code, notes, and snippets.

@W-Floyd
Forked from pklaus/tunnelbroker-net.sh
Last active August 28, 2021 22:34
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save W-Floyd/20a4c16ceb1e008cb995b7ff7dcc0a2a to your computer and use it in GitHub Desktop.
Save W-Floyd/20a4c16ceb1e008cb995b7ff7dcc0a2a to your computer and use it in GitHub Desktop.
tunnelbroker.net automatic tunnel IP update and tunnel setup (on Linux)
#!/bin/bash
# This script is published by Philipp Klaus <philipp.l.klaus@web.de>
# on <http://blog.philippklaus.de/2011/05/ipv6-6in4-tunnel-via-hurricane-electric-tunnelbroker-net-automatic-ip-update-on-mac-os-x/>
# It is originally by freese60 and modified by limemonkey.
# Found on <http://www.tunnelbroker.net/forums/index.php?topic=287.0>
# Further modified by pklaus, at <https://gist.github.com/pklaus/960672>
# Forked and mostly replaced by W-Floyd, <https://gist.github.com/W-Floyd/20a4c16ceb1e008cb995b7ff7dcc0a2a>
###############################################################################
TUNNELNAME='he-ipv6'
DEVNAME="$(ip route | grep default | tr -s ' ' | cut -d ' ' -f 5 | head -n 1)"
#DEVNAME='tun0' # If using a VPN to a static address, since I'm too lazy to get it working behind a NAT.
LOCAL_IPV4="$(ip addr show "${DEVNAME}" | grep 'inet ' | tr -s ' ' | cut -d ' ' -f 3 | sed 's#/.*##')"
echo -n "Getting external IP"
EXTERNAL_IPV4="$(curl -s 'https://canihazip.com/s')"
echo ": ${EXTERNAL_IPV4}"
# The username you use to login at tunnelbroker.net
HEUSER='you_user_name'
# This 'Update Key' can be found on the 'Advanced' tab of the tunnel details page.
HEKEY='your_update_key'
# The 'Tunnel ID' from the tab IPv6 tunnel on the tunnel details page.
HETUNNEL='your_tunnel_id'
# Tunnel settings
HESERVER4END='216.66.84.42'
HECLIENT6END='2001:470:1f12:c78::2/64'
###############################################################################
# Get root permission
###############################################################################
sudo true || exit 1
###############################################################################
# Remove the interface if it exists
###############################################################################
if [ "$(grep "${TUNNELNAME}" <<< "$(ifconfig -l | tr ' ' '\n' )")" ]; then
echo "Removing previous tunnel."
sudo ip -6 route del ::/0 dev "${TUNNELNAME}"
sudo ip link set "${TUNNELNAME}" down
sudo ip tunnel del "${TUNNELNAME}"
echo "Done removing previous tunnel."
fi
###############################################################################
echo "Updating IPv4 tunnel endpoint setting with Hurricane Electric."
curl "https://${HEUSER}:${HEKEY}@ipv4.tunnelbroker.net/nic/update?hostname=${HETUNNEL}&myip=${EXTERNAL_IPV4}" 1&> /dev/null
echo "Setting up tunnel."
sudo ip tunnel add "${TUNNELNAME}" mode sit remote "${HESERVER4END}" local "${LOCAL_IPV4}" ttl 255
sudo ip link set "${TUNNELNAME}" up
sudo ip addr add "${HECLIENT6END}" dev "${TUNNELNAME}"
sudo ip -6 route add ::/0 dev "${TUNNELNAME}"
echo "Done setting up tunnel."
exit
@W-Floyd
Copy link
Author

W-Floyd commented Oct 24, 2017

Huh, looks like I changed pretty much everything...

@samip5
Copy link

samip5 commented Feb 6, 2020

Could you maybe also change the ifconfig commands to use the actual ip commands, so it's not platform specific?
A lot of distros are not using that anymore and it's considered deprecated in some cases.

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