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"
}