Difference between revisions of "Manual:Scripting"

From MikroTik Wiki
Jump to: navigation, search
Line 434: Line 434:
 
   <td><code><nowiki>:delay <time></nowiki></code></td>
 
   <td><code><nowiki>:delay <time></nowiki></code></td>
 
   <td>do nothing for a given period of time</td>
 
   <td>do nothing for a given period of time</td>
   <td><code><nowiki></nowiki></code></td>
+
   <td><code></code></td>
 
</tr>
 
</tr>
 
<tr>
 
<tr>
Line 440: Line 440:
 
   <td><code><nowiki>:put <expression></nowiki></code></td>
 
   <td><code><nowiki>:put <expression></nowiki></code></td>
 
   <td>put supplied argument to console</td>
 
   <td>put supplied argument to console</td>
   <td><code><nowiki></nowiki></code></td>
+
   <td><code></code></td>
 
</tr>
 
</tr>
 
<tr>
 
<tr>
Line 446: Line 446:
 
   <td><code><nowiki>:len <expression></nowiki></code></td>
 
   <td><code><nowiki>:len <expression></nowiki></code></td>
 
   <td>return string length or array element count</td>
 
   <td>return string length or array element count</td>
   <td><code><nowiki>:put [:len "length=8"];</nowiki></code></td>
+
   <td><code>:put [:len "length=8"];</code></td>
 
</tr>
 
</tr>
 
<tr>
 
<tr>
Line 452: Line 452:
 
   <td><code><nowiki>:typeof <var></nowiki></code></td>
 
   <td><code><nowiki>:typeof <var></nowiki></code></td>
 
   <td>return data type of variable</td>
 
   <td>return data type of variable</td>
   <td><code><nowiki></nowiki></code></td>
+
  <td><code>:put [:typeof 4];</code></td>
 +
</tr>
 +
<tr>
 +
  <td><b><var>pick</var></b></td>
 +
   <td><code><nowiki>:pick <var> <start>[<end>]</nowiki></code></td>
 +
  <td>return range of elements or substring. If end position is not specified, will return all elements from given position to end of string or array.</td>
 +
  <td><code>:put [:pick "abcde" 1 3]</code></td>
 +
</tr>
 +
<tr>
 +
  <td><b><var>log</var></b></td>
 +
  <td><code><nowiki>:log <topic> <message></nowiki></code></td>
 +
  <td>write message to system log. Available topics are [[system logging]]</td>
 +
  <td><code>:log info "Hello from script";</code></td>
 +
</tr>
 +
<tr>
 +
  <td><b><var>time</var></b></td>
 +
  <td><code><nowiki>:time <expression></nowiki></code></td>
 +
  <td>return interval of time needed to execute command</td>
 +
  <td><code>:put [:time {:for i from=1 to=10 do={ :delay 100ms }}];</code></td>
 +
</tr>
 +
<tr>
 +
  <td><b><var>set</var></b></td>
 +
  <td><code><nowiki>:set <var> [<value>]</nowiki></code></td>
 +
  <td>assign value to declared variable. Ja set bez veertiibas tad typof ir 'nil' vajadzetu but nothing.</td>
 +
  <td><code>:global a; :set a true;</code></td>
 +
</tr>
 +
<tr>
 +
  <td><b><var>find</var></b></td>
 +
  <td><code><nowiki>:find <arg> <arg> <start></nowiki></code></td>
 +
  <td>return position of substring or array element</td>
 +
  <td><code>:put [:find "abc" "a" -1]; #kaapeec -1?? jaabuut 0</code></td>
 +
</tr>
 +
<tr>
 +
  <td><b><var>environment</var></b></td>
 +
  <td><code><nowiki>:environment print <start></nowiki></code></td>
 +
  <td>print initialized variable information</td>
 +
  <td><code>:global myVar true; :environment print;</code></td>
 +
</tr>
 +
<tr>
 +
  <td><b><var>error</var></b></td>
 +
  <td><code><nowiki>:error <output> </nowiki></code></td>
 +
  <td>generate console error</td>
 +
  <td><code></code></td>
 +
</tr>
 +
<tr>
 +
  <td><b><var>write</var></b></td>
 +
  <td><code><nowiki> </nowiki></code></td>
 +
  <td>izmests</td>
 +
  <td><code></code></td>
 +
</tr>
 +
<tr>
 +
  <td><b><var>parse</var></b></td>
 +
  <td><code><nowiki>:parse <expression> </nowiki></code></td>
 +
  <td>parse string and return parsed console commands. (parsees arii LUA)</td>
 +
  <td><code></code></td>
 +
</tr>
 +
<tr>
 +
  <td><b><var>resolve</var></b></td>
 +
  <td><code><nowiki>:resolve <arg> </nowiki></code></td>
 +
  <td>return IP address of given DNS name</td>
 +
  <td><code>:put [:resolve "www.google.lv"];</code></td>
 +
</tr>
 +
<tr>
 +
  <td><b><var>toarray</var></b></td>
 +
  <td><code><nowiki>:toarray <var> </nowiki></code></td>
 +
  <td>convert variable to array</td>
 +
  <td><code></code></td>
 +
</tr>
 +
<tr>
 +
  <td><b><var>tobool</var></b></td>
 +
  <td><code><nowiki>:tobool <var> </nowiki></code></td>
 +
  <td>convert variable to boolean</td>
 +
  <td><code></code></td>
 +
</tr>
 +
<tr>
 +
  <td><b><var>toid</var></b></td>
 +
  <td><code><nowiki>:toid <var> </nowiki></code></td>
 +
  <td>convert variable to internal ID</td>
 +
  <td><code></code></td>
 +
</tr>
 +
<tr>
 +
  <td><b><var>toip</var></b></td>
 +
  <td><code><nowiki>:toip <var> </nowiki></code></td>
 +
  <td>convert variable to IP address</td>
 +
  <td><code></code></td>
 +
</tr>
 +
<tr>
 +
  <td><b><var>tonum</var></b></td>
 +
  <td><code><nowiki>:tonum <var> </nowiki></code></td>
 +
  <td>convert variable to integer</td>
 +
  <td><code></code></td>
 +
</tr>
 +
<tr>
 +
  <td><b><var>tostr</var></b></td>
 +
  <td><code><nowiki>:tostr <var> </nowiki></code></td>
 +
  <td>convert variable to string</td>
 +
  <td><code></code></td>
 +
</tr>
 +
<tr>
 +
  <td><b><var>totime</var></b></td>
 +
  <td><code><nowiki>:totime <var> </nowiki></code></td>
 +
  <td>convert variable to time</td>
 +
  <td><code></code></td>
 +
</tr>
 +
<tr>
 +
  <td><b><var>break</var></b></td>
 +
  <td><code><nowiki>:break </nowiki></code></td>
 +
  <td>It terminates the nearest enclosing loop. If a for loop is terminated by break, the loop control target keeps its current value. break may only occur syntactically nested in a [[for]] or [[while]] [[loop]].</td>
 +
  <td><code></code></td>
 +
</tr>
 +
<tr>
 +
  <td><b><var>continue</var></b></td>
 +
  <td><code><nowiki>:continue </nowiki></code></td>
 +
  <td>It continues with the next cycle of the nearest enclosing loop. continue may only occur syntactically nested in a [[for]] or [[while]] [[loop]].</td>
 +
  <td><code></code></td>
 
</tr>
 
</tr>
 
</table>
 
</table>
 
</p>
 
</p>
 +
 +
<h4>Menu specific commands</h4>
 +
<h3>Common commands</h3>
 +
<p>
 +
</p>
 +
 
</div>
 
</div>
  

Revision as of 14:05, 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 :put [:typeof 4];
pick :pick <var> <start>[<end>] return range of elements or substring. If end position is not specified, will return all elements from given position to end of string or array. :put [:pick "abcde" 1 3]
log :log <topic> <message> write message to system log. Available topics are system logging :log info "Hello from script";
time :time <expression> return interval of time needed to execute command :put [:time {:for i from=1 to=10 do={ :delay 100ms }}];
set :set <var> [<value>] assign value to declared variable. Ja set bez veertiibas tad typof ir 'nil' vajadzetu but nothing. :global a; :set a true;
find :find <arg> <arg> <start> return position of substring or array element :put [:find "abc" "a" -1]; #kaapeec -1?? jaabuut 0
environment :environment print <start> print initialized variable information :global myVar true; :environment print;
error :error <output> generate console error
write izmests
parse :parse <expression> parse string and return parsed console commands. (parsees arii LUA)
resolve :resolve <arg> return IP address of given DNS name :put [:resolve "www.google.lv"];
toarray :toarray <var> convert variable to array
tobool :tobool <var> convert variable to boolean
toid :toid <var> convert variable to internal ID
toip :toip <var> convert variable to IP address
tonum :tonum <var> convert variable to integer
tostr :tostr <var> convert variable to string
totime :totime <var> convert variable to time
break :break It terminates the nearest enclosing loop. If a for loop is terminated by break, the loop control target keeps its current value. break may only occur syntactically nested in a for or while loop.
continue :continue It continues with the next cycle of the nearest enclosing loop. continue may only occur syntactically nested in a for or while loop.

Menu specific commands

Common commands