Mikrotik Mikrotik

You can use our dynamic DNS service with Mikrotik routers with these simple steps.

First you need to copy paste the following script to a terminal window (change the values in bold). Note that you need to escape the special characters such as “ and $ and also to insert the \r\ and \n line control characters.

/system script
add name=Dynu
policy=read,write,test
:global ddnsuser "your_Dynu_username"
:global ddnspass "your_Dynu_password"
:global theinterface "WAN_Interface_Name"
:global ddnshost "your_Dynu_hostname"
:global ipddns [:resolve $ddnshost];
:global ipfresh [ /ip address get [/ip address find interface=$theinterface ] address ]
:if ([ :typeof $ipfresh ] = nil ) do={
:log info ("DynuDDNS: No IP address on $theinterface .")
} else={
: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 ("DynuDDNS: IP-Dynu = $ipddns")
:log info ("DynuDDNS: IP-Fresh = $ipfresh")
:log info "DynuDDNS: Update IP needed, Sending UPDATE...!"
:global str "/nic/update?hostname=$ddnshost&myip=$ipfresh"
/tool fetch address=api.dynu.com src-path=$str mode=http user=$ddnsuser password=$ddnspass dst-path=("/Dynu.".$ddnshost)
:delay 1
:global str [/file find name="Dynu.$ddnshost"];
/file remove $str
:global ipddns $ipfresh
:log info "DynuDDNS: IP updated to $ipfresh!"
} else={
:log info "DynuDDNS: dont need changes";
} }

You can also create the script using WinBox by going to the menu System then Script. Open a dialog box pop-up window into which you can directly paste the content in the source section from the above script.

In case of double NAT, you may use this script instead of the one above to detect IP address from the internet. Note that you need to escape the special characters such as “ and $ and also to insert the \r\ and \n line control characters.

# get the current IP address from the internet
/tool fetch mode=http address="checkip.dynu.com" src-path="/"
dst-path="/dynu.checkip.html"
:local result [/file get dynu.checkip.html contents]

# parse the current IP result
:local resultLen [:len $result]
:local startLoc [:find $result ": " -1]
:set startLoc ($startLoc + 2)
:local currentIP [:pick $result $startLoc $resultLen]
:global ddnsuser your_Dynu_username
:global ddnspass "your_Dynu_password"
:global ddnshost "your_Dynu_hostname"
:global ipddns [:resolve $ddnshost];

#:global ipddns

:if ($ipddns != $currentIP) do={
:log info ("DynuDDNS: IP-Dynu = $ipddns")
:log info ("DynuDDNS: IP-Fresh = $currentIP")
:log info "DynuDDNS: Update IP needed, Sending UPDATE...!"
:global str "/nic/update?hostname=$ddnshost&myip=$currentIP"
:log info "currentIP is $currentIP"
/tool fetch address=api.dynu.com src-path=$str mode=http user=$ddnsuser password=$ddnspass
dst-path=("/Dynu.".$ddnshost)
:delay 1
:global str [/file find name="Dynu.$ddnshost"];
/file remove $str
:global ipddns $currentIP
:log info "DynuDDNS: IP updated to $currentIP!"
} else={
:log info "DynuDDNS: No change needed";
}
You can verify that the script is added successfully by going to System then Scripts.

After verifying that the script is updating IP for your hostname properly, you can add a scheduler entry to run the script every 5 minutes.

/system scheduler add comment="Update Dynu DDNS" interval=5m name=ddns_sheduller \
on-event="/system script run Dynu\r\n" policy=read,write,test start-time=startup
Loading...