Skip to content

Instantly share code, notes, and snippets.

@aaronk6
Created April 26, 2023 20:55
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?
RouterOS dynamic DNS update script for he.net
# 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