Versions Compared

Key

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

...

Execute the script in the background. The result can be written in the file by setting a "file"parameter or printed to the CLI by setting "as-string".

When using the "as-string" parameter executed script is blocked (not executed in the background).

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
convert:convert from=[arg] to=[arg]

Converts specified value from one format to another. By default uses an automatically parsed value, if the "from" format is not specified (for example, "001" becomes "1", "10.1" becomes "10.0.0.1", etc.).

from specifies the format of the value - base32, base64, hex, raw, url.

to specifies the format of the output value - base32, base64, hex, raw, url.

:put [:convert 001 to=hex ]

31

:put [:convert [/ip dhcp-client/option/get hostname raw-value] from=hex to=raw ]

MikroTik

delay:delay <time>do nothing for a given period of time
environment:environment print <start>print initialized variable information:global myVar true; :environment print;
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 setting a "file"parameter or printed to the CLI by setting "as-string".

When using the "as-string" parameter executed script is blocked (not executed in the background).


Code Block
languageros
{
:local j [:execute {/interface print follow where [:log info ~Sname~]}];
:delay 10s;
:do { /system script job remove $j } on-error={}
}


find:find <arg> <arg> <start>return position of a substring or array element:put [:find "abc" "a" -1];
jobname
:jobnamereturn current script name
Code Block
languageros
titleLimit script execution to single instance
:if ([/system script job print count-only as-value where script=[:jobname] ] > 1) do={
  :error "script instance already running"
  }


len:len <expression>return string length or array element count:put [:len "length=8"];
log:log <topic> <message>write a message to the system log. Available topics are "debug, error, info and warning":log info "Hello from script";
onerror:onerror <var_name> in={<command>} do={<expression>}

The command used to catch errors and get error details. The do={...} block is executed, when in={...} block has an error,  and error details are written in <var_name> variable. 

Warning

Parameter order is important. The "error" parameter must be set before "do" block, otherwise do block will not see the local variable. 


:onerror can return false (if there is no error) and true (if there is an error) values, so it can be used in :if condition statement scripts.

:onerror errorName in={ :error "failure" } do={ :put "Critical $errorName" }
parse:parse <expression>parse the string and return parsed console commands. Can be used as a function.:global myFunc [:parse ":put hello!"];
$myFunc;
pick:pick <var> <start> [<end>]

return range of elements or substring. If the end is not specified, will return only one element from an array.

  • var - value to pick elements from
  • start - starting index to start picking from (the first element index is 0)
  • end - terminating index (element at this index is not included)


Code Block
languageros
[admin@MikroTik] > :put [:pick "abcde" 1 3]
bc


put:put <expression>put the supplied argument into the console:put "Hello world"
resolve:resolve <arg>return the IP address of the given DNS name:put [:resolve "www.mikrotik.com"];
retry:retry command=<expr> delay=[num] max=[num] on-error=<expr>Try to execute the given command "max" amount of times with a given "delay" between tries. On failure, execute the expression given in the "on-error" block

:retry command={abc} delay=1 max=2 on-error={:put "got error"}
got error

Code Block
languagetext
:retry command={abc} delay=1 max=2 on-error={:put "got error"}
got error


typeof:typeof <var>the return data type of variable:put [:typeof 4];
rndnum:rndnum from=[num] to=[num]random number generator:put [:rndnum from=1 to=99];
rndstr:rndstr from=[str] length=[num]

Random string generator.

from specifies characters to construct the string from and defaults to all ASCII letters and numerals.
length specifies the length of the string to create and defaults to 16.

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



set:set <var> [<value>]assign value to a declared variable.:global a; :set a true;
terminal:terminal terminal related commands
time:time <expression>return interval of time needed to execute the command:put [:time {:for i from=1 to=10 do={ :delay 100ms }}];
timestamp:timestampreturns the time since epoch, where epoch is January 1, 1970 (Thursday), not counting leap seconds


Code Block
languagetext
[admin@MikroTik] > :put [:timestamp]
2735w21:41:43.481891543
or
[admin@MikroTik] > :put [:timestamp]
2735w1d21:41:43.481891543
with the day offset


toarray:toarray <var>convert a variable to the array
tobool:tobool <var>convert a variable to boolean
toid:toid <var>convert a variable to internal ID
toip:toip <var>convert a variable to IP address
toip6:toip6 <var>convert a variable to IPv6 address
tonum:tonum <var>convert a variable to an integer
tostr:tostr <var>convert a variable to a string
totime:totime <var>convert a variable to time
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
convert:convert from=[arg] to=[arg]

Converts specified value from one format to another. By default uses an automatically parsed value, if the "from" format is not specified (for example, "001" becomes "1", "10.1" becomes "10.0.0.1", etc.).

from specifies the format of the value - base32, base64, hex, raw, url.

to specifies the format of the output value - base32, base64, hex, raw, url.

:put [:convert 001 to=hex ]

31

:put [:convert [/ip dhcp-client/option/get hostname raw-value] from=hex to=raw ]

MikroTik

delay:delay <time>do nothing for a given period of time
environment:environment print <start>print initialized variable information:global myVar true; :environment print;
error:error <output>Generate console error and stop executing the scriptexecute:execute <expression>
Code Block
languageros
{
:local j [:execute {/interface print follow where [:log info ~Sname~]}];
:delay 10s;
:do { /system script job remove $j } on-error={}
}
find:find <arg> <arg> <start>return position of a substring or array element:put [:find "abc" "a" -1];grep:grep script=<expr> before=[num] after=[num] patter=[regex]

return lines of script output that do match specified pattern

before is  number of lines before the matched pattern that are returned together with result string

after is  number of lines after the matched pattern that are returned together with result string

filename is the name of file to which command output can be saved

pattern is the regular expression which is used against the script output in order to find matched results

script is the set of commands that will be executed and their provided output will be matched against the pattern

:grep script={ip firewall filter print; ip firewall mangle print; ip firewall/nat print; ip firewall raw print} pattern="srclist" 

jobname
:jobnamereturn current script name
Code Block
languageros
titleLimit script execution to single instance
:if ([/system script job print count-only as-value where script=[:jobname] ] > 1) do={
  :error "script instance already running"
  }
len:len <expression>return string length or array element count:put [:len "length=8"];log:log <topic> <message>write a message to the system log. Available topics are "debug, error, info and warning":log info "Hello from script";onerror:onerror <var_name> in={<command>} do={<expression>}

The command used to catch errors and get error details. The do={...} block is executed, when in={...} block has an error,  and error details are written in <var_name> variable. 

Warning

Parameter order is important. The "error" parameter must be set before "do" block, otherwise do block will not see the local variable. 

:onerror can return false (if there is no error) and true (if there is an error) values, so it can be used in :if condition statement scripts.:onerror errorName in={ :error "failure" } do={ :put "Critical $errorName" }parse:parse <expression>parse the string and return parsed console commands. Can be used as a function.:global myFunc [:parse ":put hello!"];
$myFunc;
pick:pick <var> <start> [<end>]

return range of elements or substring. If the end is not specified, will return only one element from an array.

  • var - value to pick elements from
  • start - starting index to start picking from (the first element index is 0)
  • end - terminating index (element at this index is not included)
Code Block
languageros
[admin@MikroTik] > :put [:pick "abcde" 1 3]
bc
put:put <expression>put the supplied argument into the console:put "Hello world"resolve:resolve <arg>return the IP address of the given DNS name:put [:resolve "www.mikrotik.com"];retry:retry command=<expr> delay=[num] max=[num] on-error=<expr>Try to execute the given command "max" amount of times with a given "delay" between tries. On failure, execute the expression given in the "on-error" block

:retry command={abc} delay=1 max=2 on-error={:put "got error"}
got error

Code Block
languagetext
:retry command={abc} delay=1 max=2 on-error={:put "got error"}
got error
typeof:typeof <var>the return data type of variable:put [:typeof 4];rndnum:rndnum from=[num] to=[num]random number generator:put [:rndnum from=1 to=99];rndstr:rndstr from=[str] length=[num]

Random string generator.

from specifies characters to construct the string from and defaults to all ASCII letters and numerals.
length specifies the length of the string to create and defaults to 16.

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

set:set <var> [<value>]assign value to a declared variable.:global a; :set a true;terminal:terminal terminal related commandstime:time <expression>return interval of time needed to execute the command:put [:time {:for i from=1 to=10 do={ :delay 100ms }}];timestamp:timestampreturns the time since epoch, where epoch is January 1, 1970 (Thursday), not counting leap seconds
Code Block
languagetext
[admin@MikroTik] > :put [:timestamp]
2735w21:41:43.481891543
or
[admin@MikroTik] > :put [:timestamp]
2735w1d21:41:43.481891543
with the day offset
toarray:toarray <var>convert a variable to the arraytobool:tobool <var>convert a variable to booleantoid:toid <var>convert a variable to internal IDtoip:toip <var>convert a variable to IP addresstoip6:toip6 <var>convert a variable to IPv6 addresstonum:tonum <var>convert a variable to an integertostr:tostr <var>convert a variable to a stringtotime:totime <var>convert a variable to time

Menu specific commands

Common commands

...