Dynamic DNS Update Script for DNSoMatic.com - MikroTik Script RouterOS
This script is a solution made of others solutions (nothing new). Much of this was adapted from the deprecated version of Dynamic DNS Update Script for DNSoMatic.com behind NAT.
The goal is to update your account on DNSoMatic.com. The main advantage of this solution is that DNSoMatic offers the possibility of propagating DNS updates to thirth party DNSlike systems like OpenDNS, DynDNS, Change IP and other 27 more.
Note: The script below is RouterOS 5.14 & 6.6 Tested!
The following permissions are required for this script to run:
- write
- test
- read
- policy (for ROS 6.0+)
:local maticuser "dnsomatic-username"
:local maticpass "dnsomatic-password"
:local matichost "hostname1,hostname2"
:local inetinterface "ether1-gateway"
:global previousIP;
:if ([/interface get $inetinterface value-name=running]) do={
:local currentIP [/ip address get [find interface="$inetinterface" disabled=no] address];
:for i from=( [:len $currentIP] - 1) to=0 do={
:if ( [:pick $currentIP $i] = "/") do={
:set currentIP [:pick $currentIP 0 $i]
}
}
:if ($currentIP != $previousIP) do={
:log info "DNSoMatic: Update needed"
:set previousIP $currentIP
:local url "http://updates.dnsomatic.com/nic/update\3Fmyip=$currentIP&wildcard=NOCHG&mx=NOCHG&backmx=NOCHG"
:local matichostarray;
:set matichostarray [:toarray $matichost];
:foreach host in=$matichostarray do={
:log info "DNSoMatic: Sending update for $host"
/tool fetch url=($url . "&hostname=$host") user=$maticuser password=$maticpass mode=http dst-path=("dnsomaticupdate-" . $host . ".txt")
:log info "DNSoMatic: Host $host updated on DNSoMatic with IP $currentIP"
}
} else={
:log info "DNSoMatic: Previous IP $previousIP and current IP equal, no update need"
}
} else={
:log info "DNSoMatic: $inetinterface is not currently running, so therefore will not update."
}
This will also need you to configure scheduler entry for periodical runs (maybe every minute or so). You will probably want a second scheduler event run this script upon RouterOS startup.
If for whatever reason the update fails, the script will not update DNSoMatic until the IP address changes again. This is rare, but could happen. It would be recommended to set up a third scheduler with longer intervals (maybe 1 hour) to run a script with the following code:
:global previousIP;
:set previousIP ""
:log info "Cleared previousIP to force DNS-O-Matic update on next run."
The following permissions are required for this script to run:
- write
- test (for ROS 6.0+)
- read (for ROS 6.0+)
- policy (for ROS 6.0+)
It will silently fail if it doesn't have this permission.
hjoelr 10/nov/2013
Credit: https://wiki.mikrotik.com