Mikrotik scripting language: a list of questions I had linking to the forum messages having answers
Posted by jpluimers on 2017/06/08
The RouterOS scripting language you can use on Mikrotik device immediately shows it’s origin: the console.
- It is a statement oriented language where statement separators can be both semicolons and new-lines.
- You can use the \ at the end-of the line as line-continuation character effectively spreading statements over multiple lines.
As promised some links to questions I asked:
- What parts of scripts can be ran from the console? – MikroTik RouterOS
The answer teaches about scope and locality on the console. - Difference between `[]`, `{}` and `()` expressions? – MikroTik RouterOS
Their positioning can be tricky:

- Bug? How can I create an empty associative array, then fill it to map strings to strings? – MikroTik RouterOS
This is how you do it:
:global convert ({}); - Is there a good way to sync a local directory from the scripts that a Mikrotik has? – MikroTik RouterOS
No there isn’t. There is a way to sync/system scriptentries to the file system, but due to a stupid 4096 length string export issue (causing the dreaded “failure: new contents too long” error), scripts longer than 4096 bytes will not even be written truncated. Since LUA got ditched and never re-introduced, LUA is not a workaround either and splitting into multiple files is impractical.
This one-liner will show you length+name of all your/system script` entries:
:foreach scriptId in [/system script find] do={ :local scriptSource [/system script get $scriptId source]; :local scriptSourceLength [:len $scriptSource]; :local scriptName [/system script get $scriptId name]; :put "$scriptSourceLength bytes: '$scriptName'" } - a
Some questions by others that were also extremely useful:
- Functions and function parameters – MikroTik RouterOS
new function syntax, much simpler than the old syntax in Functions in CMD Scripts – MikroTik RouterOS that hopefully someone someday will convert to the new syntax. - Functions and function parameters – MikroTik RouterOS
:typeof can return “nothing” - Functions and function parameters – MikroTik RouterOS
Fragment that adds all scripts named “Function.*” as :global functions upon system startup.
Declare those functions when you need them, just like in the nested-function example. - run script from terminal – MikroTik RouterOS
this is in fact very simple:
it also has the benefit that the terminal does tell you on which line and column your script is wrong (Winbox does not show that during execution):

- [Solved] Use of externally defined global variables inside import scripts, – MikroTik RouterOS
It’s better to pass information to functions as parameters (named parameters make code a lot more readable than positional parameters). - Return IP Octet Function – MikroTik RouterOS
Parses an IP octet and returns either a specific or all octets. More elaborate: Mikrotik Scripting – Function to Split an IP Address into an Array | Paper Street Online
Note you can use bitwise operators on octets. - How do you clear a global variable? – MikroTik RouterOS
List all global variables using /system script environment
Unset a variable with a play :set variableName - Array Pop Function – MikroTik RouterOS
- Array Push Function – MikroTik RouterOS
- Basic XML/string parser function – MikroTik RouterOS
Function getBetween(inputString, betweenStart, betweenEnd) - Using :find command where string is not found – MikroTik RouterOS
:if ([:len [:find "abcd" "x"]] > 0) do={:put "Found";} else={:put "Not Found";}; - Manual:Configuration Management – MikroTik Wiki
When you upload a script over ftp and have it end with auto.rsc, then it is automatically being executed and logged. For instance a file called anything.auto.rsc will have the log written to anything.auto.log. - I did it! Script to compute UNIX time! – MikroTik RouterOS
- Understanding scripting data types – MikroTik RouterOS
:typeof, nil, nothing, str, :parse vs new-style functions (:parse can be faster!) - Simple HTTP GET? – MikroTik RouterOS
Some escaping required… - It is not possible to exit or break a loop statement – MikroTik RouterOS so if you want to break a :for loop early, you have to recode it into a :while loop. You can
:returnfrom a function when inside a loop, but that’s not the same (for instance compare C#breakversusreturnor Delphibreakversusexit). - :for loops are a strange beast so I will elaborate on those in a separate post.
And a few observations:
- Functions do not need to be global. The RouterOS Scripting Manual paragraph on functions shows an example with :global that works just as fine with :local
:local myFunc do={:put "hello from function"} $myFunc # output: # hello from function - a
–jeroen






Mikrotik RouterOS scripting: for loops are a bit of getting used to « The Wiert Corner – irregular stream of stuff said
[…] Earlier, I wrote “:for loops are a strange beast so I will elaborate on those in a separate post.” so now is the time to do that. […]