Syed Jahanzaib – Personal Blog to Share Knowledge !

December 1, 2015

DYNU Script for Mikrotik (free dynamic dns script)

Filed under: Mikrotik Related — Tags: , , , — Syed Jahanzaib / Pinochio~:) @ 4:04 PM

logo

An alternate to changeip.com (free ddns) .

From few days , we were having issues in updating our mikrotik/linux base machines dynamic ip to free DDNS service like changeip.com, therefore today I tested DYNU as an alternate and it worked very good. we can use it as backup or primary ddns service for our mikrotik routers.

First create account at http://www.dynu.com, add your required host. and then add script and schedule it to run after every 5  (or xx) minutes

Mikrotik Script for Dynu ddns.

The below script is working 100% on Mikrotik 5.x and 6.x versions.
[Tested as of now / 1st-DEC-2015]

(You can copy paste following code in mikrotik by going to SYSTEMS / SCRIPTS / CLICK ON + sign to add script, and paste following.

NOTE: MAKE SURE YOU CHANGE THE REQUIRED FIELDS BEFORE DEPLOYMENT.


SCRIPT NAME: “DYNU

1- SCRIPT FOR DSL MODEM IN BRIDGE MODE, MIKROTIK HAVE THE PUBLIC IP ADDRESS AT WAN INTERFACE.

(FOR BRIDGE MODE, PUBLIC IP IS DIRECTLY ON MIKROTIK WAN INTERFACE)

NOTE: If you have multiple IP address configured on WAN interface, it will give you error of “Invalid internal item number

##############################################
# DYNU SCRIPT for Mikrotik , DSL Modem BRIDGE MODE,
# Wan dialing is done by mikrotik so public ip is on mikrotik directly
# CHANGE THE FOLLOWING ACCORDING TO YOUR CREDENTIALS
# Syed Jahanzaib / aacable at hotmail dot com
# ####################################################
:global ddnsuser DYNU_USER
:global ddnspass "DYNU_PASSWORD"
:global theinterface "WAN_INTERFACE_NAME"
:global ddnshost "YOUR_DYNU_HOSTNAME.dynu.com"
#### Donot change below this line ####
: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";
} }

Now run it and watch the results.


3


 

2- SECOND SCRIPT FOR DSL MODE, MIKROTIK IS CONNECTED TO INTERNET BEHIND DSL. (NAT)

 


########################################
# DYNU DNS Script for Mikrotik Behind DSL
# 14th JAN-2015
# Syed Jahanzaib / aacable at hotmail dot com
# ####################################################

# 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 warning "UpdateDynDNS: currentIP = $currentIP"

######################################################

:global ddnsuser DNSUSERNAME
:global ddnspass "DNSPASSWORD"
:global ddnshost "YOURHOST.dynu.com"
:global ipddns [:resolve $ddnshost];
#:global ipddns 1.2.3.4

: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"
/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: dont need changes";
} }

 

To  scheudle it to run every 5 minutes , use


/system scheduler
add disabled=no interval=5m name=dynu on-event=dynu policy=\
ftp,reboot,read,write,policy,test,winbox,password,sniff,sensitive,api start-date=jan/01/1970 \
start-time=00:00:00


 

Regard’s

Syed Jahanzaib

 

11 Comments »

  1. It works as per in the script, thankkkk youuuu ! I have question off this topic. Do mikrotik have “Ip sla” ?
    I need script for BGP failover to happen as how we can do in Cisco. anyone can help or any solution already available ? paste the link here, I can have a look at it. THANK YOU AGAIN!

    Like

    Comment by Saran — December 2, 2015 @ 8:51 PM

  2. Aoa! script updating my local IP (192.168.x.x) to DYNU DDNS. please help….thanx in adv

    Like

    Comment by Asif Siddiqui — December 30, 2015 @ 10:31 PM

  3. need some help anyone here?

    Like

    Comment by Asif Siddiqui — January 2, 2016 @ 12:20 AM

  4. Aoa! script updating my local IP (192.168.x.x) to DYNU DDNS. please help

    Like

    Comment by Asif Siddiqui — January 12, 2016 @ 5:35 PM

    • I have updated the post. 2 scripts are now posted. one for Mikrotik with direct public ip (like modem is in bridge mode and mikrotik dialing the net via pppoe-out1)
      second script is for mikrotik which is behind dsl NAT. use that one for your mikrotik and do let me know the results.

      Like

      Comment by Syed Jahanzaib / Pinochio~:) — January 14, 2016 @ 12:07 PM

  5. does not work on version 6
    thanks

    Like

    Comment by Mohsen Ahmed — January 16, 2016 @ 4:20 AM

  6. scheduler not working on yours command

    this command working fine thankx
    /system scheduler add name=dynu interval=00:05 on-event=”/system script run dynu\r\n”

    Like

    Comment by fasih ur rehman — December 15, 2016 @ 12:05 AM

  7. Dear,
    I’tried your second script for a device behind NAT, but it didn’t work. The OS version is 6.35, the device is hAP.
    Do I need to do some port forwarding on the router (ADSL modem), if yes, what port do I need to open?

    Best regards, Paolo

    Like

    Comment by Paolo Zovic — February 19, 2017 @ 6:56 PM

  8. work good

    Like

    Comment by Mo Abdali — March 5, 2017 @ 6:44 PM


RSS feed for comments on this post. TrackBack URI

Leave a comment