Subscribe For Free Updates!

We'll not spam mate! We promise.

Sunday 24 November 2013

Access Mikrotik Remotely via DynamicDNS



~ Article By Syed Jahanzaib ~
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 . . .




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.
After verification of the account, Login to see your account status, by default no domain name is added,
As showed in the image below . . .

ADD DOMAIN NAME TO YOUR ACCOUNT

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 . . .

After adding your domain name, You can see your domain name in the main page,
Now its time to configure the DynamicDNS script on the Mikrotik.

CONFIGURE DDNS SCRIPT AND SCHEDULER ON MIKROTIK

Login to Mikrotik via WINBOX,
Click on NEW TERMINAL , after login, paste the following text in the TERMINAL window,
ddns-script-direct-nat script code:
Script taken from
[Preferred]
http://www.changeip.com/mikrotik/5.x.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/system script
 
add name=update-ddns policy=\
ftp,reboot,read,write,policy,test,winbox,password,sniff,sensitive,api \
source="# HomingBeacon Main Dynamic DNS Update Script\r\
\n# Written by Sam Norris, ChangeIP.com\r\
\n# 20100728 Tested on RouterOS 4.9\r\
\n# 20110511 Tested on RouterOS 5.2\r\
\n\r\
\n# Set your specific ChangeIP.com preferences here.\r\
\n:global ddnsuser \"your-userid\"\r\
\n:global ddnspass \"your-password\"\r\
\n:global ddnshost \"your-site-name\"\r\
\n# Change ddnsport to 8245 to bypass proxy.\r\
\n:local ddnsport 8245\r\
\n\r\
\n# Do not edit anything below this line.  You have been warned.\r\
\n# Abusive updates to the system will cause firewall blocks.\r\
\n\r\
\n# Please be considerate and\r\
\n# do not let this script run more than once per 3-5 minutes.\r\
\n\r\
\n:log info \"DDNS: Starting.\"\r\
\n\r\
\n# Initialize checkpoint\r\
\n:global ddnscheckpoint\r\
\n:if ([:typeof \$ddnscheckpoint] = \"time\") do={\r\
\n\t:log info (\"DDNS: Last check was \" . ([/system clock get time] - \$d\
dnscheckpoint))\r\
\n} else={\r\
\n\t:log info \"DDNS: Cannot determine checkpoint, set now.\"\r\
\n\t:global ddnscheckpoint ( [/system clock get time] - 1d )\r\
\n}\r\
\n\r\
\n# Get the current IP\r\
\n:if ([/system clock get time] - \$ddnscheckpoint > [:totime 180s] || [/s\
ystem clock get time] - \$ddnscheckpoint < [:totime 0s]) do={\r\
\n   :log info \"DDNS: Performing remote IP detection.\"\r\
\n   /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\r\
\n   :global ddnscheckpoint [/system clock get time]\r\
\n} else={\r\
\n   :log info \"DDNS: Please be considerate and wait a few seconds longer\
.\"\r\
\n   :break\r\
\n}\r\
\n\r\
\n# Parse the IP address received from fetch script.\r\
\n\t:global ddnslastip\r\
\n\t:local html [/file get \"ip.changeip.com.txt\" contents]\r\
\n\t:local ddnsip [:pick \$html ([:find \$html \"<!--IPADDR=\"] + 11) [:fi\
nd \$html \"-->\"] ]\r\
\n\r\
\n# Is it a valid IP and is it different than the last one\?\r\
\n\t:if ([:typeof [:toip \$ddnsip]] = \"ip\" AND \$ddnsip != \$ddnslastip \
) do={\r\
\n\t\t:log info \"DDNS: Sending UPDATE with \$ddnsip\"\r\
\n\t\t:log info [/tool dns-update name=\$ddnshost address=\$ddnsip key-nam\
e=\$ddnsuser key=\$ddnspass ]\r\
\n\t\t:global ddnslastip \$ddnsip\r\
\n\t} else={\r\
\n\t\t:log info \"DDNS: No update required.\"\r\
\n\t}\r\
\n}\r\
\n"
[Alternate]
http://wiki.mikrotik.com/wiki/Dynamic_DNS_Update_Script_for_dynDNS_behind_NAT
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
/system script
add name=DDNS policy=\
ftp,reboot,read,write,policy,test,winbox,password,sniff,sensitive,api \
source="# Dynamic DNS for ChangeIP.com behind NAT\
T\r\
\n# Here is where you need to set your definitions\r\
\n# Enter your check-ip.com user account id, password and domain-name your selected\r\
\n:local user \"user\"\r\
\n:local pass \"pass\"\r\
\n:local host \"your-ddns-site-name\"\r\
\n#\r\
\n:global lastwanip;\r\
\n:if ([ :typeof \$lastwanip ] = \"nothing\" ) do={ :global lastwanip 0.0.0.0 };\r\
\n:local wanip [:resolve \$host];\r\
\n:if ( \$wanip != \$lastwanip ) do={\r\
\n\t/tool fetch mode=http address=\"checkip.dyndns.org\" src-path=\"/\" dst-path=\"/dyndns.checkip.html\"\r\
\n\t:local result [/file get dyndns.checkip.html contents]\r\
\n\t:local resultLen [:len \$result]\r\
\n\t:local startLoc [:find \$result \": \" -1]\r\
\n\t:set startLoc (\$startLoc + 2)\r\
\n\t:local endLoc [:find \$result \"</body>\" -1]\r\
\n\t:local currentIP [:pick \$result \$startLoc \$endLoc]\r\
\n\t:set lastwanip \$currentIP;\r\
\n\t:put [/tool dns-update name=\$host address=\$currentIP key-name=\$user key=\$pass ]\r\
\n}"

→ NOTE:
Make sure you change the user id + password + host name  to match the same you have entered at the time of registration.

Also Make sure that you change the INTERFACE setting.

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.
1
2
3
/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 EDIT
As showed in the image below . . .

Now it will show you the updated Record.
As showed in the image below . . .

Everything is ready and in place.

TEST
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,
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. Also Make sure that you change the INTERFACE setting.

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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
:global ddnsuser "user-aacable"
:global ddnspass "passwd"
:global ddnshost "full hostname"
:global ddnsinterface "ether1"
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 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 SCRIPT
.
Regard’s
Syed Jahanzaib

Please Give Us Your 1 Minute In Sharing This Post!
SOCIALIZE IT → , ,
FOLLOW US →
SHARE IT →

0 comments:

Post a Comment

 
".