最新ros脚本详细解释

最新ros脚本详细解释时间:2010-05-24 21:56来源:深圳网吧联盟 作者:admin 点击:130次因为下面这个问题,我觉得 脚本 详细解释也许对大家有用处,决定把以前 网络 时候找的资料发上来,希望对大家有用处 [apple@sgg-gatewy] :put (([/ip firewall address-list get 0 address ] 255.255 .255.0) . \/24) 202.103.24.0/24 这样使用put可以实现把 202.103.24.0 和/24 结合成一个整体 但是怎么样把这个新的值202.103.24.0/24赋给一个变量呢,set不能完成,谁有好的建议或者
因为下面这个问题,我觉得脚本详细解释也许对大家有用处,决定把以前网络时候找的资料发上来,希望对大家有用处

[apple@sgg-gatewy] > :put (([/ip firewall address-list get 0 address ] & 255.255
.255.0) . \/24)
202.103.24.0/24
这样使用put可以实现把
202.103.24.0
和/24
结合成一个整体
但是怎么样把这个新的值202.103.24.0/24赋给一个变量呢,set不能完成,谁有好的建议或者意见
我准备做的是通过vpn客户端的访问源地址,自动添加/ip route rule里面的数据
现在可以自动把202.103.24.68这样的具体ip自动添加,但是我想根据这个ip然后自动添加一个标准c段

[apple@sgg-gatewy] > :environment print
Global Variables
Local Variables
[apple@sgg-gatewy] > :put (([/ip firewall address-list get 0 address ] & 255.255.255.0) . \/24)
221.232.119.0/24
[apple@sgg-gatewy] > :global testip
[apple@sgg-gatewy] > :put $testip

[apple@sgg-gatewy] > :set testip 202.103.24.0/24
[apple@sgg-gatewy] > :put $testip
202.103.24.0/24
[apple@sgg-gatewy] > :set testip [:put (([/ip firewall address-list get 0 address ] & 255.255.255.0) . \/24)]
221.232.119.0/24
[apple@sgg-gatewy] > :put $testip

[apple@sgg-gatewy] > :set testip {[:put (([/ip firewall address-list get 0 address ] & 255.255.255.0) . \/24)]}
[apple@sgg-gatewy] > :put $testip
[:put (([/ip firewall address-list get 0 address ] & 255.255.255.0) . \/24)]
[apple@sgg-gatewy] > :set testip ([:put (([/ip firewall address-list get 0 address ] & 255.255.255.0) . \/24)])
221.232.119.0/24
[apple@sgg-gatewy] > :put $testip

[apple@sgg-gatewy] > :environment print
Global Variables
Local Variables
testip=

现在的需要解决的是,使用什么方法,或者用别的命令来实现把连接好了的新数字赋给testip这个变量
直接脚本里面写dst-address=[:put (([/ip firewall address-list get 0 address ] & 255.255.255.0) . \/24)]
也是无效的的,测试过了的
用put,直接输出了,不能完成赋值
不用put,不能连接两个字段
大家一起

思考思考,先表示感谢


:foreach i in=[/ip firewall address-list find list=telvpn-stable ] do=[/ip route rule add dst-address=(([/ip firewall address-list get $i address ] & 255.255.255.0) . \/24) table=tel action=lookup disabled=no comment="telvpn add by lcnja`s auto script" src-address=0.0.0.0/0]

呵呵,自己搞定了

---------------------------------------------------------------------------------------------------------------------------------------------

RouterOS2.96脚本详解
四种变量







global - 定义全局变量, 可以要所有的脚本中调用共享
local - 定义本地变量,只能在其所要的脚本下调用,不能被其它脚本共享
loop index variables - 定义在for或foreach里的索引号变量
monitor variables - 监视变量


ROS算术操作










- 负号;相减。
! 逻辑非。
/ 相除。
. 连接。两个符串的连接,添加元素到列表
^ 异或(XOR)
~ 取反
* 相剩
& 与(AND)
&& 逻辑与
+ 相加
< 小于
<< 向左位移
<= 小于等于
> 大于
>= 大于等于
>> 向右位移
| 或
|| 逻辑或
ROS说明书里的例子:






计算顺序







[admin@MikroTik]> :put (10+1-6*2=11-12=2+(-3)=-1)
false
[admin@MikroTik]> :put (10+1-6*2=11-12=(2+(-3)=-1))
true






逻辑非






[admin@MikroTik]> :put (!true)
false
[admin@MikroTik]> :put (!(2>3))
true






数位取反






[admin@MikroTik]> :put (~255.255.0.0)
0.0.255.255






加法






[admin@MikroTik]> :put (3ms + 5s)
00:00:05.003
[admin@MikroTik]> :put (10.0.0.15 + 0.0.10.0)
cannot add ip address to ip address
[admin@MikroTik]> :put (10.0.0.15 + 10)
10.0.0.25




减法









[admin@MikroTik]> :put (15 - 10)
5
[admin@MikroTik]> :put (10.0.0.15 - 10.0.0.3)
12
[admin@MikroTik]> :put (10.0.0.15 - 12)
10.0.0.3
[admin@MikroTik]> :put (15h - 2s)
14:59:58






乘法






[admin@MikroTik]> :put (12s * 4)
00:00:48
[admin@MikroTik]> :put (-5 * -2)
10






除法






[admin@MikroTik]> :put (10s / 3)
00:00:03.333
[admin@MikroTik]> :put (5 / 2)
2
[admin@MikroTik]> :put (0:0.10 / 3)
00:00:02






比较






[admin@MikroTik]> :put (10.0.2.3<=2.0.3.10)
false
[admin@MikroTik]> :put (100000s>27h)
true
[admin@MikroTik]> :put (60s,1d!=1m,3600s)
true
[admin@MikroTik]> :put (bridge=routing)
false
[admin@MikroTik]> :put (yes=false)
false
[admin@MikroTik]> :put (true=aye)
false






逻辑与 AND, 逻辑或 OR






[admin@MikroTik]> :put ((yes && yes) || (yes && no))
true
[admin@MikroTik]> :put ((no || no) && (no || yes))
false






数位与AND, 或OR, 异或XOR









[admin@MikroTik]> :put (10.16.0.134 & ~255.255.255.0)
0.0.0.134




位移操作









[admin@MikroTik]> :put (~((0.0.0.1 << 7) - 1))
255.255.2

55.128




连接操作








[admin@MikroTik]> :put (1 . 3)
13
[admin@MikroTik]> :put (1,2 . 3)
1,2,3
[admin@MikroTik]> :put (1 . 3,4)
13,4
[admin@MikroTik]> :put (1,2 . 3,4)
1,2,3,4
[admin@MikroTik]> :put ((1 . 3) + 1)
14
[admin@MikroTik]> :set a "It's "
[admin@MikroTik]> :put ($a . OK)
It's OK






ROS脚本保留字






beep execute global list pick time toip typeof
delay find if local put toarray tonum while
do for led log resolve tobool tostr
environment foreach len nothing set toid totime




声音和警报









:beep length=2s frequency=10000
产生2秒10kHz的音频
length缺省值为100ms
frequency缺省值为1000Hz



execute调用其他命令或者脚本











:set a "/int dis lan\n/int dis wan"
:execute $a
执行多条命令,例子里执行了两条命令。\n是换行



:global
定义全局变量的使用











:list interface
显示相关命令。显示当前目录及子目录下有关interface的命令



:pick 数组和字串的截取











取字符串或数组的某一断。字符串(数组)的第一个为0。
[admin@MikroTik]>:put [:pick "I love you" 2 6]
love






:time 计算命令执行所用时长






执行命令所需的时间
[admin@MikroTik]> :put [:time [:resole https://www.360docs.net/doc/1211885240.html,]]
00:00:00.006
执行解析https://www.360docs.net/doc/1211885240.html,这个域名所需的时间



数据类型转换











toip toarray tonum tobool tostr toid totime
转换值类型







延迟 多少秒






:delay 3
延时3秒,缺省为1秒






:find 查找功能






查找字符串或数组中第一个出现查找内容的位置
[admin@MikroTik]>:put [:find abcdcba cd]
2
[admin@MikroTik]>:put [:find "1,2,3,4,3,2,1" 2]
1



:put 屏幕输出









输出到屏幕上,上面就很多例子了。

:if 条件判断
条件选择
[admin@MikroTik]>:if(1<2) do={:put true}
true
如果条件为真,执行do={}里面的命令
[admin@MikroTik]>:if(1>2) do={:put true} else={:put flase}
flase
如果条件为真,执行do={}里面的命令,否则执行else={}里有命令
[/hide]




:local 局部变量的使用





定义本地变量
:local myip
局部变量只能在某个脚本或者命令行使用,其他的脚本或者命令行不能调用






:while 循环






条件为真时循环执行do={}里的脚本命令
[admin@MikroTik]>:set i 0;:while($i<5) do={:put $i;:set i ($i+1)}
0
1
2
3
4
5






:for 循环









循环执行do={}里的脚本命令
:for i from=1 to=100 step=30 do={:put $i}
1
31
61
91






:foreach 循环






在集合(数组)里循环执行do={}里的脚本命令
:foreach i in=[/interface find type=ether] do={:put [/interface get $i name]}
ether1

ether2
ether3
ether4
在[/interface find type=ether]的集合(类型为ether的interface ID)循环输出

interface的名称。我这时有四张网卡



:log 日志类型 日志内容
写日志操作









写文本到日志(script log)
类型在/system logging里可以找到
:log info "系统信息"
到LOG里查查运行结果吧







:resolve
域名解析







解析域名的IP地址
[admin@MikroTik] > :put [:resolve https://www.360docs.net/doc/1211885240.html,]
61.172.201.240










:len
字符串或数组的长度










[admin@MikroTik] > :put [:len hello]
5
[admin@MikroTik] > :put [:len "1,2,23,65,54,6"]
6



:nothing
空值。nothing不等0,不等于空字符""
:find abc a的结果是0
:find abc d的结果是nothing






:set
赋值




[admin@MikroTik] > :set a test
将abc字符赋给变量a
[admin@MikroTik] > :put $a
test
[admin@MikroTik] > :put a
a



变量的引用
引用变量的值要在变量前面加$
如下
[apple@sgg-gatewy] > :environment print
Global Variables
Local Variables
我们先查看一下环境,显示没有如何的全局或者局部变量
[apple@sgg-gatewy] > :global testip
我们定义一个全局变量
[apple@sgg-gatewy] > /ip firewall address-list print
Flags: X - disabled, D - dynamic
# LIST ADDRESS
0 D telvpn 221.232.119.228
1 telvpn-stable 221.232.119.228
查看一下address-list有没数据,因为我们下面使用序号查询了,所有首先需要使用pr命令显示一下,要不回失败,也可以不按序号查就可以不需要这个
[apple@sgg-gatewy] > :set testip (([/ip firewall address-list get 0 address ] & 255.255.255.0) . \/24)
使用set对testip变量赋值,上面是个复制的脚本来给testip赋值的,当然你用:set testip 3
这样的简单的来给testip赋值也是可以的
[apple@sgg-gatewy] > :environment print
Global Variables
Local Variables
testip=221.232.119.0/24
我们查看环境,现在有了一个变量,并且已经有了数值了

下面使用put来输出变量的值到屏幕
$testip
用这个方式来表示testip变量的数值
[apple@sgg-gatewy] > :put $testip
221.232.119.0/24
[apple@sgg-gatewy] >







:environment print
显示所有变量及其值



相关文档
最新文档