Skip to content

Instantly share code, notes, and snippets.

@tspspi
Created November 1, 2019 10:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tspspi/c712f87a87cf6ac53f42dfc5faed5c09 to your computer and use it in GitHub Desktop.
Save tspspi/c712f87a87cf6ac53f42dfc5faed5c09 to your computer and use it in GitHub Desktop.
Hurricane electric IPv6 tunnel endpoint dynamic IP update script
#!/bin/sh
# INTERFACE ... Is the public interface to the internet with assigned public IP
# GIFIF ....... The generic routing encapsulating interface
# POPIP ....... IP address of your assigned point of presence (tunnel endpoint)
# STATEFILE ... File that keeps the current "old" IP address
# LOGFILE ..... Logfile into which updates will be written
# TID ......... Tunnel ID (see tunnel configuration page)
# USERID ...... User ID (as registered)
# UPDATEKEY ... See advanced tab of tunnel configuration page
INTERFACE=ng0
GIFIF=gif0
POPIP="216.66.80.98"
STATEFILE="/var/db/tunnelip.state"
LOGFILE="/var/log/tunnel.log"
TID="FILLIN_TUNNELIP"
USERID="FILLIN_USERID"
UPDATEKEY="FILLIN_UPDATEKEY"
getCurrentIpAddress()
{
IPADDR=`ifconfig ${INTERFACE} | grep 'inet ' | cut -d ' ' -f 2 2> /dev/null`
if [ $? -eq 0 ]; then
# echo "IP Address ${IPADDR}" >> ${LOGFILE}
else
echo "Nonexisting Interface ${INTERFACE}, aborting" >> ${LOGFILE}
exit 1
fi
}
getTunnelIpAddress()
{
TUNENDPOINTADDR=`ifconfig ${GIFIF} | grep 'tunnel inet' | cut -d ' ' -f 3 2> /dev/null`
if [ $? -eq 0 ]; then
# echo "Tunnel endpoint ${IPADDR}" >> ${LOGFILE}
else
echo "Nonexisting interface ${GIFIF}" >> ${LOGFILE}
exit 1
fi
}
getCurrentIpAddress
getTunnelIpAddress
if [ -e ${STATEFILE} ]; then
OLDIP=`cat ${STATEFILE}`
else
# Assign just anything invalid
OLDIP="257.257.257.257"
fi
UPDATE=0
if [ "${OLDIP}" != "${IPADDR}" ]; then
UPDATE=1
fi
if [ "${TUNENDPOINTADDR}" != "${IPADDR}" ]; then
UPDATE=1
fi
if [ ${UPDATE} -gt 0 ]; then
ifconfig ${GIFIF} tunnel ${IPADDR} ${POPIP}
RES=`curl -s https://${USERID}:${UPDATEKEY}@ipv4.tunnelbroker.net/nic/update?hostname=${TID}`
ERRORCNT=0
if [ "${RES}" == "badauth" ]; then
echo "Invalid credentials!" >> ${LOGFILE}
ERRORCNT=1
fi
if [ ${ERRORCNT} -eq 0 ]; then
echo "Update successful (response ${RES})" >> ${LOGFILE}
echo ${IPADDR} > ${STATEFILE}
else
echo "Update failed, not updating database (response ${RES})" >> ${LOGFILE}
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment