Mikrotik

Mikrotik router configuration with DynDNS.it

BrandMikrotik
ModelAll Models
Providerhttps://dyndns.it/
Official Websitehttps://mikrotik.com/
Configuration to useProvided Script

DynDNS.it Mikrotik configuration

You can configure a Mikrotik router with dyndns.it service using the following script and setting a cron job that calls the script.

Step 1

Connect to the router using a Terminal (like puTTY) or Winbox software.

Step 2

Substitute in the following script your values for

  • YOURUSERNAME,
  • YOURPASSWORD,
  • YOURHOSTNAME

getting the values from your dyndns.it account.

# Set the following values from your dyndns.it account https://dyndns.it/host-management/
	:local username "YOURUSERNAME"
	:local password "YOURPASSWORD"
	:local hostname "YOURHOSTNAME"

## no need to modify after this line.

	: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.it" src-path="/" dst-path="/dyndns.checkip.html"
	:delay 1
	: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 "</body>" -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

	:if (($currentIP != $previousIP) || ($dyndnsForce = true)) do={
		:set dyndnsForce false
		:set previousIP $currentIP
		:log info "$currentIP or $previousIP"
		/tool fetch user=$username password=$password mode=http address="update.dyndns.it" \
		src-path="nic/update?hostname=$hostname&myip=$currentIP" \
		dst-path="/dyndns.txt"
		:delay 1
		: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")
	}

If you use Winbox

Paste the script using the menu System->Script

If you use a Terminal

You should first create a new system script using the following commands:

/system script
add name=DDNS
policy=read,write,test

and then paste the script.

Step 3

Add a scheduler that calls the script every 5 minutes to send an update when IP changes.

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

Conclusion

After 5 minutes the Mikrotik updates the hostname and keeps it updated when the IP changes. You can check the updates sent to DDNS in you client area of the dyndns.it website.