MikroTik scripting is a powerful tool for automating tasks and managing devices in RouterOS. This guide provides a detailed overview of scripting in MikroTik, including best practices, syntax, and examples.
Basics of MikroTik Scripting
MikroTik scripts are written in a simple language similar to a command-line interface. They are executed in sequence and can be used to automate repetitive tasks, monitor systems, or configure devices.
Structure
Scripts in MikroTik follow a logical structure:
- Comments: Start with
#
or:comment
for inline comments. - Commands: Use RouterOS commands.
- Control Structures: Use loops, conditions, and functions.
Example Basic Script
# This is a sample script
:log info "Starting the script"
:local var1 "Hello World"
:log info $var1
Variables in MikroTik
Variables are essential for storing and manipulating data.
Defining Variables
Use :local
to define a variable.
:local var1 "example text"
Types of Variables
- String: Text data. Example:
"RouterOS"
- Integer: Numbers. Example:
42
- Boolean: Logical values (
true
/false
). - Array/List: Ordered collections. Example:
[:toarray "a,b,c"]
- Dictionary: Key-value pairs.
Variable Scope
Variables defined with :local
are accessible only within the script. Use :global
for variables that persist outside the script.
:global sharedVar "This is global"
:local localVar "This is local"
Control Structures
MikroTik supports basic programming structures:
Conditional Statements
Use if
and else
to branch logic.
:if ($var1 = "RouterOS") do={
:log info "It's RouterOS"
} else={
:log warning "Unknown system"
}
Loops
For Loop
:for i from=1 to=5 do={
:log info "Loop iteration $i"
}
While Loop
:local counter 0
:while ($counter < 5) do={
:log info "Counter: $counter"
:set counter ($counter + 1)
}
Foreach Loop
:foreach item in=[/interface print as-value] do={
:log info "Interface: $item"
}
Functions
Functions are reusable blocks of code.
Creating a Function
:global myFunction do={
:local param1 $1
:local param2 $2
:log info "Param1: $param1, Param2: $param2"
}
Calling a Function
$myFunction "Value1" "Value2"
Working with System Commands
You can execute RouterOS commands inside scripts.
Example: Backup Configuration
/system backup save name="backup_$(/system clock get date)"
Example: Reboot Router
/system reboot
Error Handling
Use try
and on-error
to handle errors.
:do {
/interface enable [find name="ether1"]
} on-error={
:log error "Failed to enable interface ether1"
}
Logging and Debugging
Logging is essential for debugging and monitoring scripts.
Log Levels
info
: General information.warning
: Warnings.error
: Errors.
Example Logging
:log info "Script started"
:log warning "This is a warning"
:log error "This is an error"
Examples of Common Scripts
Ping Monitoring
Check if a host is reachable and take action.
:local host "8.8.8.8"
:if ([/ping $host count=3] = 0) do={
:log error "$host is unreachable"
} else={
:log info "$host is reachable"
}
Dynamic Firewall Rule
Automatically block an IP after failed login attempts.
:foreach ip in=[/log find where message~"login failure"] do={
:local srcIP [/log get $ip src-address]
/ip firewall address-list add list=blacklist address=$srcIP timeout=1h
:log warning "Blocked IP: $srcIP"
}
Best Practices
- Use Comments: Document your scripts for clarity.
- Avoid Global Variables: Use
:global
only when necessary. - Test Scripts: Test in a safe environment before applying to production.
- Keep Scripts Modular: Use functions to break down complex scripts.
- Limit Resource Usage: Avoid infinite loops or excessive resource consumption.
Resources
- MikroTik Wiki: https://wiki.mikrotik.com
- RouterOS Documentation: https://help.mikrotik.com
- Community Forum: https://forum.mikrotik.com
This guide provides the foundation for scripting in MikroTik. With practice, you can create scripts tailored to your specific needs.