Dynamic DNS on your Mikrotik Router

Deploying a router in a remote location means that you must support it with a VPN etc. You’ll need to have Dynamic DNS on your Mikrotik router in order to easily connect. Dynamic DNS is a service that many registrars offer that allows you to setup a system to update the public IP address of the router so that you can connect to it using a friendly name.

For this example, I’m going to use my registrar, Directnic, however, if your registrar offers Dynamic DNS, it probably works in a similar way, you’ll just have to follow the instructions on their site.

Typical Mikrotik Site Setup

Above is a drawing of a typical simple setup where you may have a Mikrotik router hooked up to a cable modem at a remote location. The modem gets a public IP address from the Internet Service Provider (ISP) when it is connected. These IP addresses are usually not permanent and can change for various reasons. This makes it difficult to make sure you can connect to the router when you need to several days or months from now.

Let’s say that your modem at this location has a public IP address of 142.250.217.206. You have a VPN endpoint configured on your router on port 8443. In order to use an SSL VPN, you have to have a domain for which you create a certificate. So if you want to connect to the router, you might have to modify your host file on your local machine and put an entry like:

142.250.217.206    mysiterouter.seethesite.com

Then you can connect to the VPN using mysiterouter.seethesite.com:8443. This is a major cramp, especially if you have lots of sites and multiple people that need to connect.

The objective here is to create a DNS record for mysiterouter.seethesite.com and have it always updated with its public IP address, even if it changes.

Log into your registrar and edit the domain record for the domain you’re going to use for the dynamic DNS. I’m using Directnic.com in these examples.

You’ll want to create an A Record for the domain with the subdomain you’re going to use for connecting to the Router.

When you create the A Record, put in a random IP address. This way, you can see if the script on the router is working as expected by noting if the IP Address is updated to something else. Make sure that you have the Dynamic DNS option selected.

Once you add the record, it should look like:

Click on the little globe icon and it will display the link that you have to hit to update the IP Address.

The link will look something like this:

Copy this link to notepad, as we’ll need it for the future steps.

Now connect to your Mikrotik router using WinBox. Click on System > Scheduler and click on the plus icon to add a new scheduler record.

Give it a meaningful name. The Start Date/Time is not important, so long as they are in the past. Interval is in three sections: hh:mm:ss. I suggest setting it to something like 00:01:00 (every minute) while you’re testing it. Then change it later to 01:00:00 for a one hour interval. Leave all the check boxes as they are. It’s probably overkill for what we want, but I haven’t had time to find the minimum checkboxes needed.

Now we have to add the “On Event” section. You’ll use the URL that you copied from your registrar Dynamic DNS to include here. Your URL will look something like:

https://directnic.com/dns/gateway/4c8b1f91928ca1937fe4d665cd5818f07cbca7f93c7fd84858591f55c302be2e/?data=8.8.8.8

There are only two lines needed for the On Event script. The first will utilize a feature that is believe is only available ROS versions 6.x and higher.

:local publicip [/ip cloud get public-address]

This creates a local variable called “publicip” by using the /ip cloud command. The “get public-address” means that it will pull that property from the return object.

If you want to see what this object contains, in WinBox, you can click on “New Terminal” and type:

/ip cloud print

Now we need to replace the placeholder IP address in our registrar link and add it into the following command:

/tool fetch url="https://directnic.com/dns/gateway/4c8b1f91928ca1937fe4d665cd5818f07cbca7f93c7fd84858591f55c302be2e/?data=$publicip" mode=https

This command does a fetch on the URL. Notice after data=, I replaced the “8.8.8.8” with “$publicip” that will replace that value with the value retrieved by the /ip cloud command that was stored in the variable.

Remember, your URL may look vastly different, but the import part is to replace the IP Address with the variable name.

So your entire On Event should look something like:

:local publicip [/ip cloud get public-address]

/tool fetch url="https://directnic.com/dns/gateway/4c8b1f91928ca1937fe4d665cd5818f07cbca7f93c7fd84858591f55c302be2e/?data=$publicip" mode=https

Then click on “OK” to save it.

In WinBox, you can click on “Log”. You should eventually see entries with a Message that looks like:

fetch: file "?data=142.250.217.206" downloaded

The actual IP address in the message should by your public IP address as far as the Mikrotik router is concerned. If your router is not connected to the internet, the Scheduler might not run at all.

Once you see a message and it looks like the data is being show properly, go back to your registrar and refresh the screen on your DNS settings to look at your A Record. It should be updated from “8.8.8.8” to show your real public IP address.

The last step is to edit your Scheduler record and replace the interval with 01:00:00 so that it only runs once an hour. Having it ping every minute is too often and unecessary.

Author: Jack Yasgar

Jack Yasgar has been developing software for various industries for two decades. Currently, he utilizes C#, JQuery, JavaScript, SQL Server with stored procedures and/or Entity Framework to produce MVC responsive web sites that converse to a service layer utilizing RESTful API in Web API 2.0 or Microsoft WCF web services. The infrastructure can be internal, shared or reside in Azure. Jack has designed dozens of relational databases that use the proper primary keys and foreign keys to allow for data integrity moving forward. While working in a Scrum/Agile environment, he is a firm believer that quality software comes from quality planning. Without getting caught up in analysis paralysis, it is still possible to achieve a level of design that allows an agile team to move forward quickly while keeping re-work to a minimum. Jack believes, “The key to long term software success is adhering to the SOLID design principles. Software written quickly, using wizards and other methods can impress the business sponsor / product owner for a short period of time. Once the honeymoon is over, the product owner will stay enamored when the team can implement changes quickly and fix bugs in minutes, not hours or days.” Jack has become certified by the Object Management Group as OCUP II (OMG Certified UML Professional) in addition to his certification as a Microsoft Certified Professional. The use of the Unified Modeling Language (UML) provides a visual guide to Use Cases and Activities that can guide the product owner in designing software that meets the end user needs. The software development teams then use the same drawings to create their Unit Tests to make sure that the software meets all those needs. The QA testing team can use the UML drawings as a guide to produce test cases. Once the software is in production, the UML drawings become a reference for business users and support staff to know what decisions are happening behind the scenes to guide their support efforts.

Leave a Reply

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