Router Babysitting Watchdogs: Gmail IP, Boot IP, IP Change
Posted: Mon Sep 30, 2013 4:54 am
I wrote/modified a few scripts to assist with babysitting remote routers. These should be quite scalable and generate minimal traffic when configured correctly. Updated for RouterOS v6.4. Thank you to SurferTim, Boretto, and the Mikrotik Wiki.
I'll also attach the scripts without comments at the end. These scripts should all be very easily adaptable to most use scenarios. I'm posting these scripts in this order because of their dependencies. Most operations within these scripts are limited to run as infrequently as possible.
I'll start with an Gmail/email IP updater. A while back, I noticed one of my notification scripts had broken - Google had changed Gmail's IP from the one I had statically set, so I added in a little redundancy for the future. I run this script every 6 hours - I don't want to miss an email, but I don't want to keep writing the same data to storage unnecessarily...so I added an if statement to prevent unnecessary writes too. Most providers don't retire the IP of a major service every day.
My second script is my boot emailer. I wrote this one because I just wanted to know when my power at a site was out...and just as important, when it came back on. It includes no fault-checking because it should only run on a boot event, and the next script will cover failure to send events. Sharing the variable prevents receiving 2 emails every time it reboots. I prefer my links to be clickable - I run OS X or Linux 90% of the time, so I can use keyboard shortcuts to more quickly highlight and copy the IPs if they open a new page - its only seconds, but its a convenience thing. However, lately Gmail's been confusing some IPs with phone numbers so I'll have to find a way to force the script to specify within the email somehow without showing "http://". Its also easier to copy in android. Accordingly, I strip off my netmask by simply deleting the last 3 characters - if your production netmask 9 or less, why are you even reading my post?
The last simple script for this section wraps everything together. While on a production network, ideally many links are static, I actually originally wrote this one for my home router since Comcast surprised me and actually changed my IP one day after what felt like 6 months on my DHCP lease. Naturally, it was applied to production practically the next day. My next little side goal is to make clickable WinBox links sent from within the script. I don't want to make the script trigger another event which would send the final email because I'm trying to remove failure points.
I'll also attach the scripts without comments at the end. These scripts should all be very easily adaptable to most use scenarios. I'm posting these scripts in this order because of their dependencies. Most operations within these scripts are limited to run as infrequently as possible.
I'll start with an Gmail/email IP updater. A while back, I noticed one of my notification scripts had broken - Google had changed Gmail's IP from the one I had statically set, so I added in a little redundancy for the future. I run this script every 6 hours - I don't want to miss an email, but I don't want to keep writing the same data to storage unnecessarily...so I added an if statement to prevent unnecessary writes too. Most providers don't retire the IP of a major service every day.
Code: Select all
##### Gmail IP Updater ##### Recommend set to run each 1h-24h
##### Swap in your own server
##### used to prevent cloud services from breaking since RouterOS won't accept a URL setting within email tool.
:global existingGmailIP
:local comparedIP
:set comparedIP [:put {:resolve "smtp.gmail.com"}];
## write reduction
:if $existingGmailIP != $comparedIP do={
/tool e-mail set server=$gmailIP
:set existingGmailIP $comparedIP
}
Code: Select all
##### IP Boot Watchdog ##### Set to run on boot.
## initial IP acquisition time - adjust to your needs, but 45 usually works. I'm more aggressive, using 20.
delay 20s
## shared variables for other IP Watchdog scripts
:global IPextemailstore;
## find address (replace "ether1" with your interface)
:set IPextemailstore [/ip address get [find interface="ether1"] address];
## Thanks Boretto for removal of netmask with KISS approach.
:local largo [:len $IPextemailstore]
:local largo [:put ($largo-3)]
:local nomask [:pick $IPextemailstore 0 $largo]
:global IPextemailstore $nomask
## send email notification (replace "notify@gmail.com" with your monitoring email distro) (replace with instructions if desired) (includes winbox URL handler)
/tool e-mail send to=notify@gmail.com from=notify@gmail.com subject="Router Rebooted - IP Updated" body="The router has rebooted. It is is now at: $IPextemailstore. This automated message was sent by a MikroTik Script."
## log activity locally QC
/log warning "System Rebooted - IP $IPstartcheck Emailed";
Code: Select all
##### IP Change Notifier ##### recommend set to run every 1m-60m depending on your urgency - running too often can email you twice.
## Boot notification prevention delay - designed to run AFTER first event of boot emailer
delay 30s
## Shared global variable (rename or enumerate for multiple ext interfaces & script uses)
:global IPemailstore;
## Unshared variables
:local IPcompare;
:local IPtime;
:local IPdate;
## Redundancy for IP Boot Watchdog script - will NOT email the first time!
:if ( $IPextemailstore = nil ) do={ set IPextemailstore [/ip address get [find interface="ether1"] address ]};
## Find current IP
:set IPcompare [/ip address get [find interface="ether1"] address];
## Compare Operation
if ($IPextemailstore != $IPcompare) do={
## limit find date/time to only when necessary
:set IPdate [/system clock get date];
:set IPtime [/system clock get time];
## remove netmask from IP
:local largo [:len $IPextemailstore];
:local largo [:put ($largo-3)];
:local nomask [:pick $IPextemailstore 0 $largo];
:local IPcompare $nomask;
## Send changed IP notification
/tool e-mail send to=notify@gmail.com from=notify@gmail.com subject="Dynamic IP changed to $IPcompare" body="The router IP changed on $IPdate at $IPtime. The IP changed from $IPextemailstore to $IPcompare. This automated message was sent by a MikroTik Script.";
## Synchronize IP for next cycle
:set IPextemailstore $IPcompare;
## Log locally for QC & close script
/log warning "System IP Changed - New IP Emailed"
}