MikroTik RouterOS Scripting: Unlocking Automation Potential
- Blog
- 17 Jul, 2024
MikroTik RouterOS Scripting: Unlocking Automation Potential
Did you know that RouterOS has its own scripting language? Many tasks you can imagine automating on your router can be scripted, allowing you to add niche features without requiring a computer science degree. With a basic understanding of scripting principles, you can harness the power of RouterOS scripting.
How RouterOS Scripting Works
Similar to popular languages like Bash and Python, RouterOS scripting allows you to script directly into the terminal. This is convenient for testing commands and executing multiple commands in sequence. However, once you develop something worthwhile, you’ll want to store it as a script in the system script section, which helps with permissions and keeps everything organised.
Adding a Basic Script
Let's add a simple script to demonstrate the basic principles. Start by giving the script a name. Although other parameters will be generated, you can focus on editing your source. The built-in script editor, though simple, highlights the syntax, allowing you to enter any RouterOS commands just as you would in the terminal. This basic script might consist of multiple lines of configuration, but the possibilities are broad, limited only by your imagination and the fundamental building blocks available.
Variables in RouterOS Scripting
RouterOS scripting supports 11 data types, automatically recognised when assigned to variables. Variables can be local or global, with local variables created using the `local` keyword followed by the variable name and optionally a value. Global variables are declared similarly but remain in the RAM after the script runs, unlike local variables which are removed. The `put` command prints variables, using the `$` symbol to reference their values.
Scope and Arithmetic Operations
RouterOS scripting requires careful management of variable scope. Local variables should be used whenever possible due to their safe and limited scope. However, enclosing a local variable in curly brackets limits its accessibility, causing any command outside the brackets to fail. Arithmetic operations, such as multiplication, are supported using the `set` command. While typical operators like multiplication work, some, like the power operator, perform different functions (e.g., bitwise XOR).
Conditional Statements and Functions
Conditional statements follow the `if` condition format, with actions placed in curly brackets. Loops and functions are essential scripting components. Functions are defined similarly to variables, using the `do` action for their value. Looping with integers and calling functions within loops can automate repetitive tasks, such as creating a multiplication table.
Advanced Automation
RouterOS scripting can automate complex configurations and behaviors, utilising built-in trigger mechanisms like Scheduler, Netwatch, and DHCP actions. This capability allows for the creation of unique features tailored to specific needs. Many users have shared useful scripts online, with resources available in forums and GitHub repositories.
Conclusion
To explore more scripting commands and operators, visit the RouterOS help page. By leveraging RouterOS scripting, you can significantly enhance your router's functionality, creating custom solutions and automating tasks to streamline your network management.
Watch Full Video Here: https://www.youtube.com/watch?v=GJ-57ByVMWk