<< Previous Next >>

My IPv6 Tunnel Update Script

# Update Hurricane Electric IPv6 Tunnel Client IPv4 address

# From: http://forum.mikrotik.com/viewtopic.php?f=9&t=56466

:local HEtunnelinterface "sit1"
:local HEtunnelid "######"
:local HEuserid "######################"
:local HEmd5pass "######################"
:local HEupdatehost "ipv4.tunnelbroker.net"
:local HEupdatepath "/ipv4_end.php"
:local WANinterface "ether1-wan"
:local outputfile ("HE-" . $HEtunnelid . ".txt")


# Internal processing below...
# ----------------------------------
:local HEipv4addr

# Get WAN interface IP address
:set HEipv4addr [/ip address get [/ip address find interface=$WANinterface] address]
:set HEipv4addr [:pick [:tostr $HEipv4addr] 0 [:find [:tostr $HEipv4addr] "/"]]

:if ([:len $HEipv4addr] = 0) do={
   :log error ("Could not get IP for interface " . $WANinterface)
   :error ("Could not get IP for interface " . $WANinterface)
}

:local HEtunnelipv4addr

# Get current TUNNEL interface IP address
:set HEtunnelipv4addr [/interface 6to4 get $HEtunnelinterface local-address]

:if ([:len $HEtunnelipv4addr] = 0) do={
    :log error ("Could not get IP for interface " . $HEtunnelinterface)
    :error ("Could not get IP for interface " . $HEtunnelinterface)
}
:if ([:len $HEtunnelipv4addr] = 0) do={
    :log error ("Could not get IP for interface " . $HEtunnelinterface)
    :error ("Could not get IP for interface " . $HEtunnelinterface)
}

:if ($HEipv4addr != $HEtunnelipv4addr) do={
    :local htmlcontent
    :log info ("Updating IPv6 Tunnel " . $HEtunnelid . " Client IPv4 address to new IP " . $HEipv4addr . "...")
    /tool fetch mode=http \
        host=($HEupdatehost) \
        url=("http://" . $HEupdatehost . $HEupdatepath . \
             "?ipv4b=" . $HEipv4addr . \
             "&pass=" . $HEmd5pass . \
             "&user_id=" . $HEuserid . \
             "&tunnel_id=" . $HEtunnelid) \
        dst-path=($outputfile)

    :set htmlcontent [/file get $outputfile contents]
    /file remove $outputfile

    :if ($htmlcontent = "+OK: Tunnel endpoint updated to: $HEipv4addr" || \
    $htmlcontent = "-ERROR: This tunnel is already associated with this IP address.  Please try and limit your updates to IP changes.") do={
        :log info "OK Tunnel endpoint updated  to $HEipv4addr. Updating 6to4 interface"

        /interface 6to4 {
            :if ([get ($HEtunnelinterface) local-address] != $HEipv4addr) do={
                :log info ("Updating " . $HEtunnelinterface . " local-address with new IP " . $HEipv4addr . "...")
                set ($HEtunnelinterface) local-address=$HEipv4addr
            }
        }
    } else={
      :log info "Update FAIL"
      :log info $htmlcontent
    }
} else={
    :log info ("This tunnel is already associated with current public IP address (" . $WANinterface . "). Address update is not necessary")
}