ROS的一些常用脚本
/ ip firewall connection {:foreach r in=[find] do={remove $r}}
disable防火墙规则
:foreach i in=[/ip firewall filter find action=drop ] do=[/ip firewall filter disable $i]
减轻syn攻击
firewall connection tracking syn sendtime 设置成50 rectime 设置成30
# RouterOS 2.96
# 这是神仙试写的一个对192.168.0.0这个网段的
# 每个IP加一个simple queue的脚本
# 网关100就不限制了
# 注意:扩展名应改为.rsc
:foreach i in [/queue simple find] \
do {:put (deleting . … . [/queue simple get $i name]);
/queue simple remove $i;}
:for i from 1 to 254 \
do { \
:if ($i!=100) \
do {/queue simple add \
name=(queue . $i) \
limit-at=128000/128000 \
burst-threshold=384000/192000 \
max-limit=512000/256000 \
burst-limit=2000000/512000 \
burst-time=16s/8s \
dst-address=(192.168.0. . $i); \
:put (192.168.0. . $i . … . added)} \
}
定时重起
/system scheduler add name=reboot interval=24h start-time=11:59:00 on-event={/system reboot} disabled=no
改变默认网关
/ip route set [/ip route find dst-address=0.0.0.0/0] gateway=xxx.xxx.xxx.xxx
Simple Queues 限速
:for i from=1 to=75 do={/queue simple add target-address=(192.168.1. . $i . “/32”) max-limit=56000}
删除所有Simple Queues
/queue simple remove [find]
ARP绑定(静态ARP)
:foreach i in=[/ip arp find dynamic=yes ] do={/ip arp add copy-from=$i}
因为下面这个问题
我觉得脚本详细解释也许对大家有用处
决定把以前网络时候找的资料发上来
希望对大家有用处
[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脚本详解
四种变量
local – 定义本地变量,只能在其所要的脚本下调用,不能被其它脚本共享
loop index variables – 定义在for或foreach里的索引号变量
monitor variables – 监视变量
ROS算术操作
! 逻辑非。
/ 相除。
. 连接。两个符串的连接,添加元素到列表
^ 异或(XOR)
~ 取反
* 相剩
& 与(AND)
&& 逻辑与
+ 相加
< 小于
<< 向左位移
<= 小于等于
> 大于
>= 大于等于
>> 向右位移
| 或
|| 逻辑或
ROS说明书里的例子:
计算顺序
false
[admin@MikroTik]> :put (10+1-6*2=11-12=(2+(-3)=-1))
true
逻辑非
false
[admin@MikroTik]> :put (!(2>3))
true
数位取反
0.0.255.255
加法
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
减法
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
乘法
00:00:48
[admin@MikroTik]> :put (-5 * -2)
10
除法
00:00:03.333
[admin@MikroTik]> :put (5 / 2)
2
[admin@MikroTik]> :put (0:0.10 / 3)
00:00:02
比较
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
true
[admin@MikroTik]> :put ((no || no) && (no || yes))
false
数位与AND, 或OR, 异或XOR
0.0.0.134
位移操作
255.255.255.128
连接操作
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脚本保留字
delay find if local put toarray tonum while
do for led log resolve tobool tostr
environment foreach len nothing set toid totime
声音和警报
产生2秒10kHz的音频
length缺省值为100ms
frequency缺省值为1000Hz
execute调用其他命令或者脚本
:execute $a
执行多条命令,例子里执行了两条命令。\n是换行
:global
定义全局变量的使用
显示相关命令。显示当前目录及子目录下有关interface的命令
:pick 数组和字串的截取
[admin@MikroTik]>:put [:pick “I love you” 2 6]
love
:time 计算命令执行所用时长
[admin@MikroTik]> :put [:time [:resole ]]
00:00:00.006
执行解析这个域名所需的时间
数据类型转换
转换值类型
延迟 多少秒
延时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 循环
[admin@MikroTik]>:set i 0;:while($i<5) do={:put $i;:set i ($i+1)}
0
1
2
3
4
5
:for 循环
:for i from=1 to=100 step=30 do={:put $i}
1
31
61
91
:foreach 循环
: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 日志类型 日志内容
写日志操作
类型在/system logging里可以找到
:log info “系统信息”
到LOG里查查运行结果吧
:resolve
域名解析
[admin@MikroTik] > :put [:resolve ]
61.172.201.240
:environment print
显示所有变量及其值
:len
字符串或数组的长度
5
[admin@MikroTik] > :put [:len “1,2,23,65,54,6”]
6
:nothing
空值。nothing不等0,不等于空字符””
:find abc a的结果是0
:find abc d的结果是nothing
:set赋值
将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] >
其它的以后再发上来,,,有什么意见可以:评论
