Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

This manual provides an introduction to RouterOS's built-in powerful scripting language.

...

Single command inside (), [] or {} does not require any end of command character. End The end of the command is determined by the content of the whole script

...

A physical line is a sequence of characters terminated by an end-of-line (EOL) sequence. Any of the standard platform line termination sequences can be used:

  • unixUnix – ASCII LF;
  • windowsWindows – ASCII CR LF;
  • mac – ASCII CR;

Standard C conventions for new-line newline characters can be used ( the \n character).

...

Code Block
languageros
 :put (192.168.88.77&0.0.0.255);

Use the "|" operator and inverted CIDR mask to calculate the broadcast address:

...

Valid characters in variable names are letters and digits. If the variable name contains any other character, then the variable name should be put in double - quotes. Example:

Code Block
languageros
#valid variable name 
:local myVar; 
#invalid variable name 
:local my-var; 
#valid because double quoted 
:global "my-var";

...

Code Block
languageros
#convert string to array 
:local myStr "1,2,3,4,5"; 
:put [:typeof $myStr]; 
:local myArr [:toarray $myStr]; 
:put [:typeof $myArr]

Variable names are case-sensitive.

Code Block
languageros
:local myVar "hello" 
# following line will generate error, because variable myVAr is not defined 
:put $myVAr 
# correct code 
:put $myVar

...

But will work with different defined variablevariables:

Code Block
languageros
 { 
:local customname "ether1"; 
/interface print where name=$customname; 
}

...

CommandSyntaxDescriptionExample
/
go to the root menu
..
go back by one menu level
?
list all available menu commands and brief descriptions
global:global <var> [<value>]define a global variable:global myVar "something"; :put $myVar;
local:local <var> [<value>]define the local variable{ :local myLocalVar "I am local"; :put $myVar; }
beep:beep <freq> <length>beep built-in speaker
delay:delay <time>do nothing for a given period of time
put:put <expression>put supplied argument to console
len:len <expression>return string length or array element count:put [:len "length=8"];
typeof:typeof <var>the return data type of variable:put [:typeof 4];
pick:pick <var> <start>[<end>]return range of elements or substring. If the end position is not specified, will return only one element from an array.:put [:pick "abcde" 1 3]
log:log <topic> <message>write a message to the system log. Available topics are "debug, error, info and warning":log info "Hello from script";
time:time <expression>return interval of time needed to execute the command:put [:time {:for i from=1 to=10 do={ :delay 100ms }}];
set:set <var> [<value>]assign value to a declared variable.:global a; :set a true;
find:find <arg> <arg> <start>return position of a substring or array element:put [:find "abc" "a" -1];
environment:environment print <start>print initialized variable information:global myVar true; :environment print;
terminal
terminal related commands
error:error <output>Generate console error and stop executing the script
execute:execute <expression>Execute the script in the background. The result can be written in the file by settingfile parameter.
{
:local j [:execute {/interface print follow where [:log info ~Sname~]}];
:delay 10s;
:do { /system script job remove $j } on-error={}
}
parse:parse <expression>parse the string and return parsed console commands. Can be used as a function.:global myFunc [:parse ":put hello!"];
$myFunc;
resolve:resolve <arg>return the IP address of the given DNS name:put [:resolve "www.mikrotik.com"];
rndnum:rndnum from=[num] to=[num]random number generator:put [:rndnum from=1 to=99];
rndstr:rndstr from=[str] length=[num]random string generator

:put [:rndnum from="abcdef%^&" length=33];

toarray:toarray <var>convert variable to the array
tobool:tobool <var>convert variable to boolean
toid:toid <var>convert variable to internal ID
toip:toip <var>convert variable to IP address
toip6:toip6 <var>convert variable to IPv6 address
tonum:tonum <var>convert variable to an integer
tostr:tostr <var>convert variable to a string
totime:totime <var>convert variable to time

...

CommandSyntaxDescription
addadd <param>=<value>..<param>=<value>add new item
removeremove <id>remove selected item
enableenable <id>enable selected item
disabledisable <id>disable selected item
setset <id> <param>=<value>..<param>=<value>change selected items parameter, more than one parameter can be specified at the time. The parameter can be unset by specifying '!' before the parameter.

Example:
/ip firewall filter add chain=blah action=accept protocol=tcp port=123 nth=4,2
print
set 0 !port chain=blah2 !nth protocol=udp

getget <id> <param>=<value>get selected items parameter value
printprint <param><param>=[<value>]print menu items. Output depends on print parameters specified. Most The most common print parameters are described here
exportexport [file=<value>]export configuration from the current menu and its sub-menus (if present). If the file parameter is specified output will be written to file with extension '.rsc', otherwise the output will be printed to console. Exported commands can be imported by import command
editedit <id> <param>edit selected items property in the built-in text editor
findfind <expression>Returns list of internal numbers for items that are matched by given expression. For example:  :put [/interface find name~"ether"]

...

ParameterDescriptionExample
append

as-valueprint output as an array of parameters and its values:put [/ip address print as-value]
briefprint brief description
detailprint detailed description, the output is not as readable as brief output but may be useful to view all parameters
count-onlyprint only count of menu items
fileprint output to file
followprint all current entries and track new entries until ctrl-c is pressed, very useful when viewing log entries/log print follow
follow-onlyprint and track only new entries until ctrl-c is pressed, very useful when viewing log entries/log print follow-only
fromprint parameters only from specified item/user print from=admin
intervalcontinuously print output in a selected time interval, useful to track down changes where follow is not acceptable/interface print interval=2
terseshow details in a compact and machine-friendly format
value-listshow values one per line (good for parsing purposes)
without-pagingIf the output does not fit in the console screen then do not stop, print all information in one piece
whereexpressions followed by where parameter parameters can be used to filter outmatched entries/ip route print where interface="ether1"

...

Code Block
languageros
#add script
/system script add name=myScript source=":put \"Hello $myVar !\""

:global myFunc [:parse [/system script get myScript source]]
$myFunc myVar=world

output:
Hello world !

...


Warning
If the function contains a defined global variable that names match the name of the passed parameter, then the globally defined variable is ignored, for compatibility with scripts written for older versions. This feature can change in future versions. Avoid using parameters with the same name as global variables.

For example:

Code Block
languageros
:global my2 "123" 

:global myFunc do={ :global my2; :put $my2; :set my2 "lala"; :put $my2 } 
$myFunc my2=1234 
:put "global value $my2"

...

Loop through keys and values

"foreach" command can be used to loop through keys and elements:

Code Block
languageros
[admin@ce0] > :foreach k,v in={2; "aX"=1 ; y=2; 5} do={:put ("$k=$v")} 

0=2 
1=5 
aX=1 
y=2

If for each the "foreach" command is used with one argument, then the element value will be returned:

...

Sub-menu level: /system script

Contains all user-created scripts. Scripts can be executed in several different ways:

...

Note: Only scripts (including schedulers, netwatch, etc) with equal or higher permission rights can execute other scripts.

PropertyDescription
comment (string; Default: )Descriptive comment for the script
dont-require-permissions (yes | no; Default: no)Bypass permissions check when the script is being executed, useful when scripts are being executed from services that have limited permissions, such as Netwatch
name (string; Default: "Script[num]")name of the script
policy (string; Default: )list of applicable policies:
  • ftp - can log on remotely via ftp FTP and send and retrieve files from the router
  • password - change passwords
  • policy - manage user policies, add and remove user
  • read - can retrieve the configuration
  • reboot - can reboot the router
  • sensitive - allows changing "hide sensitive" parameter
  • sniff - can run sniffer, torch, etc
  • test - can run ping, traceroute, bandwidth test
  • write - can change the configuration

Read more detailed policy descriptions here

source (string;)Script source code

...