Saturday, April 27, 2013

Mikrotik DynDNS configuration how-to

,

Mikrotik is so powerful but unfortunately it doesn't support dynamic DNS service without using a script...
I had to create a full new config before some days so i was searching for the DDNS option but couldn`t find a thing.

So after our dear friend google there seems to be a script to bypass this limit:




# Set needed variables
:local username "yourusername"
:local password "yourpassword"
:local hostname "yourdyndnsorgname.dyndns.org"
:global systemname [/system identity get name]

:if ($systemname = "Site1" ) do= {
:set hostname "yourdomain1.dyndns.org"
}
:if ($systemname = "Site2" ) do= {
:set hostname "yourdomain2.dyndns.org"
}
:if ($systemname = "Site3" ) do= {
:set hostname "yourdomain3.dyndns.org"
}

:global dyndnsForce
:global previousIP

# print some debug info
:log info ("UpdateDynDNS: username = $username")
:log info ("UpdateDynDNS: password = $password")
:log info ("UpdateDynDNS: hostname = $hostname")
:log info ("UpdateDynDNS: previousIP = $previousIP")

# get the current IP address from the internet (in case of double-nat)
/tool fetch mode=http address="checkip.dyndns.org" src-path="/" dst-path="/dyndns.checkip.html"
:local result [/file get dyndns.checkip.html contents]

# parse the current IP result
:local resultLen [:len $result]
:local startLoc [:find $result ": " -1]
:set startLoc ($startLoc + 2)
:local endLoc [:find $result "" -1]
:local currentIP [:pick $result $startLoc $endLoc]
:log info "UpdateDynDNS: currentIP = $currentIP"

# Remove the # on next line to force an update every single time - useful for debugging,
# but you could end up getting blacklisted by DynDNS!

#:set dyndnsForce true

# Determine if dyndns update is needed
# more dyndns updater request details available at http://www.dyndns.com/developers/specs/syntax.html
:if (($currentIP != $previousIP) || ($dyndnsForce = true)) do={
:set dyndnsForce false
:set previousIP $currentIP
/tool fetch user=$username password=$password mode=http address="members.dyndns.org" \
src-path="/nic/update?hostname=$hostname&myip=$currentIP" dst-path="/dyndns.txt"
:local result [/file get dyndns.txt contents]
:log info ("UpdateDynDNS: Dyndns update needed")
:log info ("UpdateDynDNS: Dyndns Update Result: ".$result)
:put ("Dyndns Update Result: ".$result)
} else={
:log info ("UpdateDynDNS: No dyndns update needed")
}
After we save the script, we have to add a new scheduled job in the event which we just made with an interval of.. let's say... 15 minutes?

Now you can 'remote access' easily.

2 people replied:

  1. what does it mean site1, site2, site3?

    ReplyDelete
    Replies
    1. Site1, Site2, Site3 is basically the name of the device which you are actually trying to configure. If you just need to update one device only, change Site1 only and disable the rest, like below:

      :if ($systemname = "YourDeviceNameHere") do = {
      :set hostname "YourDDNSnameHere"
      }
      #:if ($systemname = "Site2" ) do= {
      #:set hostname "yourdomain2.dyndns.org"
      #}
      #:if ($systemname = "Site3" ) do= {
      #:set hostname "yourdomain3.dyndns.org"
      #}

      If you are actually have more scripts to play with, lets say:
      You have a dhcp enabled network so everytime mikrotik reboots it chooses another ip, this ip also makes the device to change name (by using another script) so you can enable different DDNS service.

      I just put it there to show the method, Site1,2,3 here means Location.
      Thanks for reading.

      Delete