Last active
March 12, 2018 19:04
-
-
Save linuxgemini/a8e4617cf504791a7f959fd32dbabb94 to your computer and use it in GitHub Desktop.
Update your DuckDNS and Tunnelbroker IPs at the same time. Tested on Mikrotik RB750Gr3.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| :local WANinterface "wan-interface-name" | |
| :local HEtunnelinterface "6to4-interface-name" | |
| :local DuckDNSDomains "duckdns-domain,or-domains-seperated-with-commas" | |
| :local DuckDNSToken "duckdns-token" | |
| :local HEtunnelid "tunnelbroker-tunnel-id" | |
| :local HEuserid "tunnelbroker-username" | |
| :local HEmd5pass "tunnelbroker-tunnel-update-passkey" | |
| # Process variables starts below. | |
| #----------------------------- | |
| :local updateHost "www.duckdns.org" | |
| :local updatePath "/update" | |
| :local resultFile "duckdns-result.txt" | |
| :local HEupdatehost "ipv4.tunnelbroker.net" | |
| :local HEupdatepath "/nic/update" | |
| :local HEresultfile ("HE-" . $HEtunnelid . ".txt") | |
| :local ipStore "ipstore.txt" | |
| # Find actual IPv4 address | |
| #------------------------------- | |
| :local currentIP | |
| :set currentIP [/ip address get [/ip address find interface=$WANinterface] address] | |
| :set currentIP [:pick [:tostr $currentIP] 0 [:find [:tostr $currentIP] "/"]] | |
| :if ([:len $currentIP] = 0) do={ | |
| :log error ("Could not get IP for interface " . $WANinterface) | |
| :error ("Could not get IP for interface " . $WANinterface) | |
| } | |
| # Check if ipStore file exists | |
| # If doesn't, create one and set it to 0.0.0.0 | |
| #-------------------------------------------------- | |
| :if ([:len [/file find where name=($ipStore) ]] < 1 ) do={ | |
| /file print file=($ipStore) | |
| /delay delay-time=2 | |
| /file set $ipStore contents="0.0.0.0" | |
| } | |
| # Set previous IPv4 address as the one on ipStore | |
| #-------------------------------------------------- | |
| :local previousIP | |
| :set previousIP [/file get ($ipStore) contents] | |
| # If previousIP is not the same as currentIP, | |
| # start the update process | |
| #-------------------------------------------------- | |
| :do { | |
| :if ($previousIP = $currentIP) do={ | |
| :log error "DuckDNS-HE_DNS Updater: Previous IP is the same as the current one." | |
| :error "DuckDNS-HE_DNS Updater: Previous IP is the same as the current one." | |
| } | |
| :log warning "Started to update the DDNS services" | |
| /delay delay-time=2 | |
| :log info ("Started updating DuckDNS with IP " . $currentIP . " - Previous IP is " . $previousIP) | |
| /tool fetch mode=https \ | |
| host=($updateHost) \ | |
| url=("https://" . $updateHost . $updatePath . \ | |
| "?domains=" . $DuckDNSDomains . \ | |
| "&token=" . $DuckDNSToken . \ | |
| "&ip=" . $currentIP) \ | |
| dst-path=($resultFile) | |
| /delay delay-time=2 | |
| :local lastChange | |
| :set lastChange [/file get ($resultFile) contents] | |
| :if ($lastChange = "OK") do={ | |
| :log warning ("DuckDNS update successful with IP " . $currentIP) | |
| } else={ | |
| :log error ("Failed to update DuckDNS with new IP " . $currentIP) | |
| } | |
| /delay delay-time=2 | |
| :log warning ("Disabling 6to4 tunnel " . $HEtunnelinterface . "...") | |
| /interface 6to4 disable ($HEtunnelinterface) | |
| /delay delay-time=2 | |
| :log warning ("Updating IPv6 Tunnel " . $HEtunnelid . " Client IPv4 address to new IP " . $currentIP . "...") | |
| /tool fetch mode=https \ | |
| host=($HEupdatehost) \ | |
| url=("https://" . $HEupdatehost . $HEupdatepath . \ | |
| "?hostname=" . $HEtunnelid . \ | |
| "&myip=" . $currentIP) \ | |
| user=($HEuserid) \ | |
| password=($HEmd5pass) \ | |
| dst-path=($HEresultfile) | |
| /delay delay-time=2 | |
| :local HEres | |
| :set HEres [/file get ($HEresultfile) contents] | |
| :if ($HEres ~ "^(good)") do={ | |
| :log warning "HE DNS Update successful." | |
| } else={ | |
| :log info ($HEres) | |
| } | |
| /delay delay-time=2 | |
| :log warning ("Updating " . $HEtunnelinterface . " local-address with new IP " . $currentIP . "...") | |
| /interface 6to4 set ($HEtunnelinterface) local-address=$currentIP | |
| /delay delay-time=2 | |
| /file remove ($resultFile) | |
| /file remove ($HEresultfile) | |
| /file set ($ipStore) contents=($currentIP) | |
| /delay delay-time=2 | |
| :log warning ("Enabling 6to4 tunnel " . $HEtunnelinterface . "...") | |
| /interface 6to4 enable ($HEtunnelinterface) | |
| /delay delay-time=2 | |
| :log warning "DNS update has finished." | |
| } on-error={ | |
| :log error "DNS Updater has errored." | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment