Dynamic DNS Update Script for DNSoMatic.com behind NAT - MikroTik Script RouterOS
This script is a solution made of others solutions (nothing new). The goal is to update your account on DNSoMatic.com. The main advantage on 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.
If you're looking for a script that will run on a MikroTik that is not behind a NAT, use this one: Dynamic DNS Update Script for DNSoMatic.com
Current Version
This new version is tested and working on RouterOS Version 5.14 & 6.6. Here are some of the features this script supports.
Works behind a NAT
Supports multiple DNS-O-Matic host update
The first time this script runs, it will not go through a full update to DNS-O-Matic. Subsequent runs, however, will work. This appears to be a limitation in RouterOS where it will not find a file that was created in the same script instance.
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"
:global previousIP;
:log info "Fetching current IP"
/tool fetch url="http://myip.dnsomatic.com/" mode=http dst-path=mypublicip.txt
:local currentIP [/file get mypublicip.txt contents]
:log info "Fetched current IP as $currentIP"
: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"
}
This will also need you to configure scheduler entry for periodical runs (maybe every 5 minutes 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
Deprecated Version
Thanks all for your solutions... with this solution DNS filtering option from OpenDNS gets open without client software.
Note: The script below is RouterOS 4.2 Tested! It should also work on 3.x series RouterOS.
:global maticuser "user"
:global maticpass "password"
:global matichost "Yourhost"
:global previousIP
:log info "DNSoMatic: Updating dynamic IP on DNS for host $matichost"
:log info "DNSoMatic: User $maticuser y Pass $maticpass"
:log info "DNSoMatic: Last IP $previousIP"
/tool fetch mode=http address="checkip.dyndns.org" src-path="/" dst-path="/dyndns.checkip.html"
:local result [/file get dyndns.checkip.html contents]
:local resultLen [:len $result]
:local startLoc [:find $result ": " -1]
:set startLoc ($startLoc + 2)
:local endLoc [:find $result "" -1]
:global currentIP [:pick $result $startLoc $endLoc]
:log info "DNSoMatic: IP actual $currentIP"
:local str "/nic/update?hostname=$matichost&myip=$currentIP&wildcard=NOCHG&mx=NOCHG&backmx=NOCHG"
:if ($currentIP != $previousIP) do={
:log info "DNSoMatic: Update need"
:set previousIP $currentIP
:log info "DNSoMatic: Sending update $currentIP"
:log info [ :put [/tool fetch host=MT user=$maticuser password=$maticpass mode=http address="updates.dnsomatic.com" src-path=$str dst-path=$matichost]]
:log info "DNSoMatic: Host $matichost updated on DNSoMatic with IP $currentIP"
} else={
:log info "DNSoMatic: Previous IP $previousIP and current $currentIP equal, no update need"
}
This will also need you to configure scheduler entry for periodical runs.
gwicon 28/nov/2009
Credit: https://wiki.mikrotik.com