[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!

[admin@RB450] /tool e-mail> print
server: 74.125.47.27:25
from: "test@networkgeekstuff.com"
username: ""
password: ""
[admin@RB450] /tool e-mail> send to="mypersonalemail@gmail.com" \
    subject="test" body="test from networkgeekstuff.com examples"

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

Secondly, lets create the script itself.

/system script add name="ether1monitorIPchange" source={

  :global actualIP;

  :local newIP [/ip address get [find interface="ether1"] address];

  :if ($newIP != $actualIP) do={
    :put "ip address $actualIP changed to $newIP";
    :set actualIP $newIP;
    /tool e-mail send to="mypersonalemail@gmail.com" \
     subject="IP change detected by Mikrotik Script"\
     body=$actualIP server=213.46.255.2 \
     from="admin@networkgeekstuff.com";
  }
}

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.

/system scheduler add name=periodicIPcheck interval=1h on-event=ether1monitorIPchange

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.

[admin@RB450] /system scheduler> print 
Flags: X - disabled 
 #   NAME                START-DATE  START-TIME              INTERVAL             ON-EVENT              RUN-COUNT 
 0   periodicIPcheck     aug/09/2013 09:11:08                1h                   ether1monitorIPch...  145

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.

 

---
Peter Havrila , published on

3 comments ...

  1. Great script, only found out that in order script to work you need to setup provider IP adress of SMTP in this field, maybe you can mention it as well 😉 Or I’m missing something?

    body=$actualIP server=213.46.255.2

    1. Hello Andrejs,
      Thanks for input!, it is interesting, but it works for me without any “provider IP” address. Maybe we have a different version running that may have SMTP client software difference (only speculation on this point), maybe I will check details when I have some extra free time.

      Peter

Comments are closed.