RouterOS dynamic DNS update script for he.net
This file contains 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
# RouterOS dynamic DNS Update Script for he.net | |
# ============================================= | |
# | |
# Add your hostname and password below and adjust the waninterface | |
# if necessary. Then set up a scheduler to run this script periodically. | |
# | |
# CLI example: | |
# | |
# /system scheduler add interval=5m name=ddns_update on-event="/system script run ddns_henet" policy=ftp,reboot,read,write,policy,test,password,sniff,sensitive,romon start-time=startup | |
# | |
# make global to preserve IP address across script executions | |
:global ipddns | |
:local ddnshost "myhost.example.com" | |
:local ddnspass "MY_SECURE_PASSWORD" | |
:local waninterface "pppoe-out1" | |
:local url "https://dyn.dns.he.net/nic/update" | |
# get current IP address from WAN interface | |
:local ipfresh [/ip address get [find where interface=$waninterface] value-name=address] | |
:if ([ :typeof $ipfresh ] = nil ) do={ | |
:log error ("DDNS: No IP address on $waninterface") | |
return | |
} | |
# extract the portion of the IP address before the last / character | |
:for i from=( [:len $ipfresh] - 1) to=0 do={ | |
:if ( [:pick $ipfresh $i] = "/") do={ | |
:set ipfresh [:pick $ipfresh 0 $i]; | |
} | |
} | |
:if ($ipddns = $ipfresh) do={ | |
:log info ("DDNS: IP address is already up to date") | |
return | |
} | |
:log info ("DDNS: IP address has changed from $ipddns to $ipfresh, sending update...") | |
:local body "hostname=$ddnshost&password=$ddnspass&myip=$ipfresh" | |
:local result [/tool fetch mode=https output=user url=$url http-method=post http-data=$body as-value] | |
:if ($result->"status" != "finished") do={ | |
:log error ("DDNS: Failed to send update") | |
return | |
} | |
:log info ("Response from server: " . $result->"data") | |
:if ([:find ($result->"data") "good"] = 0) or ([:find ($result->"data") "nochg"] = 0) do={ | |
:set ipddns $ipfresh | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment