Syed Jahanzaib – Personal Blog to Share Knowledge !

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