找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 44832|回复: 93

[其它] 自动根据流量大小动态分配限BT流量大小

[复制链接]
发表于 2005-4-9 17:51:01 | 显示全部楼层 |阅读模式

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

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

×
:set r9 $r8; :set r8 $r7; :set r7 $r6; :set r6 $r5; :set r5 $r4; :set r4 $r3; :set r3 $r2; :set r2 $r1; :set r1 $r0:set t9 $t8; :set t8 $t7; :set t7 $t6; :set t6 $t5; :set t5 $t4; :set t4 $t3; :set t3 $t2; :set t2 $t1; :set t1 $t0:foreach i in [/interface find name=ether3] do={do:{/interface monitor-traffic $i once do={:set rtemp ($received-bits-per-second>$r0*4/3):set ttemp ($sent-bits-per-second>$t0*4/3):set r0 $received-bits-per-second :set t0 $sent-bits-per-second:if ($rtemp||$ttemp) do={:delay 1000ms }}} while ($rtemp||$ttemp):set rx (($r0+$r1+$r2)/6+($r3+$r4+$r5)/9+($r6+$r7+$r8+$r9)/24):set tx (($t0+$t1+$t2)/6+($t3+$t4+$t5)/9+($t6+$t7+$t8+$t9)/24):if (($r0+$r1+$r2)>($rxm*2+(-15))) do={:foreach d in [/queue tree find name=p2p-download] do={:set dm [/queue tree get $d max-limit]}:foreach u in [/queue tree find name=p2p-upload] do={:set um [/queue tree get $u max-limit]}} else={:set dm ($dm/4);  /queue tree set p2p-download burst-threshold=$dm max-limit=$dm :set um ($um/4);  /queue tree set p2p-upload burst-threshold=$um max-limit=$um}:if ($rxm>73400320) do={:set rxm 73400320}:if ($txm>70254592) do={:set txm 70254592}:if ($rx5000000) do={:set dm ($dm+(-500000)):set rxm ($rxm+200000)}}:if ($tx5000000) do={:set um ($um+(-500000)):set txm ($txm+200000)}}:set dbt ($dm*12/13):set dbm ($dm+3000000);  /queue tree set p2p-download burst-threshold=$dbt max-limit=$dm burst-limit=$dbm:set ubt ($um*12/13):set ubm ($um+3000000);  /queue tree set p2p-upload burst-threshold=$ubt max-limit=$um burst-limit=$ubm:if($r0>$rxm) do={:set rxm $r0}:if($rx$txm) do={:set txm $t0}:if($tx

_______________2_.txt

2.03 KB, 下载次数: 902

routeros
发表于 2005-4-9 18:25:46 | 显示全部楼层
晕。。看不懂啊,有没有详细的说明/
routeros
回复

使用道具 举报

 楼主| 发表于 2005-4-10 08:24:19 | 显示全部楼层
Scripting ExamplesDocument revision: 1 (Wed Dec 22 13:15:36 GMT 2004) Applies to: MikroTik RouterOS V2.8  General InformationSummaryThis section contains various scripting examples.Related DocumentsVRRP DHCP Client and Server HotSpot Gateway Universal Client Interface AAA IP Pools Scripting Host and Complementary Tools General examplesCreating many firewall rulesTo make 100 entries in firewall input rule making it to accept all packets from addresses starting with 1.1.1.1 till 1.1.1.100::for e from 1 to 100 do={  /ip firewall rule input add \    src-address=(1.1.1. . $e)\    src-netmask=255.255.255.255}Getting bandwidth test valuesThis example shows how to get bandwidth-test command's results. As the example uses global variables, other scripts run at the same time may get the current TX values reported by the command./system script add name=bandtest source={  :global i  /tool bandwidth-test 1.1.1.1 \    direction=transmit \    duration=14s \    do={      :if ($status="running") do={        :set i $tx-current        }      }  }Using commentsIt is sometimes more convinient to refer to an entry by its name. But what to do, if name is not available for an entry (for example, in firewall or in routing)? The example shows how to use comments, which are available for any router's entry table.Suppose, we need to switch routing tables depending on the reachability of the 10.0.0.217 host. First, we need to print the policy routing rule table, and choose the entry number you need to change. We will use number 0 in this example:[admin@MikroTik] ip policy-routing rule> set 0 comment=comNow, the actual configuration:[admin@MikroTik] system script> add name=gw_1 source={  /ip policy-routing rule set \    [/ip policy-rout find comment=com] \    table=gw1  }[admin@MikroTik] system script> add name=gw_2 source={  /ip policy-routing rule set \    [/ip policy-rout find comment=com ] \    table=gw2  }[admin@MikroTik] system script> /tool netwatch[admin@MikroTik] tool netwatch> add host=10.0.0.217 interval=10s timeout=998ms \\... up-script=gw_1 down-script=gw_2In this case we're pinging host 10.0.0.217 every 10s and if no response is received, the routing table is changed.Sending files from routerTo send a backup file from a router every 7 days to the example@example.com e-mail address:/system script add name=e-backup source={  /system backup save name=email  /tool e-mail send \    to="example@example.com" \    subject=([/system identity get name] . " Backup") \    file=email.backup  }/system scheduler add interval=7d name="email-backup" on-event=e-backupExecute script until rebootSometimes it is needed to execute script until the router is rebooted. To do this you may use an endless loop. For example:/system script add name=loop source={  :while true do={    :log message="example"    :delay 10s    }  }Here we use 10 second long delays after executing the needed command (in this example, we just write a example example message to the system log).Run the script as usual - with a /system script run loop command. You may stop in from the /system script job submenu (with the remove command).A ping-based netwatchThis is how you can make something like NetWatch by yourself. For example, we will send e-mail if five pings will fail:/system script add name=pinging source={  :if ([/ping 1.1.1.1 count=5] = 0) do={    /tool e-mail send \      to=example@example.com \      subject="Can't ping 1.1.1.1"    }  }Notifying examplesMonitoring DHCP client's addressIn this example, we will monitor IP address on the ether1 interface, and if it is changed, send an e-mail:/system script add name="changed-address" source={  :if ([/system scheduler get check run-count] add name=eth_stat_to_log source={  /interface ethernet monitor ether1 once do={:log \\... message=("\[My log 001\] :: Eth1 status: " . $status)}}Attach this script to a system scheduler event.[admin@MikroTik] system scheduler> add interval=1h name="int_to_log" \\... on-event=eth_stat_to_logConfigure System-Info facility to log all information to disk./system logging facility set System-Info=diskNotes Attention! Enabled local disk logging leads to regular writes to the local file storage. This may result in a faster wearout of solid state medium (CF and IDE-Flash cards).Watching network performanceRun this script to make a system LED be lit proportionally to the data rate received by the ether1 interface (i.e., the LED is lit for 10 * data_rate_in_megabits_per_second miliseconds):/ system script add name="led" source={  :local rx  /interface monitor-traffic ether1 interval=1s do={    :set rx ($received-bits-per-second/1048576)    :if ($rx!=0) do={      :led led2=yes length=((10*$rx) . "ms")      } \    else={      :log message="There is no traffic received on this interface"      }    }  }Notify on data rate recordsRunning this script in scheduler every seconds, you will get reports on data rate highest records. This may be useful for monitoring peak speeds through interfaces./system script add name="record" source={  :global tmp  :global tx  :global rx  :foreach i in [/interface find] do={    /interface monitor-traffic $i once do={      :set tx ($sent-bits-per-second/1048576)      :set rx ($received-bits-per-second/1048576)      :if ([/system scheduler get record run-count]=1) do={        :global ttx        :set ttx $tx        :global trx        :set trx $rx        }      :if ($tx>$ttx) do={        /tool e-mail send \          to=example@example.com \          subject="Script message" \          body=("The transmission traffic on " . \          [/interface get $i name] . " got up to " . $tx . "Mbps")        :set ttx $tx        }      :if ($rx>$trx) do={        /tool e-mail send \          to=example@example.com \          subject="Script message" \          body=("The receiving traffic on " . \          [/interface get $i name] . " got up to " . $rx . "Mbps")        :set trx $rx        }      }    }  }Notify on AP changeThis script if run from scheduler will notify you in case AP's MAC address change for any of your wlan cards switched in station mode./ system script add name="mail_on_AP_change" source={  :global mac  :foreach i in [/interface wireless find mode=station] do={    /interface wireless monitor $i once do={      :set mac $bssid      :if ([/system scheduler get mail_on_AP_change run-count]=1) do={        :global tmp        :set tmp $mac        :if ($mac!=$tmp) do={          /tool e-mail send \            to=example@example.com \            subject=("AP has changed for the " . \            [/interface wireless get $i name] . " interface") \            body=("Client AP's MAC address has changed to " . $mac          }        }      }    }  }Load-balancing and fail-overDescriptionFirst of all, note that IP protocol does not allow much tricks unless you control both sides of a link. Every such a trick is limited by routing - you must make packets be able to run out of your network, as well as to return back correctly, so that both peers can talk to each other. In most cases, the particular path is determined by the IP address you use, so you should take care that the packets sent out to the Internet will be able to return from the same channel. The only exception is the use of dynamic routing protocols, such as BGP or OSPF (which are not covered by this document) - these protocols may provide load-balancing and fail-over by themselves.The fail-over terms may mean two things:in case the default gateway of your router does not respond (i.e., the channel has been broken, or the gateway is down), you will switch all the traffic to another gateway in case your router has crashed, a backup router will take care of your network's traffic (this is also known as high availability, which is provided by VRRP protocol) More complicated term is load balancing. We can differentiate between at least four kinds of traffic-treating that may be called "load-balancing":NAT with per session balancing (means no servers on the router side will get any benefit of balancing unless they use DNS-based load-balancing) - each session is tracked and routed through the same channel until the connection is closed. Each new connection is sent through the next route (you can have more than two). This is supported by MikroTik RouterOS. balancing with routers configured on each side (no NAT used and servers get the benefit of balancing). This is supported by MikroTik RouterOS. packet based balancing (also known as bonding or trunking) - can mess up tcp with out-of-order packets (also needs NAT if only one router doing it). The only real benifit of this if you are controlling both sides. This is not currently supported by MikroTik RouterOS, but is planned in future versions. real bonding of things like synchronous links - needs hardware that supports this on both sides of the link. This is not currently supported by MikroTik RouterOS. Also, be warned that one download session will never see double the speed. Of course if you had some kind of accelerator on your computer that opened multiple sessions, then you would see the benefit.Gateway fail-overThe most simple way to do this is to use netwatch. Here we will ping once in 5 seconds the "primary" gateway (2.2.2.2) of the router, and if does not respond, we will switch to the "backup" gateway (3.3.3.1):/system script add name=down source={/ip route \{... set [/ip route find dst-address=0.0.0.0] gateway 3.3.3.1}/system script add name=up source={/ip route \{... set [/ip route find dst-address=0.0.0.0] gateway 2.2.2.1}/tool netwatch add host=2.2.2.2 interval=5s up-script=up down-script=downLoad-balancing 1Given that we have two Internet providers, each of them have given us an IP address range. We have an internal network and we want to provide load-balancing for this network. To do this, we should set up a default route with both Internet gateways:/ip route add gateway=1.1.1.1,2.2.2.1It is required to make a source NAT (or masquerading) for the outgoing traffic depending on what gateway will it use, or else we will not benefit from having two gateways.Servers will not get any benefit of load-balancing as there is a predetermined routing path to their addresses unless You can make clients use several addresses of the same server, which are reachable by different paths. This could be done with so called DNS-based load-balancing, when You specify more than one IP address for the same DNS name. This topic will not be covered by RouterOS documentation.As it is very desirable to provide fail-over together with load-balancing, you can use the following script to maintain the correct gateway list:/system script add name=fo source={  :local R1  :local R2  :if ([/tool netwatch get R1 status]=up) do={:set R1 1.1.1.1}  :if ([/tool netwatch get R2 status]=up) do={:set R2 2.2.2.1}   /ip route set [/ip route find dst-address=0.0.0.0/0] \  gateway=($R1 . , . $R2)  }/tool netwatch add comment=R1 host=1.1.1.1 interval=5s up-script=fo \  down-script=fo/tool netwatch add comment=R2 host=2.2.2.1 interval=5s up-script=fo \  down-script=foWe can also expand this script to three or even more gateways. For example, if we had a third gateway of 3.3.3.1, the script would look like this:/system script add name=fo source={  :local R1  :local R2  :local R3  :if ([/tool netwatch get R1 status]=up) do={:set R1 1.1.1.1}  :if ([/tool netwatch get R2 status]=up) do={:set R2 2.2.2.1}  :if ([/tool netwatch get R3 status]=up) do={:set R3 3.3.3.1}   /ip route set [/ip route find dst-address=0.0.0.0/0] \  gateway=($R1 . , . $R2 . , . $R3)  }/tool netwatch add comment=R1 host=1.1.1.1 interval=5s up-script=fo \  down-script=fo/tool netwatch add comment=R2 host=2.2.2.1 interval=5s up-script=fo \  down-script=fo/tool netwatch add comment=R3 host=3.3.3.1 interval=5s up-script=fo \  down-script=foLoad-balancing 2To do this, you need two RouterOS boxes connected with at least two lines. Let's call R1 the box that is providing Internet to your local users, and R2 that is connected at the other side of the multiple link, and that is connected to your Internet provider(s).In R1, this is done in the same manner as in the previeous example, just without SRC-NAT (you can make a SRC-NAT on the other one if you want, but this is not required for the method to work). Gateways would be the addresses of R2 put each on a different interface (link).In R2 you will need to make the same multipath routing as in R1, changing the gateways to the respective addresses of the R1 ends of the lines. Also you will need to change the dst-address from the 0.0.0.0 to the respective network you have behind the R1.VRRP ScriptThe following script is working with VRRP protocol. The script is checking '/ip vrrp' submenu, if there could be found some entry with a Backup flag then the script takes the interface names of all entries with a master flag and disables these interfaces under '/interface' list; if the state of all entries is master, then the script takes a name of such an entry and enables it under '/interface' list.:global tmp;:global t;:global iface;:global bool:set bool 0:foreach i in [/ip vrrp find backup=yes] do={  /ip vrrp {    :set bool 1    :foreach e in [find master=yes] do={      :set iface [get $e interface]      /interface disable [/inter find name=$iface]      }    }  }:if ($bool = 0) do={  /ip vrrp {    :foreach e in [find invalid=true] do={      :set iface [get $e interface]      /interface enable [/interf find name=$iface]      }    }  }Miscellaneous scriptsRegistration Table CheckingThe following example script checks the wireless registration table for a certain MAC address and if that address is not available, checks for a second MAC address and in case this one is not available too disables the wireless interface, after that enables the wireless interface./system script add name=search source={  :if ([/interface wireless reg find mac-address=00:01:02:03:04:05] = 0) do={    :if ([/interface wireless reg find mac-address=AA:AB:AA:AA:AA:AA] = 0) do={      /interface wireless disable [find]      /interface wireless enable [find]    }  }}Cleaning up the firewallThis example will remove everythig from the firewall::foreach  i in  [/ip firewall find] do={  /ip firewall rule $i remove [/ip firewall rule $i find]}:foreach  i in  [/ip firewall find] do={        /ip firewall remove $i}Converting ARP entries from dynamic to staticTo convert all the dynamic ARP entries on ether1 interface to static ones::foreach i in [/ip arp find dynamic=yes interface=ether1] do={  /ip arp add copy-from=$i}Night policyWe can define different rules for different time of the day. For example, here are two scripts that will change the queue rule Cust0. Every day at 9AM the queue will be set to 64Kbit/s and at 5PM the queue will be set to 128Kbit/s.[admin@MikroTik] queue simple> add name=Cust0 interface=public \\... target-address=192.168.0.0/24 max-limit=64000/64000[admin@MikroTik] queue simple> /system script[admin@MikroTik] system script> add name=start_limit source={  /queue simple set Cust0 max-limit=64000/64000  }[admin@MikroTik] system script> add name=stop_limit source={  /queue simple set Cust0 max-limit=128000/128000  }[admin@MikroTik] system script> .. scheduler[admin@MikroTik] system scheduler> add interval=24h name="set-64k" \\... start-time=9:00:00 on-event=start_limit[admin@MikroTik] system scheduler> add interval=24h name="set-128k" \\... start-time=17:00:00 on-event=stop_limitAnother similar example is just to disable all queues at night:[admin@MikroTik] system script> add name=start_limit source={  /queue simple enable [/queue simple find]  }[admin@MikroTik] system script> add name=stop_limit source={  /queue simple disable [/queue simple find]  }[admin@MikroTik] system script> .. scheduler[admin@MikroTik] system scheduler> add interval=24h name="start" \\... start-time=9:00:00 on-event=start_limit[admin@MikroTik] system scheduler> add interval=24h name="stop" \\... start-time=17:00:00 on-event=stop_limitAnd here we will reference entries using comment property:/ip firewall rule forward add p2p=all-p2p action drop comment=p2p/system script add name=p2p_disable source={  /ip firewall rule forward {    disable [find comment=p2p]    }  }/system script add name=p2p_enable source={  /ip firewall rule forward {    enable [find comment=p2p]    }  }/system scheduler add interval=24h name="enable_p2p" \  start-time=20:00:00 on-event=p2p_enable/system scheduler add interval=24h name="disable_p2p" \  start-time=7:00:00 on-event=p2p_disableManaging HotSpot usersTo erase a user when his/her total uptime has reached the respective uptime limit:/ system script add name="delete_user" source={  :foreach i in [/ip hotspot user find profile=default] do={    :if ([/ip hotspot user get $i limit-uptime]
routeros
回复

使用道具 举报

发表于 2005-4-10 15:39:46 | 显示全部楼层
更晕!!!
routeros
回复

使用道具 举报

发表于 2005-4-10 16:07:36 | 显示全部楼层
能加点注释吗?
routeros
回复

使用道具 举报

发表于 2005-4-10 16:55:02 | 显示全部楼层
比你还晕!!!!!!!
routeros
回复

使用道具 举报

发表于 2005-4-10 17:05:56 | 显示全部楼层
顶``好东西``
routeros
回复

使用道具 举报

发表于 2005-4-11 01:49:14 | 显示全部楼层
QUOTE (ycfei @ Apr 10 2005, 04:55 PM)
比你还晕!!!!!!!  
  比你还晕!!!!!!!
routeros
回复

使用道具 举报

发表于 2005-4-11 02:43:39 | 显示全部楼层
晕~!晕~!!晕晕晕~!!!
routeros
回复

使用道具 举报

发表于 2005-4-11 17:03:10 | 显示全部楼层
hi,lovellh,these are realy very nice.where do you find it?
routeros
回复

使用道具 举报

发表于 2005-4-11 18:21:46 | 显示全部楼层
好东西,收藏!
routeros
回复

使用道具 举报

 楼主| 发表于 2005-4-11 20:16:57 | 显示全部楼层
脚本是我朋友写的,,,动态限BT:global rx:global tx:global dl:global dm:global ul:global um:foreach i in [/interface find name=ether3] do={/interface monitor-traffic $i once do={:set rx $received-bits-per-second :set tx $sent-bits-per-second:foreach d in [/queue tree find name=p2p-download] do={:set dl [/queue tree get $d max-limit]}:foreach u in [/queue tree find name=p2p-upload] do={:set ul [/queue tree get $u max-limit]}:if ($dl71303168) do={:set dm ($dl+(-1000000)):set dl ($dl+(-2000000));  /queue tree set p2p-download limit-at=$dl max-limit=$dm}}:if ($ul71303168) do={:set um ($ul+(-1000000)):set ul ($ul+(-2000000));  /queue tree set p2p-upload limit-at=$ul max-limit=$um}}}}
routeros
回复

使用道具 举报

发表于 2005-4-11 21:11:46 | 显示全部楼层
不知道里面写的ether3是外网卡呢?还是内网卡??正在看。不错。。。谢谢
routeros
回复

使用道具 举报

发表于 2005-4-19 16:28:03 | 显示全部楼层
晕中,建议先放到精华区,方便,以后查找
routeros
回复

使用道具 举报

发表于 2005-4-19 16:47:05 | 显示全部楼层
强烈要求说明。。
routeros
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-18 13:08 , Processed in 0.128388 second(s), 7 queries , Gzip On, Redis On.

Powered by Discuz! X3.5 Licensed

© 2001-2023 Discuz! Team.

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