Sometimes you have a customer who has more than one internet connection and you want to setup Mikrotik Cloud to update the DDNS name to point to a specific IP and “failover” to another IP should the primary link fail. For example your WEB and VPN services are hosted on different Internet links, with fail-over, should a link go down. Lets say that by default your Cloud DDNS is updating with the IP address of the WEB internet connection and you want to change that to use the VPN Internet connection.

This is possible by changing the default route to Mikrotik Cloud DDNS service.

First, a brief description of how, I think, Mikrotik Cloud DDNS works.

When IP/Cloud is enabled, the router will connect to cloud.mikrotik.com on udp port 15252. The DDNS <mikrotikserial>.sn.mynetname.net will be updated with the IP address of the source connection.

This is why we need to define a default route to cloud.mikrotik.com from the desired internet connection.

Update:
I found that the wiki now explains the process for updating DDNS and time. You can find the relevant section here. For some reason it does not mention the port and URL used.

The below script will create multiple default routes to the DDNS url (cloud.mikrotik.com) and will give priority to one of the connections(distance 90) over the other (distance 91).

Make sure the distance values are unique. The script will delete any entries with distance 90 and 91. 

DNS settings need to be configured aswell.

1
 /ip dns set servers=server1,server2,...

Static IPs are being assumed in this example. Usually the route to 0.0.0.0 is added manually in the IP/Route list when static IPs are provide by the ISP so the gateway IP is known.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#variable to store the IP address of the Primary gateway you wish to use to update the MikrotikCloud DDNS
:local PrimaryGW "<Primary Gateway IP>"
 
#variable to store the IP address of the Secondary gateway you wish to use to update the CloudIP DDNS
:local SecondaryGW "<Secondary Gateway IP>"
 
#The url of the Mikrotik Cloud DDNS
:local cloudIPDDNS "cloud.mikrotik.com"
 
#resolve the ip address
:resolve $cloudIPDDNS
 
#Delete any routes that already exist with distance 90 and 91
/ip route remove [find distance="90"]
/ip route remove [find distance="91"]
 
#Add the new default routes to the Mikrotik Cloud DDNS
:foreach aRecord in=[/ip dns cache all find where (name=$cloudIPDDNS && type=”A”)] do={
/ip route add dst-address=[/ip dns cache all get $aRecord data] gateway=$PrimaryGW distance=”90″;
}
 
:foreach aRecord in=[/ip dns cache all find where (name=$cloudIPDDNS && type=”A”)] do={
/ip route add dst-address=[/ip dns cache all get $aRecord data] gateway=$SecondaryGW distance=91;
}

If you have a dynamic ip address, you can use the below lines to change the script to get the current gateway address from ISPs with PPPoE or DHCP client interfaces.

For Example, if the Primary is a PPPoE internet connection, then change:

1
:local PrimaryGW "<Primary Gateway IP>"

to

1
:local PrimaryGW [/ip address get[find interface=<PPPoE-Client Name>] network]

<PPPoE-Client Name> should be the name of the PPPoE client connection.

If the Primary internet connection acquires the ip address with the help of a DHCP client, you can change the below line from

1
:local PrimaryGW "<Primary Gateway IP>"

to

1
:local PrimaryGW [/ip dhcp-client get[find interface=<DHCP-Client Interface>] gateway]

<DHCP-Client Interface> should be the name of the interface where the DHCP-Client is running; i.e where the modem is connected.

Note: The scripts above are ready to be copied and pasted in winbox. If I find it necessary, I will update the post with the syntax to be applied from Mikrotik Terminal window.

The screenshot below shows a Route list before the script is executed. As you can see there is a PPPoE connection and static IP connection(ether3)

This screenshot shows the Route List after the script is executed.Here you can see the routes have been created with the primary route being the ether3 and the secondary route is the PPPoE connection. The ip address 81.198.87.240 is resolved from cloud.mikrotik.com

Please note that now cloud.mikrotik.com resolves to multiple ips and the screenshot below does not reflect this. On the otherhand, the script has been updated to cater for this change.

If you check the IP/Cloud dialogue box, you should find that the IP address used is that of the internet connection you set as primary….Unless the link is down:)

Should you wish to make the script force and update of the DDNS, you can add the below line at the end of the script

1
/ip cloud force-update

The above examples should be of help to come up with a proper script should you have a mixture of static and dynamic internet connections.

If you need any clarifications leave a comment below.

If you found it useful please leave a comment too 🙂

By Brian Farrugia

I am the author of Phy2Vir.com. More info can be found on the about page.

6 thought on “Mikrotik IP/Cloud and multiple ISPs”
  1. nslookup showed me two ips for cloud.mikrotik.com so i made mangle rule for them to force cloud use my pppoe connection insted of random one from 3 isps. That does not work for me =(

    1. Hi,
      thanks for commenting. Yes I noticed this recently. After reading this Mikrotik Forum post I made the following modification.

      #Add the new default routes to the Mikrotik Cloud DDNS IPs
      :foreach aRecord in=[/ip dns cache all find where (name=$cloudIPDDNS && type=”A”)] do={
      ip route add dst-address=[/ip dns cache all get $aRecord data] gateway=$PrimaryGW distance=”90″;
      }

      :foreach aRecord in=[/ip dns cache all find where (name=$cloudIPDDNS && type=”A”)] do={
      ip route add dst-address=[/ip dns cache all get $aRecord data] gateway=$SecondaryGW distance=91;
      }

      Please note that the DNS settings need to be configured on the mikrotik i.e /ip dns set servers=dns1,dns2,…

      Let me know if this helps 🙂

  2. can u tell me why when i run the script in terminal is runs ok.
    when i import it in scripts and run it i get the error

    expected type value (line 21 column 78)

  3. do={
    ip route add dst-address=[/ip dns cache all get $aRecord data] gateway=$SecondaryGW distance=91;

    works better with /

    do={
    /ip route add dst-address=[/ip dns cache all get $aRecord data] gateway=$SecondaryGW distance=91;

    1. Hi, thanks for pointing it out. I will update the script. Funny though as on my router it ran without issues.
      Thanks for leaving a comment. Hope you find the script useful.

  4. also this character u r using made me problems until i figure out.
    && type=”A”

    for me is && type=”A”

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.