ROS SCript课程系列1

RouterOS SCript课程系列

第63课程 RouterOS Script语言

RouterOS script语言是RouterOS内置的,功能强大,语法灵活,脚本经常与命令结合,通过脚本,可以实现自动化操作,提高工作效率等,

RouterOS脚本语言非常的强大,是整个RouterOS的核心,是精通RouterOS的必由之路。

熟悉Script脚本的前提是熟悉RouterOS的CLI命令行操作。我们将利用几节课的时间来进行学习,那现在就开始吧。


我们从定义变量开始学习。

在RouterOS中,定义变量通过关键字:global和local来实现。

1、变量的定义,引用,作用域

global 用于定义全局变量,
local用于定于局部变量,
变量是有作用范围的。

定义变量是有要求的:

1、有字母和数字,下划线等的组合;abc,8day “a_8day” "a-8day"
2、大小写敏感;Asum,asum是不同的
3、不能用保留字或者关键字,特殊字符;#, ?,

:global global "this is a global";

数组的定义及引用

{:local a { "aX"=1 ; ay=2 }; :put ($a->"aX")}

#有效
:local myVar;
#无效
:local my-var;
#有效,括号引起来
:global "my-var";


变量的赋值:set
变量的引用 $

例如:

:global a;
:set a 10;
:put $a;

{
:local a 3;
{
:local b 4;
:put ($a+$b);
}
#b是浅红色,是未定义变量,所有会输出7,3
:put ($a+$b);
}

##7,3

{
:local a 3;
{
:global b 4;
}
:put ($a+$b);
}


# 这里注意的b的范围


2、RouterOS脚本语言数据类型

类型 描述
num (number) - 64bit signed integer, possible hexadecimal input;
bool (boolean) - values can bee true or false;
str (string) - character sequence;
ip - IP address; xxx.xxx.xxx.xxx
ip-prefix - IP prefix;
ip6 - IPv6 address
ip6-prefix - IPv6 prefix
id (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;
nil - default variable type if no value is assigned;

:global tt 2345;
:global ipa 192.168.10.2; #ip
:global ipd 192.168.10.1/24; #ip-prefix
:global ipd 192.168.10.1-24; #str
:global ta true;

3、脚本与命令:


RouterOS脚本分为多个命令行。命令行将一一执行,直到脚本结束或发生运行时错误为止。

一般格式:

[prefix] [path] command [uparam] [param=[value]] .. [param=[value]]

[prefix] - 以 ":"或者 "/" 开始
[path] - 菜单对应的路径,随时可以用?来获取帮助
command - 命令
[uparam] - 如果命令需要指定的参数
[params] - param=[value]

多条命令之间需要用 ; 隔开

可以使用() [ ] {}等

[ ] 用来保存命令执行后的结果

{ } 脚本块,或作用域

( ) 括号

比如:

注释 #

连接


:if (1=1) do={:put "haha..."}

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

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

关键字 and or in from to等等 不能定义为变量或者函数名

界定符 : ( ) [ ] { } : ; $ /


4、转义字符:

" 双引号
\ 反斜杠
换行符
回车
水平tab
$ Output $ character. Otherwise $ is used to link variable.
? Output ? character. Otherwise ? is used to print "help" in console.
\_ - space
a - BEL (0x07)
 - backspace (0x08)
f - 换页
v 垂直tab
xx Print character from hex value. Hex number should use capital letters.


具体例子::put "48454C4C4F This is a test";

HELLO
This
is
a
test

5、运算符:


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

6、比较符:(关系运算)

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

6、逻辑运算 (或,与,非)

“!” 非,取反 :put (!true);

“&&” , “and” AND :put (true&&true)

“||” , “or” OR :put (true||false);

“in” :put (1.1.1.1/32 in 1.0.0.0/8);


7、位运算符:

“~” 位反转 :put (~0.0.0.0)

“ |” 按位或。

对每对对应位执行逻辑或运算。在每一对中,如果一位或两位都为“ 1”,则结果为“ 1”,否则结果为“ 0”。
例如 :put (192.168.88.0|0.0.0.255)


“ ^” 按位异或。与OR相同,但如果两个位不相等,则每个位置的结果为“ 1”,如果两个位不相等,则结果为“ 0”。
例如:put (1.1.1.1^255.255.0.0)


“&” 按位与。如果第一和第二位为“ 1”,则每对结果为“ 1”。否则结果为“ 0”。
例如:put (192.168.88.77&255.255.255.0)
“ <<” 左移给定的位数,不支持IPv6地址数据类型。
例如:put (192.168.88.77<<8)

“ >>” 按给定的位数右移,IPv6地址数据类型不支持
例如:put (192.168.88.77>>24)


连接符 : . 字符串连接符


数组操作符:“->” 数组连接符

:global aaa {a=1;b=2} #aaa[a]=1

:put ($aaa->"a")
1
:put ($aaa->"b")
2

原文地址:https://www.cnblogs.com/itfat/p/12929783.html