Hello again! Emmanuel Corels here from Emmanuel Corels Creatives, back with another straightforward MikroTik tutorial. This time, let’s talk about automation and email notifications—two super handy features if you want to keep an eye on your network without constantly checking WinBox. Ever wondered how to schedule backups, reboot tasks, or get an email when your internet goes down? We’ll cover the basics right here.
Why Automate and Send Alerts?
- Hands-Off Management: Let the router perform routine tasks at specific times—like backups or reboots—so you don’t have to.
- Early Warnings: If a WAN link goes down, you can have MikroTik email you.
- Regular Maintenance: Scripts can prune old logs, refresh routes, or anything else you might need on a schedule.
Step 1: Basic Scripting on MikroTik
MikroTik’s scripting language is essentially a series of RouterOS commands. For instance, if you’d normally type /ip dns print
in the terminal, that’s also valid in a script—just copy-paste it into a script file.
-
WinBox → System → Scripts
- Click the “+” to create a new script.
- Name:
BackupScript
(for example). - Source: The commands you want. For instance:
/system backup save name=auto-backup /export file=auto-export
- Hit OK.
-
Test It
- Highlight the script, click Run Script. Check Files to see if
auto-backup.backup
orauto-export.rsc
got created.
- Highlight the script, click Run Script. Check Files to see if
(Tip: If you see errors, open Log or the Script window again for any hints. Adjust your commands accordingly.)
Step 2: Scheduling Your Script
Now that you have a script, let’s run it automatically—say, every night at 2:00 AM.
- WinBox → System → Scheduler
- Click “+”:
- Name:
NightlyBackup
- Start Date: Today’s date (or whenever you want to begin).
- Start Time:
02:00:00
- Interval:
1d
(meaning daily). - On Event:
BackupScript
(the name of your script).
- Name:
Click OK. Now your script runs each night at 2 AM. Check Files the next morning to confirm new backups were generated.
Step 3: Setting Up Email on MikroTik
Before you can get email alerts, MikroTik needs to know your SMTP server details. For example, if you use Gmail or another mail provider:
- WinBox → Tools → Email
- Server:
smtp.gmail.com
(if using Gmail). - Port:
587
(TLS) or465
(SSL) depending on your setup. - From:
YourGmailAddress@gmail.com
- User:
YourGmailAddress@gmail.com
- Password:
<YourGmailPassword>
or an App Password if you have 2FA. - TLS:
yes
orstarttls
if needed.
- Server:
(Gmail sometimes requires enabling “less secure apps” or using an app-specific password. Check Google’s documentation if you get auth errors.)
To test:
/tool e-mail send to="YourOtherEmail@domain.com" subject="Test" body="Hello from MikroTik"
If all’s well, you’ll receive a test email.
Step 4: Sending Automated Email Alerts
Now that email is working, you can incorporate email sends into scripts. For instance, let’s expand our backup script to email the newly created file:
/system backup save name=auto-backup
/export file=auto-export
/tool e-mail send to="MyEmail@domain.com" subject="Nightly Backup" body="Backup Attached" file="auto-backup.backup"
(Note: file=auto-backup.backup
attaches the backup file. You can also attach .rsc
exports if needed.)
Just update your script under System → Scripts and re-run or wait for the scheduler. Remember, large attachments might cause issues if your email provider has size limits—MikroTik backups for large configs can become big.
Step 5: Netwatch for Link Monitoring
If you want an alert when a certain host goes offline—like your ISP gateway or a critical server—Netwatch can do that. It pings a target periodically and triggers “up” or “down” scripts.
- WinBox → Tools → Netwatch
- Click “+”:
- Host: e.g.,
8.8.8.8
(Google DNS), or your ISP gateway IP. - Interval:
1m
(Netwatch checks every minute). - Timeout:
1s
or2s
, up to you. - Down Script: The script that runs when the host is unreachable. Example:
/tool e-mail send to="MyEmail@domain.com" subject="WAN Down" body="Can't reach 8.8.8.8"
- Up Script: If you want an email or log entry once the host is back online:
/tool e-mail send to="MyEmail@domain.com" subject="WAN Up" body="8.8.8.8 is reachable again"
- Host: e.g.,
Now, if your internet goes down, Netwatch can send a “down” alert. When it’s restored, you get an “up” message. Perfect for real-time monitoring.
Common Pitfalls
- Email Auth Errors: Double-check your SMTP settings. Many providers require specific ports or TLS.
- Scheduler Overlaps: If you run multiple scripts close together, they might conflict. Spread out schedules or keep scripts short.
- Large Attachments: Some backup files might exceed your email quota. Consider using
/export
(which is usually smaller) or storing backups on an FTP instead. - Netwatch Frequency: Setting
interval=10s
might spam your email. Keep it at a reasonable interval like 1 minute or 5 minutes. - Script Syntax: One wrong slash or command can break the script. Check logs or do a test run manually.
Wrapping Up
With just a few steps, you can automate backups, reboots, or any other RouterOS command on a schedule—and get email updates when something’s amiss. Scripts are your Swiss Army knife in RouterOS, and combining them with Scheduler and Netwatch opens up tons of possibilities: from routine maintenance to proactive alerts.
As always, keep exploring, keep refining your scripts, and don’t hesitate to experiment with more advanced logic or external integrations. MikroTik is all about giving you the power to shape your network exactly how you want. Have fun automating!
Explained with enthusiasm by
Emmanuel Corels – Admin, Emmanuel Corels Creatives