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.

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.