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

 

March 30, 2012

Access Mikrotik Remotely via DynamicDNS


~ Article By Syed Jahanzaib ~
Last Updated : 28th January, 2014

If you have Mikrotik Server with DSL connection with dynamic ip , which changes on every reboot / reset, and you want to access mikrotik from the Internet then you can use changeip.com (DynamicDNS) service, using this service, you don’t have to query the DSL IP every time, all you have to remember the domain name that you register on the site, and rest of things will be handled by the DDNS script configured on the Mikrotik.

Following is the the scenario:

…    INTERNET            >>    DSL MODEM          >>> MIKROTIK
User with WINBox            (with dynamic ip)

DSL MODE IP              =   192.168.30.1
MIKROTIK WAN IP =  192.168.30.5

Ok Lets Start.

CONFIGURE DSL MODEM TO ENABLE PORT FORWARDING

First open you DSL Modem page to enable PORT Forwarding (from DSL Modem to Mikrotik box)
Now add Port Forwarding rule

WINBOX uses TCP Port 8291
As showed in the image below . . .

Now Click on SAVE / APPLY

.

CREATE NEW ACCOUNT ON CHANGEIP.COM

Now Open http://www.changeip.com in your browser and register new account.
As showed in the image below . . .

1


2

Now it will send you an confirmation email, Open your mail box, and click on the link it have send you to activate your account.

ADD DOMAIN NAME TO YOUR ACCOUNT

Login to see your account , by default no domain name is added, Just click on DNS MANAGER to add your required name


As showed in the image below . . .

3

Now you have to add your own desired name and its domain.  You can select various domains from the drop down list.
As showed in the image below . . .

4

After adding your domain name, You can see your domain name in the main page,

As showed in the image below . . .

5

Now its time to configure the DynamicDNS script on the Mikrotik.

CONFIGURE DDNS SCRIPT AND SCHEDULER ON MIKROTIK

Login to Mikrotik via WINBOX,
Goto Scripts / and new script.

Paste following into the script

SCRIPTNAME : ddns-script-behind-dsl-modem

Script Code tested with MIKROTIK version 6.xx

# Set your specific ChangeIP.com preferences here.
:global ddnsuser "CHANGEIP USERNAME"
:global ddnspass "PASSWORD"
:global ddnshost "YOUR-FREE-DDNS SITE NAME"
# Change ddnsport to 8245 to bypass proxy.
:local ddnsport 80
 
# Do not edit anything below this line. You have been warned.
# Abusive updates to the system will cause firewall blocks.
 
# Please be considerate and
# do not let this script run more than once per 3-5 minutes.
 
:log info "DDNS: Starting."
 
# Initialize checkpoint
:global ddnscheckpoint
:if ([:typeof $ddnscheckpoint] = "time") do={
 :log info ("DDNS: Last check was " . ([/system clock get time] - $ddnscheckpoint))
} else={
 :log info "DDNS: Cannot determine checkpoint, set now."
 :global ddnscheckpoint ( [/system clock get time] - 1d )
}
 
# Get the current IP
:if ([/system clock get time] - $ddnscheckpoint > [:totime 180s] || [/system clock get time] - $ddnscheckpoint < [:totime 0s]) do={
 :log info "DDNS: Performing remote IP detection."
 /tool fetch address="ip.changeip.com" host="ip.changeip.com" src-path=("/?" . [/int eth get 0 mac-address ]) dst-path="ip.changeip.com.txt" mode=http port=$ddnsport
 :global ddnscheckpoint [/system clock get time]
} else={
 :log info "DDNS: Please be considerate and wait a few seconds longer."
 :break
}
 
# Parse the IP address received from fetch script.
 :global ddnslastip
 :local html [/file get "ip.changeip.com.txt" contents]
 :local ddnsip [:pick $html ([:find $html "<!--IPADDR="] + 11) [:find $html "-->"] ]
 
# Is it a valid IP and is it different than the last one?
 :if ([:typeof [:toip $ddnsip]] = "ip" AND $ddnsip != $ddnslastip ) do={
 :log info "DDNS: Sending UPDATE with $ddnsip"
 :log info [/tool dns-update name=$ddnshost address=$ddnsip key-name=$ddnsuser key=$ddnspass ]
 :global ddnslastip $ddnsip
 } else={
 :log info "DDNS: No update required."
 }
}

 

Script for Mikrotik ver 6.x  [CODE FOR PPP DIALER WHERE INTERNET IS DIRECTLY CONNECTED ON MIKROTIK

:local ddnsuser "USERNAME"
:local ddnspass "PASSWORD"
:local ddnshost "CHANGEIP_YOUR_WEB_NAME"
:local ddnsinterface "WAN_INTERFACE"
:global ddnslastip
:global ddnsip [ /ip address get [find interface=$ddnsinterface disabled=no] address ]
:if ([ :typeof $ddnslastip ] = nil ) do={ :global ddnslastip 0.0.0.0/0 }

:if ([ :typeof $ddnsip ] = nil ) do={
:log info ("DDNS: No ip address present on " . $ddnsinterface . ", please check.") } else={
:if ($ddnsip != $ddnslastip) do={
:log info "DDNS: Sending UPDATE!"
:log info [ /tool dns-update name=$ddnshost address=[:pick $ddnsip 0 [:find $ddnsip "/"] ] key-name=$ddnsuser key=$ddnspass ]
:global ddnslastip $ddnsip } else={
:log info "DDNS: No change" }
}

As showed in the image below . . .

To verify that the script is pasted and working properly,
Goto SYSTEMS   >  SCRIPTS , and double click on the DDNS to view its contents, Just verify it everything is in place.
As showed in the image below . . .

Okay, as the script is in place, its time to Execute the script so it can update the current WAN IP to your DynamicDNS account.

As showed in the image below . . .

As you can see that the script have updated the record to dynamicDNS account, its time to verify it.

ADDING SCHEDULER (So it can update record after every 5 minutes)

Also add this in scheduler so it can run after every 5 minutes, you can adjust it as per your requirement.

/system scheduler
add disabled=no interval=5m name="Run DDNS every 5 minutes" on-event=update-ddns policy=\
ftp,reboot,read,write,policy,test,winbox,password,sniff,sensitive,api start-date=mar/30/2012 start-time=14:24:00

VERIFY UPDATED IP RECORD ON CHANGEIP.COM

Go back to changeip.com and see your domain name record by clicking on

View Recent DDNS Updates

As showed in the image below . . .

6

Now it will show you the updated Record.

As showed in the image below . . .

7

Everything is now ready and in place.

TEST TIME !!!


CONNECT TO YOUR MIKROTIK FROM INTERNET

Its time to hit the road. From any other Remote PC with separate internet connection, try to ping your domain name and try to connect it via winbox or try open it in your browser,

NTOE: In the image below, I used another domain name, as at the moment I only had old image , so ignore the name, just understand the logic

As showed in the images below . . .

.

.

TIP: Script for DSL Modem in BRIDGE Mode …

If you have DSL Modem configured in BRIDGE mode, and you are dialing via Mikrotik PPPoE Client Dialer, then use the following Script.

NOTE:

Make sure you change the user id + password + host name to match the same you have entered at the time of registration.
Most importantly donot forget to change the interface name (for example pppoe-out1 or likewise

Following Script have been taken from
http://wiki.mikrotik.com/wiki/Dynamic_DNS_Update_Script_for_ChangeIP.com

ddns-script-modem-in-pppoe-mode- code.

:global ddnsuser "user-aacable"
:global ddnspass "passwd"
:global ddnshost "full hostname"
:global ddnsinterface "pppoe-out1"
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# END OF USER DEFINED CONFIGURATION
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

:global ddnssystem ("mt-" . [/system package get [/system package find name=system] version] )
:global ddnsip [ /ip address get [/ip address find interface=$ddnsinterface] address ]
:global ddnslastip
:if ([:len [/interface find name=$ddnsinterface]] = 0 ) do={ :log info "DDNS: No interface named $ddnsinterface, please check configuration." }
:if ([ :typeof $ddnslastip ] = "nothing" ) do={ :global ddnslastip 0.0.0.0/0 }
:if ([ :typeof $ddnsip ] = "nothing" ) do={
:log info ("DDNS: No ip address present on " . $ddnsinterface . ", please check.")
} else={
:if ($ddnsip != $ddnslastip) do={
:log info "DDNS: Sending UPDATE!"
:log info [ :put [/tool dns-update name=$ddnshost address=[:pick $ddnsip 0 [:find $ddnsip "/"] ] key-name=$ddnsuser key=$ddnspass ] ]
:global ddnslastip $ddnsip
} else={
:log info "DDNS: No changes necessary."
}
}
# END OF THE SCRIPT

.

Regard’s
Syed Jahanzaib

%d bloggers like this: