Manual:Scripting: Difference between revisions

From MikroTik Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 89: Line 89:
</nowiki></pre>
</nowiki></pre>
</p>
</p>
<h3>Data types</h3>
<h3>Data types</h3>
<p>
<p>
RouterOS scripting language has following data types:
RouterOS scripting language has following data types:
<table>
<table class="styled_table">
<tr>
  <th width="150">Type</th>
  <th >Description</th>
</tr>
<tr>
<tr>
<td width="30"></td><td width="150"><b><var>number</var></b></td><td >- 64bit signed integer, possible hexadecimal input;</td>
  <td ><b><var>number</var></b></td>
</tr><tr>
  <td >- 64bit signed integer, possible hexadecimal input;</td>
<td width="30"></td><td width="150"><b><var>boolean</var></b></td><td >- values can bee <code>true</code> or <code>false</code>;</td>
</tr>
</tr><tr>
<tr>
<td width="30"></td><td width="150"><b><var>string</var></b></td><td >- character sequence (??? kaads ir memas limits ???);</td>
  <td ><b><var>boolean</var></b></td>
</tr><tr>
  <td >- values can bee <code>true</code> or <code>false</code>;</td>
<td width="30"></td><td width="150"><b><var>IP</var></b></td><td >- IP address;</td>
</tr>
</tr><tr>
<tr>
<td width="30"></td><td width="150"><b><var>internal ID</var></b></td><td >- hexadecimal value prefixed by '*' sign. Each menu item has assigned unique number - internal ID;</td>
  <td ><b><var>string</var></b></td>
</tr><tr>
  <td >- character sequence (??? kaads ir memas limits ???);</td>
<td width="30"></td><td width="150"><b><var>time</var></b></td><td >- date and time value;</td>
</tr>
</tr><tr>
<tr>
<td width="30"></td><td width="150"><b><var>array</var></b></td><td >- sequence of values organized in an array;</td>
  <td ><b><var>IP</var></b></td>
</tr><tr>
  <td >- IP address;</td>
<td width="30"></td><td width="150"><b><var>nothing</var></b></td><td >- default variable type if no value is assigned;</td>
</tr>
<tr>
  <td ><b><var>internal ID</var></b></td>
  <td >- hexadecimal value prefixed by '*' sign. Each menu item has assigned unique number - internal ID;</td>
</tr>
<tr>
  <td ><b><var>time</var></b></td>
  <td >- date and time value;</td>
</tr>
<tr>
  <td ><b><var>array</var></b></td>
  <td >- sequence of values organized in an array;</td>
</tr>
<tr>
  <td ><b><var>nothing</var></b></td>
  <td >- default variable type if no value is assigned;</td>
</tr>
</tr>
</table>
</table>
</p>


</p>
<h4>Constant Escape Sequences</h4>
<h4>Constant Escape Sequences</h4>
<p>
<p>
Following escape sequences can be used to define certain special character within string:
Following escape sequences can be used to define certain special character within string:
<table>
<table class="styled_table">
<tr>
  <td width="150"><b>\'</b></td>
  <td > - single quote</td>
</tr>
<tr>
  <td ><b>\"</b></td>
  <td > - double quote</td>
</tr>
<tr>
<tr>
<td width="30"></td><td width="150"><b>\'</b></td><td > - single quote</td>
  <td ><b>\\</b></td>
</tr><tr>
  <td > - backslash</td>
<td width="30"></td><td width="150"><b>\"</b></td><td > - double quote</td>
</tr>
</tr><tr>
<tr>
<td width="30"></td><td width="150"><b>\\</b></td><td > - backslash</td>
  <td ><b>\n</b></td>
</tr><tr>
  <td > - newline</td>
<td width="30"></td><td width="150"><b>\n</b></td><td > - newline</td>
</tr>
</tr><tr>
<tr>
<td width="30"></td><td width="150"><b>\r</b></td><td > - carriage return</td>
  <td ><b>\r</b></td>
</tr><tr>
  <td > - carriage return</td>
<td width="30"></td><td width="150"><b>\t</b></td><td > - horizontal tab</td>
</tr>
</tr><tr>
<tr>
<td width="30"></td><td width="150"><b>\$</b></td><td ></td>
  <td ><b>\t</b></td>
  <td > - horizontal tab</td>
</tr>
<tr>
  <td ><b>\$</b></td>
  <td ></td>
</tr>
</tr>
</table>
</table>
Line 147: Line 181:
( MRZ: bugs vai fiicha ??? ka whitespaci katras jaunas rindas priekshaa??? ).
( MRZ: bugs vai fiicha ??? ka whitespaci katras jaunas rindas priekshaa??? ).
</p>
</p>


<h3>Operators</h3>
<h3>Operators</h3>
<p>
lala
lala
</p>
<h4>Arithmetic Operators</h4>
<h4>Arithmetic Operators</h4>
<p>
<p>
Usual arithmetic operators are supported in RouterOS scripting language<br />
Usual arithmetic operators are supported in RouterOS scripting language<br />


<table border=0 bgcolor=#4f4f4f>
<table class="styled_table">
<tr border=0 bgcolor=#cccccc>
<tr>
  <th bgcolor=#ffffff width="30" align="left"></th>
   <th width="100">Opearator</th>
   <th width="100" align="left">Opearator</th>
   <th width="300">Description</th>
   <th width="300" align="left">Description</th>
   <th >Example</th>
   <th align="left">Example</th>
</tr>
</tr>
<tr border=0 bgcolor=#f4f4f4>
<tr>
   <td bgcolor=#ffffff width="30"></td>
   <td><b><var>"+"</var></b></td>
  <td width="100"><b><var>"+"</var></b></td>
   <td>binary addition</td>
   <td width="300">binary addition</td>
   <td><code>:put (3+4);</code></td>
   <td><code>:put (3+4);</code></td>
</tr>
</tr>
<tr border=0 bgcolor=#f4f4f4>
<tr>
   <td bgcolor=#ffffff width="30"></td>
   <td><b><var>"-"</var></b></td>
  <td width="100"><b><var>"-"</var></b></td>
   <td>binary subtraction</td>
   <td width="300">binary subtraction</td>
   <td><code>:put (1-6);</code></td>
   <td><code>:put (1-6);</code></td>
</tr>
</tr>
<tr border=0 bgcolor=#f4f4f4>
<tr>
   <td bgcolor=#ffffff width="30"></td>
   <td><b><var>"*"</var></b></td>
  <td width="100"><b><var>"*"</var></b></td>
   <td>binary multiplication</td>
   <td width="300">binary multiplication</td>
   <td><code>:put (4*5);</code></td>
   <td><code>:put (4*5);</code></td>
</tr>
</tr>
<tr border=0 bgcolor=#f4f4f4>
<tr>
   <td bgcolor=#ffffff width="30"></td>
   <td><b><var>"/"</var></b></td>
  <td width="100"><b><var>"/"</var></b></td>
   <td>binary division</td>
   <td width="300">binary division</td>
   <td><code>:put (10/2);</code></td>
   <td><code>:put (10/2);</code></td>
</tr>
</tr>
<tr border=0 bgcolor=#f4f4f4>
<tr>
   <td bgcolor=#ffffff width="30"></td>
   <td><b><var>"-"</var></b></td>
  <td width="100"><b><var>"-"</var></b></td>
   <td>unary negation</td>
   <td width="300">unary negation</td>
   <td><code>{ :local a 1; :put (-a); }</code></td>
   <td><code>{ :local a 1; :put (-a); }</code></td>
</tr>
</tr>
<tr border=0 bgcolor=#f4f4f4>
<tr>
   <td bgcolor=#ffffff width="30"></td>
   <td><b><var>"+"</var></b></td>
  <td width="100"><b><var>"+"</var></b></td>
   <td>unary plus</td>
   <td width="300">unary plus</td>
   <td></td>
   <td></td>
</tr>
</tr>
</table>
</table>
</p>
</p>


<h4>Relational Operators</h4>
<h4>Relational Operators</h4>
<p>
<p>
<table border=0 bgcolor=#4f4f4f>
 
<tr border=0 bgcolor=#cccccc>
<table class="styled_table">
  <th bgcolor=#ffffff width="30" align="left"></th>
<tr>
   <th width="100" align="left">Opearator</th>
   <th width="100">Opearator</th>
   <th width="300" align="left">Description</th>
   <th width="300">Description</th>
   <th align="left">Example</th>
   <th>Example</th>
</tr>
</tr>
<tr border=0 bgcolor=#f4f4f4>
<tr>
   <td bgcolor=#ffffff width="30"></td>
   <td><b><var>"<"</var></b></td>
  <td width="100"><b><var>"<"</var></b></td>
   <td>less</td>
   <td width="300">less</td>
   <td><code>:put (3<4);</code></td>
   <td><code>:put (3<4);</code></td>
</tr>
</tr>
<tr border=0 bgcolor=#f4f4f4>
<tr>
   <td bgcolor=#ffffff width="30"></td>
   <td><b><var>">"</var></b></td>
  <td width="100"><b><var>">"</var></b></td>
   <td>greater</td>
   <td width="300">greater</td>
   <td><code>:put (3>4);</code></td>
   <td><code>:put (3>4);</code></td>
</tr>
</tr>
<tr border=0 bgcolor=#f4f4f4>
<tr>
   <td bgcolor=#ffffff width="30"></td>
   <td><b><var>"="</var></b></td>
  <td width="100"><b><var>"="</var></b></td>
   <td>equal</td>
   <td width="300">equal</td>
   <td><code>:put (2=2);</code></td>
   <td><code>:put (2=2);</code></td>
</tr>
</tr>
<tr border=0 bgcolor=#f4f4f4>
<tr>
   <td bgcolor=#ffffff width="30"></td>
   <td><b><var>"<="</var></b></td>
  <td width="100"><b><var>"<="</var></b></td>
   <td>less or equal</td>
   <td width="300">less or equal</td>
   <td></td>
   <td></td>
</tr>
</tr>
<tr border=0 bgcolor=#f4f4f4>
<tr>
   <td bgcolor=#ffffff width="30"></td>
   <td><b><var>">="</var></b></td>
  <td width="100"><b><var>">="</var></b></td>
   <td>greater or equal</td>
   <td width="300">greater or equal</td>
   <td></td>
   <td></td>
</tr>
</tr>
<tr border=0 bgcolor=#f4f4f4>
<tr>
   <td bgcolor=#ffffff width="30"></td>
   <td><b><var>"!="</var></b></td>
  <td width="100"><b><var>"!="</var></b></td>
   <td>not equal</td>
   <td width="300">not equal</td>
   <td></td>
   <td></td>
</tr>
</tr>
Line 252: Line 276:
<h4>Logical Operators</h4>
<h4>Logical Operators</h4>
<p>
<p>
<table border=0 bgcolor=#4f4f4f>
<table class="styled_table">
<tr border=0 bgcolor=#cccccc>
<tr>
  <th bgcolor=#ffffff width="30" align="left"></th>
   <th width="100">Opearator</th>
   <th width="100" align="left">Opearator</th>
   <th width="300">Description</th>
   <th width="300" align="left">Description</th>
   <th >Example</th>
   <th align="left">Example</th>
</tr>
</tr>
<tr border=0 bgcolor=#f4f4f4>
<tr>
   <td bgcolor=#ffffff width="30"></td>
   <td><b><var>“!” , “not”</var></b></td>
  <td width="100"><b><var>“!” , “not”</var></b></td>
   <td>logical NOT</td>
   <td width="300">logical NOT</td>
   <td><code>:put (!true);</code></td>
   <td><code>:put (!true);</code></td>
</tr>
</tr>
<tr border=0 bgcolor=#f4f4f4>
<tr>
   <td bgcolor=#ffffff width="30"></td>
   <td><b><var>“&&” , “and”</var></b></td>
  <td width="100"><b><var>“&&” , “and”</var></b></td>
   <td>logical AND</td>
   <td width="300">logical AND</td>
   <td><code>:put (true&&true)</code></td>
   <td><code>:put (true&&true)</code></td>
</tr>
</tr>
<tr border=0 bgcolor=#f4f4f4>
<tr>
   <td bgcolor=#ffffff width="30"></td>
   <td><b><var>“||” , “or”</var></b></td>
  <td width="100"><b><var>“||” , “or”</var></b></td>
   <td>logical OR</td>
   <td width="300">logical OR</td>
   <td><code>:put (true||false);</code></td>
   <td><code>:put (true||false);</code></td>
</tr>
</tr>
Line 301: Line 321:
</tr>
</tr>
<tr>
<tr>
   <td><b><var>“^”</var></b></td>
   <td><b><var>“^”</var></b></td>
   <td>bitwise XOR. The same as OR, but the result in each position is “1” if two bits are not equal, and “0” if bits are equal.</td>
   <td>bitwise XOR. The same as OR, but the result in each position is “1” if two bits are not equal, and “0” if bits are equal.</td>
Line 326: Line 345:
<h4>Concatenation Operators</h4>
<h4>Concatenation Operators</h4>
<p>
<p>
<table border=0 bgcolor=#4f4f4f>
<table class="styled_table">
<tr border=0 bgcolor=#cccccc>
<tr>
  <th bgcolor=#ffffff width="30" align="left"></th>
   <th width="100">Opearator</th>
   <th width="100" align="left">Opearator</th>
   <th width="300">Description</th>
   <th width="300" align="left">Description</th>
   <th>Example</th>
   <th align="left">Example</th>
</tr>
</tr>
<tr border=0 bgcolor=#f4f4f4>
<tr>
   <td bgcolor=#ffffff width="30"></td>
   <td><b><var>“.”</var></b></td>
  <td width="100"><b><var>“.”</var></b></td>
   <td>concatenates two strings</td>
   <td width="300">concatenates two strings</td>
   <td><code>:put (“concatenate” . “ “ . “string”);</code></td>
   <td><code>:put (“concatenate” . “ “ . “string”);</code></td>
</tr>
</tr>
Line 346: Line 363:
<h4>Other Operators</h4>
<h4>Other Operators</h4>
<p>
<p>
<table border=0 bgcolor=#4f4f4f>
 
<tr border=0 bgcolor=#cccccc>
<table class="styled_table">
  <th bgcolor=#ffffff width="30" align="left"></th>
<tr>
   <th width="100" align="left">Opearator</th>
   <th width="100">Opearator</th>
   <th width="300" align="left">Description</th>
   <th width="300">Description</th>
   <th align="left">Example</th>
   <th>Example</th>
</tr>
</tr>
<tr border=0 bgcolor=#f4f4f4>
<tr>
   <td bgcolor=#ffffff width="30"></td>
   <td><b><var>“[]”</var></b></td>
  <td width="100"><b><var>“[]”</var></b></td>
   <td>nez kaa lai shito zveeru nosauc</td>
   <td width="300">nez kaa lai shito zveeru nosauc</td>
   <td><code>:put [ :len "my test string"; ];</code></td>
   <td><code>:put [ :len "my test string"; ];</code></td>
</tr>
</tr>
<tr border=0 bgcolor=#f4f4f4>
<tr>
   <td bgcolor=#ffffff width="30"></td>
   <td><b><var>“()”</var></b></td>
  <td width="100"><b><var>“()”</var></b></td>
   <td>group operator</td>
   <td width="300">group operator</td>
   <td><code>:put ( "value is " . (4+5));</code></td>
   <td><code>:put ( "value is " . (4+5));</code></td>
</tr>
</tr>
<tr border=0 bgcolor=#f4f4f4>
<tr>
   <td bgcolor=#ffffff width="30"></td>
   <td><b><var>“,”</var></b></td>
  <td width="100"><b><var>“,”</var></b></td>
   <td>sequential evaluation operator</td>
   <td width="300">sequential evaluation operator</td>
   <td><code>target-addresses=1.1.1.1,2.2.2.2</code></td>
   <td><code>target-addresses=1.1.1.1,2.2.2.2</code></td>
</tr>
</tr>
</table>
</table>
</p>
</p>
</div>
 


<h3>Commands</h3>
<h3>Commands</h3>

Revision as of 11:27, 26 September 2008

Scripting language manual

lala

Line structure

RouterOS script is divided into number of logical lines.

Logical line

The end of logical line is represented by the token “;” or NEWLINE. Logical line can be constructed from more than one physical line by following line joining rules.

Physical Line

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:

  • unix – ASCII LF;
  • windows – ASCII CR LF;
  • mac – ASCII CR;

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

Comments

A comment starts with a hash character (#) and ends at the end of the physical line. A comment signifies the end of the logical line unless line joining rules are invoked ( nav taisniiba, skat piemeerus). Comments are ignored by syntax. If (#) character appear inside string it is not considered a comment.

Example
# this is a comment

:global a; # bad comment (MRZ: shitadu vajadzetu atlaut)

:global myStr "lala # this is not a comment"


Line joining

Two or more physical lines may be joined into logical lines using backslash character (\). A line ending in a backslash cannot carry a comment. A backslash does not continue a comment. A backslash does not continue a token except for string literals. (MRZ: nezinu vai shii ir taisniiba, buutu logiski ja taa buutu). A backslash is illegal elsewhere on a line outside a string literal.

Example
:if ($a = true \
      and $b=false) do={ :put “$a $b”; }

:if ($a = true \      # bad comment
      and $b=false) do={ :put “$a $b”; }

# comment \
    continued – invalid  (syntax error)


Whitespace between tokens

Whitespace can be used to separate tokens. Whitespace is necessary between two tokens only if their concatenation could be interpreted as a different token.

Example
{ 
   :local a true; :local b false;	
   :put (a&&b); # whitespace is not required	
   :put (a and b); # whitespace is required	
}


Keywords

( Vai RouterOSaa vispaar ir keywordi??? )
The following words are keywords and cannot be used as variable and function names:

and       or        not       break
continue  do        for       foreach
in        from      to        while
global    local     if        else

(break un continue vajadzeetu ieviest)
(varbuut veel kaads?)

Delimiters

The following tokens serve as delimiters in the grammar:

()  []  {}  :   ;   $   / 


Data types

RouterOS scripting language has following data types:

Type Description
number - 64bit signed integer, possible hexadecimal input;
boolean - values can bee true or false;
string - character sequence (??? kaads ir memas limits ???);
IP - IP address;
internal ID - hexadecimal value prefixed by '*' sign. Each menu item has assigned unique number - internal ID;
time - date and time value;
array - sequence of values organized in an array;
nothing - default variable type if no value is assigned;

Constant Escape Sequences

Following escape sequences can be used to define certain special character within string:

\' - single quote
\" - double quote
\\ - backslash
\n - newline
\r - carriage return
\t - horizontal tab
\$

(MRZ: varbuut veel kaads)

Example
:put "This\nis\na\ntest";

which will show on display
This
is
a
test
( MRZ: bugs vai fiicha ??? ka whitespaci katras jaunas rindas priekshaa??? ).


Operators

lala

Arithmetic Operators

Usual arithmetic operators are supported in RouterOS scripting language

Opearator Description Example
"+" binary addition :put (3+4);
"-" binary subtraction :put (1-6);
"*" binary multiplication :put (4*5);
"/" binary division :put (10/2);
"-" unary negation { :local a 1; :put (-a); }
"+" unary plus

Relational Operators

Opearator Description Example
"<" less :put (3<4);
">" greater :put (3>4);
"=" equal :put (2=2);
"<=" less or equal
">=" greater or equal
"!=" not equal

Logical Operators

Opearator Description Example
“!” , “not” logical NOT :put (!true);
“&&” , “and” logical AND :put (true&&true)
“||” , “or” logical OR :put (true||false);

Bitwise Operators

Laikam no v3.12 visi bitwise operatori nav attiecinaami tikai uz IP adreseem, bet arii citiem mainigajiem.

Opearator Description Example
“~” bit inversion :put (~0.0.0.0)
“|” bitwise OR. Performs logical OR operation on each pair of corresponding bits. In each pair the result is “1” if one of bits or both bits are “1”, otherwise the result is “0”.
“^” bitwise XOR. The same as OR, but the result in each position is “1” if two bits are not equal, and “0” if bits are equal.
“&” bitwise AND. In each pair the result is “1” if first and second bit is “1”. Otherwise the result is “0”.
“<<” left shift by given amount of bits
“>>” right shift by given amount of bits

Concatenation Operators

Opearator Description Example
“.” concatenates two strings :put (“concatenate” . “ “ . “string”);

Arrayiem??

Other Operators

Opearator Description Example
“[]” nez kaa lai shito zveeru nosauc :put [ :len "my test string"; ];
“()” group operator :put ( "value is " . (4+5));
“,” sequential evaluation operator target-addresses=1.1.1.1,2.2.2.2


Commands

Global commands

Every global command should start with ":" token, otherwise it will be treated as variable. ( Velak laikam buus ka vares rakstit komandas bez ";"

Command Syntax Description Example
/ go to root menu
.. go back by one menu level
global :global <var> [<value>] define global variable :global myVar "something"; :put $myVar;
local :local <var> [<value>] define 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> return data type of variable