点击这里给我发消息      点击这里给我发消息


QQ: 358207017






首 页>技术信息>技术信息技术信息

作者: QQ:86698456  点击次数:   添加时间: 2017-03-13


Scripting language has two types of variables:
global - accessible from all scripts created by current user, defined by global keyword;
local - accessible only within the current scope, defined by local keyword.
Every variable, except for built in RouterOS variables, must be declared before usage by local or global keywords. Undefined variables will be marked as undefined and will result in compilation error. Example:
# following code will result in compilation error, because myVar is used without declaration :set myVar "my value"; :put $myVar
Correct code:
:local myVar; :set myVar "my value"; :put $myVar;
Valid characters in variable names are letters and digits. If variable name contains any other character, then variable name should be put in double quotes. Example:
#valid variable name :local myVar; #invalid variable name :local my-var; #valid because double quoted :global "my-var";
If variable is initially defined without value then variable data type is set to nil, otherwise data type is determined automatically by scripting engine. Sometimes conversion from one data type to another is required. It can be achieved using data conversion commands. Example:
#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.
:local myVar "hello" # following line will generate error, because variable myVAr is not defined :put $myVAr # correct code :put $myVar
Global commands
Every global command should start with ":" token, otherwise it will be treated as variable.
Command Syntax Description Example
/ go to root menu
.. go back by one menu level
? list all available menu commands and brief descriptions
global :global [] define global variable :global myVar "something"; :put $myVar;
local :local [] define local variable { :local myLocalVar "I am local"; :put $myVar; }
beep :beep beep built in speaker
delay :delay do nothing for a given period of time
put :put put supplied argument to console
len :len return string length or array element count :put [:len "length=8"];
typeof :typeof return data type of variable :put [:typeof 4];
pick :pick [] return range of elements or substring. If end position is not specified, will return only one element from an array. :put [:pick "abcde" 1 3]
log :log write message to system log. Available topics are "debug, error, info and warning" :log info "Hello from script";
time :time return interval of time needed to execute command :put [:time {:for i from=1 to=10 do={ :delay 100ms }}];
set :set [] assign value to declared variable. :global a; :set a true;
find :find return position of substring or array element :put [:find "abc" "a" -1];
environment :environment print print initialized variable information :global myVar true; :environment print;
terminal terminal related commands
error :error Generate console error and stop executing the script
parse :parse parse string and return parsed console commands. Can be used as function. :global myFunc [:parse ":put hello!"];
resolve :resolve return IP address of given DNS name :put [:resolve ""];
toarray :toarray convert variable to array
tobool :tobool convert variable to boolean
toid :toid convert variable to internal ID
toip :toip convert variable to IP address
toip6 :toip6 convert variable to IPv6 address
tonum :tonum convert variable to integer
tostr :tostr convert variable to string
totime :totime convert variable to time
Menu specific commands
Common commands
Following commands available from most sub-menus:
Command Syntax Description
add add =..= add new item
remove remove remove selected item
enable enable enable selected item
disable disable disable selected item
set set =..= change selected items parameter, more than one parameter can be specified at the time. Parameter can be unset by specifying '!' before parameter. 
/ip firewall filter add chain=blah action=accept protocol=tcp port=123 nth=4,2 
set 0 !port chain=blah2 !nth protocol=udp
get get = get selected items parameter value
print print =[] print menu items. Output depends on print parameters specified. Most common print parameters are describedhere
export export [file=] export configuration from current menu and its sub-menus (if present). If file parameter is specified output will be written to file with extension '.rsc', otherwise output will be printed to console. Exported commands can be imported by import command
edit edit edit selected items property in built-in text editor
find find find items by given expression.
Import command is available from root menu and is used to import configuration from files created by exportcommand or written manually by hand.
print parameters
Several parameters are available for print command:
Parameter Description Example
as-value print output as array of parameters and its values :put [/ip address print as-value]
brief print brief description
detail print detailed description, output is not as readable as brief output, but may be useful to view all parameters
count-only print only count of menu items
file print output to file
follow print all current entries and track new entries until ctrl-c is pressed, very useful when viewing log entries /log print follow
follow-only print and track only new entries until ctrl-c is pressed, very useful when viewing log entries /log print follow-only
from print parameters only from specified item /user print from=admin
interval continuously print output in selected time interval, useful to track down changes where follow is not acceptable /interface print interval=2
terse show details in compact and machine friendly format
value-list show values one per line (good for parsing purposes)
without-paging If output do not fit in console screen then do not stop, print all information in one piece
where expressions followed by where parameter can be used to filter out matched entries /ip route print where interface="ether1"
More than one parameter can be specified at a time, for example, /ip route print count-only interval=1 where interface="ether1"
Loops and conditional statements
Command Syntax Description
do..while :do { } while=( ); :while ( ) do={ }; execute commands until given condition is met.
for :for from= to= step= do={ } execute commands over a given number of iterations
foreach :foreach in= do={ }; execute commands for each elements in list
Conditional statement
Command Syntax Description
if :if() do={} else={} If a given condition is true then execute commands in the do block, otherwise execute commands in the elseblock if specified.
{  :local myBool true;  :if ($myBool = false) do={ :put "value is false" } else={ :put "value is true" } }
Script repository
Sub-menu level: /system script
Contains all user created scripts. Scripts can be executed in several different ways:
on event - scripts are executed automatically on some facility events ( scheduler, netwatch, VRRP)
by another script - running script within script is allowed
manually - from console executing run command or in winbox
Property Description
name (string; Default: "Script[num]") name of the script
policy (string; Default: ) list of applicable policies:
api - api permissions
ftp - can log on remotely via ftp and send and retrieve files from the router
local - can log on locally via console
password - change passwords
policy - manage user policies, add and remove user
read - can retrieve the configuration
reboot - can reboot the router
sensitive - see passwords and other sensitive information
sniff - can run sniffer, torch etc
ssh - can log on remotely via secure shell
telnet - can log on remotely via telnet
test - can run ping, traceroute, bandwidth test
web - can log on remotely via http
winbox - winbox permissions
write - can retrieve and change the configuration
source (string;) Script source code
Read only status properties:
Property Description
last-started (date) Date and time when the script was last invoked.
owner (string) User who created the script
run-count (integer) Counter that counts how many times script has been executed
Menu specific commands
Command Description
run (run [id|name]) Execute specified script by ID or name
Sub-menu level:
/system script environment
Contains all user defined variables and their assigned values.
[admin@MikroTik] > :global example; [admin@MikroTik] > :set example 123 [admin@MikroTik] > /environment print "example"=123
Read only status properties:
Property Description
name (string) Variable name
user (string) User who defined variable
value () Value assigned to variable
Sub-menu level: /system script job
Contains list of all currently running scripts. 
Read only status properties:
Property Description
owner (string) User who is running script
policy (array) List of all policies applied to script
started (date) Local date and time when script was started
See also
Scripting Examples
User submitted Scripts