[minipost] Mikrotik scripting: Monitoring interface IP for changes

On my home network (that you might have noticed in older articles), I own a very nice Mikrotik/RoaterBoard RB450 router. I have an external public IP address that I recieve via DHCP, but sometimes my provider decides to change it. If this happens, sometimes it takes a while before I manually learn the new IP on it and because I am accessing home network via VPN and other stuff, this is technically an outage for me to not know the current public IP of my home. Normally people use things like dyndns.com service on their routers to manually update a hostname, but that is a little bit out of your control and I wanted a way how I can be directly informed about this change of external public IP. On Mikrotik, there is a quick and effective way how to monitor anything by a script and scheduled periodically, so this is a quick example how to monitor your external interface IP and have your Mikrotik send you an email if it changes.

First, let look configure your Mirotik to be able to send emails. The following is what I use to send emails to gmail as a normal SMTP message on port TCP25. The second command will also send a test message to “mypersonalemail@gmail.com” (this is a fake name of course), if you want to try it, change this to your email address and send it. And yes, do not enter your username/password! You do not need username/password to send via SMTP!

NOTE: You might want to check your gmail SPAM filter because this will most probably end there.

Secondly, lets create the script itself.

This simple script will use a global variable “actualIP” to store the IP value and once the script is executed, it will compare the currently stored value with this variable and if the do not match, it will update the variable and notify you by email. because this script was never executed until now, the actualIP variable is empty. This means that you can test the script by simply executing it and it will send you the current IP on ether1 interface.

You can test run your script manually by finding it via /system script print and then executing /system script run <number>, where <number> is the ID of the script. You should get your first email about IP change from this.

Now, lets schedule a periodic execution of this script to have a permanent monitoring of the IP on ether1.

The above command will create a periodic execution of the script every hour. You can also monitor the execution count that is visible if you use /system scheduler print as shown below.

In summary

I hope this helped you in some way, I encourage you to have a look on a great Mikrotik Scripting Wiki for full tutorial if you want to learn a bit more.

 

If you enjoyed this blog, please share.

About Peter Havrila

Author's Profile