Manually rebooting routers, updating firewall address lists, or tweaking bandwidth limits is tedious and error‑prone. MikroTik RouterOS includes a powerful scripting engine and scheduler that can automate almost any task. This guide teaches you the basics of RouterOS scripting and shows you practical, production‑ready scripts for daily use.
• Why Automate Your MikroTik?
- » Save time – Let the router handle recurring tasks while you focus on more important work.
- » Improve reliability – Scripts run exactly the same way every time, eliminating human error.
- » React instantly – Use netwatch, scheduler, or DHCP events to trigger actions automatically.
- » Enhance security – Dynamically block port scanners or brute‑force attempts.
Scripting Basics: The Building Blocks
1. The Script Editor
Access scripts under System → Scripts in WinBox, or via CLI:
/system script
Scripts are stored in order and can be run manually or triggered by events.
2. The Scheduler
Find it under System → Scheduler. Schedulers run scripts at specific times (e.g., daily at 3 AM) or intervals (e.g., every 5 minutes).
3. Basic Script Syntax
RouterOS scripts use a simple, command‑based language. Each command is on a new line. Variables use $variableName.
:local myMessage "Hello, MikroTik" :put $myMessage
Conditional statements framework layout loop example:
:if ([:len [/interface find where running]] > 0) do={
:put "At least one interface is running"
} else={
:put "No interfaces are running"
}
Loops implementation format structure:
:foreach i in=[/interface ethernet find] do={
:put [/interface ethernet get $i name]
}
Practical Script Examples (Ready to Use)
Example 1: Automatic Daily Backup and Email
Create a hardware state backup file and route it via network mail utilities.
Script Node (name: backup_email):
/system backup save name=auto-backup-$[/system clock get date] /tool e-mail send to="info@kmikro.com" subject="Router Backup" body="Daily backup attached." file=auto-backup-$[/system clock get date].backup
Scheduler Execution Node (daily at 2 AM):
/system scheduler add name=daily-backup start-time=02:00:00 interval=1d on-event=backup_email