level 1
当网管也有几年的经验了,现在网吧都关了,我也成了无业游民了。没工作做了之后自己买了一个ROS rb750gr3。给家里用,家里的手机电脑等数量还真不少,所以根据学到的知识写了一个动态限速方案,在这里分享给大家。(仅作参考)
2017年08月04日 04点08分
1
level 1
声明:该脚本使用ros 6.36.1环境下。如使用在其它版本,脚本语法可能有些不同。脚本若有错别字请自行更正或修正。我没有检查。设备是rb750gr3.
2017年08月04日 04点08分
2
前排提示,所有代码都可以粘贴到new terminal执行
2017年08月04日 04点08分
level 1
1)首先,需要将内网中,在线的主机IP,都保存到/ip firewall address-lists里面,下面会有用到,timeout就设为10分钟吧。
/ip firewall mangle add chain=forward src-address="192.168.88.0/24" in-interface=Lan action=add-src-to-address-list address-list=onlineAddress address-list-timeout=00:10:00
2017年08月04日 04点08分
3
level 1
2)再增加同一网段的所有IP的标记,in-interface src-address和out-interface dst-address分别设置。所有标记都启用,并且关掉passthrough。
:for item from=1 to=254 do={/ip firewall mangle;add chain=forward src-address="192.168.88.$item" in-interface=Lan action=mark-packet new-packet-mark="192.168.88.$item" passthrough=no comment="192.168.88.$item";add chain=forward dst-address="192.168.88.$item" out-interface=Lan action=mark-packet new-packet-mark="_192.168.88.$item" passthrough=no comment="_192.168.88.$item";}
2017年08月04日 04点08分
4
level 1
3)然后再在queue tree里面,增加同一网段所有IP的queue包含up和down,一共254*2+2个队列需要增加。每一queue的包标记使用对应的包名称。 增加好后除了down和up这两个主队列,其它都先禁用。主队列使用paren=global,max-limit=你的最大带宽。所有子队列的max-limit=最大带宽*0.8。
/queue tree;add name=down parent=global max-limit=100000000;add name=up parent=global max-limit=2000000;:for item from=1 to=254 do={add name="_192.168.88.$item" parent=down max-limit=80000000 packet-mark="_192.168.88.$item" disable=yes;add name="192.168.88.$item" parent=up max-limit=1600000 packet-mark="192.168.88.$item" disable=yes}
2017年08月04日 04点08分
5
level 1
4)最后一部分就是限速部分,需要system scripts和scheduler来完成。计划写两个脚本,其中一个总开关检查功能。先介绍总开关功能,总开关将声明一个全局变量,用来存放上一次执行时的在线主机IP列表。总开关每三十秒运行一次(scheduler定时执行),每次运行都获取一次在线主机IP列表,将这列表与全局变量保存的IP列表对比,判断新旧在线主机IP列表是否有变化,是否有下线的主机IP(若有下线的,直接执行disable=yes关了对应的queue),若IP列表有变化,运行另外一个执行脚本,剩余的事都交给另外一个脚本了。 介绍第二个脚本功能,第二个脚本功能获取当前在线主机IP的数量,用来计算一会要使用到的limit值。建议limit=最大带宽*0.7/当前在线主机数量。
事实上脚本的实际写法要比上文的介绍要复杂一些,多了一些判断什么的,但是全介绍出来的话太累人了,自行看脚本吧。
代码如下:
/system script;add name=onlineAddressChack source=":global onlineAddressBak;:local onlineAddressObj [/ip firewall address-list find list=onlineAddress];:local onlineAddress \"\";:foreach item in=\$onlineAddressObj do={:set onlineAddress (\$onlineAddress . \",\" . [/ip firewall address-list get \$item address]);};:set onlineAddress [:toarray \$onlineAddress];:local Tswitch 0;:if ([:typeof \$onlineAddressBak] = \"array\") do={:if (\$onlineAddress != \$onlineAddressBak) do={:set Tswitch 1;:foreach item in=\$onlineAddressBak do={:local Sswitch 1;:foreach item2 in=\$onlineAddress do={:if (\$item = \$item2) do={:set Sswitch 0;};};:if (\$Sswitch = 1) do={:local tempAddress [:tostr \$item];/queue tree set [find name=\$tempAddress] disable=yes;/queue tree set [find name=\"_\$tempAddress\"] disable=yes;:set Tswitch 1;};};};} else={:set Tswitch 1;};:if (\$Tswitch = 1) do={:set onlineAddressBak \$onlineAddress;/system script run [find name=onlineAddressChange];};";add name=onlineAddressChange source=":global onlineAddressBak;:local numOfOnlineAddress [:len \$onlineAddressBak];:local limitUp (1400000 / \$numOfOnlineAddress);:local limitDown (70000000 / \$numOfOnlineAddress);:foreach item in=\$onlineAddressBak do={/queue tree set [find name=\$item] dis=no limit-at=\$limitUp;:delay 500ms;/queue tree set [find name=\"_\$item\"] dis=no limit-at=\$limitDown;:delay 500ms;}";
2017年08月04日 04点08分
6
朋友可以加个微信吗。我用的你的代码,运行有没有出错。但是最后一步不知道该如何做了。
2019年01月06日 03点01分
13687509502
2019年01月06日 03点01分
level 1
第二个脚本中,每执行一次queue操作,后面都有一个delay 500ms。 若各位在线设备大于30台,建议此延时值改小一点,例如400ms? 最低不要低于100ms,除非你CPU真的挺强的。
2017年08月04日 04点08分
7
我的设备测试得到运行脚本时CPU占用10%左右,若此时宽带满载的话,那CPU就占用很高了
2017年08月04日 04点08分
level 1
此方案也可以修改为 网页优先,游戏优先等。 做法就是改变queue tree结构,和改变
mangle的标记方式。
2017年08月04日 04点08分
8
也入RB路由 楼主这个思路很适合我 请问怎么修改优先游戏网页呢?游戏我都标记了 思路还没通。。。
2019年10月28日 16点10分
没工作之后已经没弄这些了,感觉简单队列就够我目前使用了。对我以前这些限速都不怎么满意。
2019年10月29日 05点10分
level 1
顶楼主,同型号路由器,网上弄了一个L7脚本,看一下有没有什么不妥的地方
2017年08月05日 02点08分
9
我对l7了解甚少,可能帮不了你呢。
2017年08月10日 15点08分
无论是l7还是普通的标记,都是标记而已。限速是由queue控制的
2017年08月10日 15点08分
@ytftdkjxkj queue我也相对了解,L7相当于基于上层软件服务类型的一个
2017年08月11日 00点08分
level 1
##作者测试过几个企业,1个网吧(游戏下行都走小包了),反应不错,HTB没有限速,要限速自己调整,调整后效果更理想.
/ip firewall layer7-protocol
add name=Tencent_qq regexp="^.\?.\?[\\x02|\\x05]\\x22\\x27.+|^.\?.\?[\\x02|\\x\
05]\\x22\\x27.+[\\x03|\\x09]\$|^.\?.\?\\x02.+\\x03\$|^/xFE/x42../x42/x02/x\
0B/x7D/x98/x38/xE4.+"
add name=Tencent_qqgame regexp="^.\?.\?\\x2D.+[\\x25\\x62\\x0E\\xC1\\x5F\\x6C|\
\\xFF\\xFF\\x20\\xCF\\x42\\x53|\\xFF\\xFF\\x10\\x17\\x87\\xA3|\\x3E\\x7F\\\
x20\\xCF\\x42\\x53|\\x1F\\x43\\x10\\x17\\x87\\xA3]|^\\x05\\x22.+\\x03\$"
add name=PPStream regexp="^.\?.\?\\c.+\\c"
add name=QQMusic regexp=\
"(^\\xFE.\?.\?.\?.\?\\xCF|^get.+\\qqmusic.\?\\qq.+\\qqmusic)"
add name=QQLive regexp="(^get.+\\video.\?\\qq.+\\flv|^\\xFE.\?.\?.\?.\?\\xD3|^\
get.+\\video.\?\\qq.+\\mp4)"
add name=Kugou regexp=\
"(^post.+\\x0D\\x0A\\x0D\\x0A|^http.+\\x0D\\x0A\\x0D\\x0A|^e)"
add name=Http regexp="http/(0\\.9|1\\.0|1\\.1) [1-5][0-9][0-9] [\t-\r -~]*(con\
nection:|content-type:|content-length:|date:)|post [\t-\r -~]* http/[01]\\\
.[019]"
add name=Http-img regexp="\\.jpg|\\.png|\\.gif|\\.bmp|\\.jpeg"
add name=Http-web regexp=\
"\\.jsp|\\.shtml|\\.html|\\.htm|\\.php|\\.asp|\\.aspx|\\.cgi"
add name=NetTV regexp=\
"^.*get.+(\\.flv|\\.f4v|\\.hlv|\\.rm|\\.swf|\\.wma|\\.mp4|\\.mp3).*\$"
add name=File regexp="^.*get.+(\\.iso|\\.exe|\\.zip|\\.rar|\\.7z|\\.gho|\\.pdf\
|\\.avi|\\.mkv|\\.wmv|\\.wav|\\.flac|\\.ape|\\.msi).*\$"
add name=QQsp regexp="(^\\x03.\?\\xE1\\x8D|^\\x02\\x02|^\\x04\\x1E)"
add name=DNS regexp="^.\?.\?.\?.\?[\\x01\\x02].\?.\?.\?.\?.\?.\?[\\x01-\?][a-z\
0-9][\\x01-\?a-z]*[\\x02-\\x06][a-z][a-z][fglmoprstuvz]\?[aeop]\?(um)\?[\\\
x01-\\x10\\x1c][\\x01\\x03\\x04\\xFF]"
add name=Http-jpg regexp="^.*(post|POST|get|GET).+\\.jpg.+\\http"
/ip firewall address-list
add address=192.168.88.10/254 list=OnLineClient
/ip firewall filter
add action=drop chain=input dst-port=22 protocol=tcp
/ip firewall mangle
add action=change-ttl chain=forward new-ttl=set:128
add action=change-mss chain=forward new-mss=1440 protocol=tcp tcp-flags=syn \
tcp-mss=1441-65535
add action=mark-connection chain=forward comment="DNS\BA\CDICMP" \
layer7-protocol=DNS new-connection-mark=dns&icmp
add action=mark-connection chain=forward new-connection-mark=dns&icmp \
protocol=icmp
add action=mark-packet chain=forward connection-mark=dns&icmp \
new-packet-mark=DNS&ICMP_up passthrough=no src-address-list=OnLineClient
add action=mark-packet chain=forward connection-mark=dns&icmp \
dst-address-list=OnLineClient new-packet-mark=DNS&ICMP_down passthrough=\
no
add action=mark-connection chain=forward comment=http layer7-protocol=Http \
new-connection-mark=http
add action=mark-connection chain=forward layer7-protocol=Http-web \
new-connection-mark=http
add action=mark-connection chain=forward layer7-protocol=Http-jpg \
new-connection-mark=http
add action=mark-connection chain=forward layer7-protocol=Http-img \
new-connection-mark=http
add action=mark-packet chain=forward connection-mark=http dst-address-list=\
OnLineClient new-packet-mark=http_down passthrough=no
add action=mark-packet chain=forward connection-mark=http new-packet-mark=\
http_up passthrough=no src-address-list=OnLineClient
add action=mark-connection chain=forward comment="\B4\F3\D0\A1\B0\FC 0-511" \
new-connection-mark=small511_conn packet-size=0-511 protocol=tcp \
src-port=!80
add action=mark-connection chain=forward new-connection-mark=small511_conn \
packet-size=0-511 protocol=udp
add action=mark-packet chain=forward connection-mark=small511_conn \
new-packet-mark=small511_u packet-size=128-511 passthrough=no \
src-address-list=OnLineClient
add action=mark-packet chain=forward connection-mark=small511_conn \
new-packet-mark=small511_d packet-size=128-511 passthrough=no
add action=mark-connection chain=forward comment=vido layer7-protocol=NetTV \
new-connection-mark=vido
add action=mark-connection chain=forward layer7-protocol=PPStream \
new-connection-mark=vido
add action=mark-connection chain=forward layer7-protocol=QQLive \
new-connection-mark=vido
add action=mark-packet chain=forward connection-mark=vido dst-address-list=\
OnLineClient new-packet-mark=vido_down passthrough=no
add action=mark-packet chain=forward connection-mark=vido new-packet-mark=\
vidio_up passthrough=no src-address-list=OnLineClient
add action=mark-connection chain=forward comment=file layer7-protocol=File \
new-connection-mark=file
add action=mark-connection chain=forward layer7-protocol=QQMusic \
new-connection-mark=file
add action=mark-connection chain=forward layer7-protocol=Kugou \
new-connection-mark=file
add action=mark-packet chain=forward connection-mark=file dst-address-list=\
OnLineClient new-packet-mark=file_down passthrough=no
add action=mark-packet chain=forward new-packet-mark=file_up passthrough=no \
src-address-list=OnLineClient
add action=mark-connection chain=forward comment=\
"\B4\F3\D0\A1\B0\FC 1301-1500" new-connection-mark=big1500_conn \
packet-size=1301-1500
add action=mark-packet chain=forward connection-mark=big1500_conn \
new-packet-mark=big1500_u packet-size=1301-1500 passthrough=no \
src-address-list=OnLineClient
add action=mark-packet chain=forward connection-mark=big1500_conn \
dst-address-list=OnLineClient new-packet-mark=big1500_d packet-size=\
1301-1500 passthrough=no
add action=mark-connection chain=forward comment=other new-connection-mark=\
other
add action=mark-packet chain=forward connection-mark=other dst-address-list=\
OnLineClient new-packet-mark=other_down passthrough=no
add action=mark-packet chain=forward comment="\BD\E1\CA\F8" connection-mark=\
other new-packet-mark=other_up passthrough=no src-address-list=\
OnLineClient
# jan/27/2015 01:53:30 by RouterOS 6.22.1
# software id = TAYM-FTBN
#
/queue type
add kind=pcq name=0m_down pcq-classifier=dst-address pcq-total-limit=10000
add kind=pcq name=0m_up pcq-classifier=src-address pcq-total-limit=10000
/queue tree
add max-limit=60M name=down parent=global queue=default
add name=icmp@dns_down packet-mark=DNS&ICMP_down parent=down priority=1 \
queue=0m_down
add name=http_down packet-mark=http_down parent=down priority=3 queue=0m_down
add name=vido packet-mark=vido_down parent=down priority=5 queue=0m_down
add name=file packet-mark=file_down parent=down priority=7 queue=0m_down
add name=other packet-mark=other_down parent=down priority=4 queue=0m_down
add max-limit=4M name=up parent=global queue=default
add name=icmp&dns_up packet-mark=DNS&ICMP_up parent=up priority=1 queue=0m_up
add name=http_up packet-mark=http_up parent=up priority=3 queue=0m_up
add name=vido_up packet-mark=vidio_up parent=up priority=5 queue=0m_up
add name=file_up packet-mark=file_up parent=up priority=7 queue=0m_up
add name=other_up packet-mark=other_up parent=up priority=4 queue=0m_up
add name=big_packet packet-mark=big1500_d parent=down priority=6 queue=\
0m_down
add name=small_packet packet-mark=small511_d parent=down priority=2 queue=\
0m_down
add name=small_packet_up packet-mark=small511_u parent=up priority=2 queue=\
0m_up
/queue interface
set pppoe-out1 queue=default
2017年08月05日 02点08分
10