找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 212367|回复: 1456

[脚本] ros 脚本详细解释

  [复制链接]
发表于 2008-1-25 21:08:30 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?注册

×
因为下面这个问题,我觉得脚本详细解释也许对大家有用处,决定把以前网络时候找的资料发上来,希望对大家有用处

[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.255.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 www.sina.com.cn]]
00:00:00.006
执行解析www.sina.com.cn这个域名所需的时间


数据类型转换

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={}里有命令

: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 www.sina.com.cn]
61.172.201.240


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

: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] >

                                                很多是网络上面的资料,我加了点解释
                                                                        情灭缘尽
                                                                          2008 01 26 武汉

[ 本帖最后由 情灭缘尽 于 2008-1-26 18:32 编辑 ]
routeros
 楼主| 发表于 2008-1-25 21:10:15 | 显示全部楼层
补充
\/24
前面的不是大写的V字母哦
是\然后/
routeros
回复

使用道具 举报

发表于 2008-1-25 21:57:58 | 显示全部楼层
不明白,你用:put干什么?
put是在终端里用的,脚本里无用
[:put 字符串] 没有返回值,就算有,也是逻辑值,执行成功与否

没细看你的代码,觉得这个PUT用得不一般,也许是高级的功能我不会用
routeros
回复

使用道具 举报

 楼主| 发表于 2008-1-26 02:07:10 | 显示全部楼层
[apple@sgg-gatewy] > :put (hehe . haha)  
hehehaha

除了使用put暂时我还没想到办法能把两个字符串连接起来,不知道各位朋友还有什么别的办法吗
routeros
回复

使用道具 举报

 楼主| 发表于 2008-1-26 03:22:55 | 显示全部楼层
: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]

呵呵,自己搞定了
routeros
回复

使用道具 举报

 楼主| 发表于 2008-1-26 18:11:31 | 显示全部楼层
呵呵,发个详细解释出来,希望大家有用哦
routeros
回复

使用道具 举报

发表于 2008-1-26 20:00:25 | 显示全部楼层
呵呵,发个详细解释出来,希望大家有用哦
routeros
回复

使用道具 举报

发表于 2008-1-26 20:05:46 | 显示全部楼层
5555555555555555555
routeros
回复

使用道具 举报

发表于 2008-1-26 20:17:00 | 显示全部楼层
6666666666666
routeros
回复

使用道具 举报

发表于 2008-1-26 21:21:37 | 显示全部楼层
赶紧学习!!!!!
routeros
回复

使用道具 举报

发表于 2008-1-26 22:01:03 | 显示全部楼层
看看
routeros
回复

使用道具 举报

发表于 2008-1-27 00:07:33 | 显示全部楼层
谢谢老大,这东西很有用
routeros
回复

使用道具 举报

发表于 2008-1-27 00:40:51 | 显示全部楼层
不错,楼主辛苦了
routeros
回复

使用道具 举报

发表于 2008-1-27 01:19:24 | 显示全部楼层
没看过!!
routeros
回复

使用道具 举报

发表于 2008-1-27 03:23:01 | 显示全部楼层
赶紧收藏!!!!
routeros
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|Archiver|手机版|小黑屋|软路由 ( 渝ICP备15001194号-1|渝公网安备 50011602500124号 )

GMT+8, 2024-5-18 05:20 , Processed in 0.073738 second(s), 6 queries , Gzip On, Redis On.

Powered by Discuz! X3.5 Licensed

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表