找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 9800|回复: 2

在桥接模式下的限速脚本

[复制链接]
发表于 2005-3-5 10:27:14 | 显示全部楼层 |阅读模式

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

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

×
CODE
#!/bin/bash# Somewhat simple script that controls bandwidth usage.## This script assumes that the following are installed.  # Userspace tools: iptables, ebtables, tc.# Kernel stuff: Netfilter with layer7 patch, QoS, Ethernet bridging tables# # It also assumes (naturally) that this computer is acting as a bridge, # although it should be very easy modify it to handle other situations.# # By Matthew Strait, 2003.  May be distributed under the GPL version 2,# http://www.gnu.org/licenses/gpl.txt# tc needs to be told about the physical devices, even if you're a bridgephysdevs="eth0 eth1"# syntax: "<match type> = <match arg> , <tc speed>".# Match types are "layer7" and "port".# "port" matches source or destination for tcp or udp.# "kbps" means "KBytes/second".  This is tc's fault.actions=( "port = 80, 40kbps" "layer7 = ftp, 20kbps" )# Hopefully, nothing below this line needs to be edited########################################################################## count commas...lastaction=`echo ${actions[*]} | tr \, '\n' | wc -l`# extra comma and array starts at zero...let lastaction-=2stop-tc() {        if ! [ $1 ]; then         echo "specify a device!"         cleanup 1        fi        for dev in $@; do         if tc qdisc del dev $dev root & /dev/null; then                 echo "tc has now stopped for $dev"         else                 echo "stopping tc for $dev failed (was probably already stopped)"         fi        done}cleanup(){        # Flush the whole mangle table.        iptables -t mangle -F        if ! [ $? = "0" ]; then echo flushing iptables failed at line $LINENO; fi        ebtables -F        if ! [ $? = "0" ]; then echo flushing ebtables failed at line $LINENO; fi        # stop traffic control completely.        stop-tc $physdevs        # if called with non-zero value, exit with that value        if [[ $1 != 0 ]]; then exit $1; fi}# 0 = don't exitcleanup 0# If these aren't loaded manually, shaping of the child connections will # not work.for m in ip_conntrack_ftp ip_conntrack_irc ip_conntrack_tftp ip_conntrack_amanda; do        if ! lsmod | grep $m > /dev/null; then  if ! modprobe $m; then          echo failed to load module $m  fi        fidone# set up basic traffic control magicfor dev in $physdevs; do        tc qdisc add dev $dev root handle 1: htb default 10        if ! [ $? = "0" ]; then echo tc failed at line $LINENO; cleanup 1; fidone# The mark number and also the queue number.  Must start at 2 so it doesn't collide.n=2# index into the speeds arrayindex=0for m in `seq 0 $lastaction`; do   match=`echo ${actions[$m]} | cut -d\, -f1`  speed=`echo ${actions[$m]} | cut -d\, -f2`  type=`echo $match | cut -d\= -f1`  arg=`echo $match | cut -d\= -f2`  echo Packets matching \"$match\" will be shaped to $speed.  if [ $type = "layer7" ]; then        iptables -t mangle -A POSTROUTING -m layer7 --l7proto $arg -j MARK --set-mark $n  elif [ $type = "port" ]; then        ebtables -A FORWARD -p IPv4 --ip-proto tcp --ip-source-port $arg -j mark --set-mark $n        ebtables -A FORWARD -p IPv4 --ip-proto tcp --ip-destination-port $arg -j mark --set-mark $n        ebtables -A FORWARD -p IPv4 --ip-proto udp --ip-source-port $arg -j mark --set-mark $n        ebtables -A FORWARD -p IPv4 --ip-proto udp --ip-destination-port $arg -j mark --set-mark $n  else        echo "failed to parse \"$match\""        cleanup 1  fi  if ! [ $? = "0" ]; then echo \[ip\|eb\]tables failed at line $LINENO; cleanup 1; fi  for dev in $physdevs; do    # per class traffic control black magic   tc class  add dev $dev parent 1:  classid 1:$n htb rate $speed burst 1k   if ! [ $? = "0" ]; then echo tc failed at line $LINENO; cleanup 1; fi   tc qdisc  add dev $dev parent 1:$n handle  $n: sfq perturb 10   if ! [ $? = "0" ]; then echo tc failed at line $LINENO; cleanup 1; fi   tc filter add dev $dev protocol ip parent 1: prio 1 handle $n fw flowid 1:$n   if ! [ $? = "0" ]; then echo tc failed at line $LINENO; cleanup 1; fi  done  let n++  let index++done
routeros
发表于 2005-8-5 12:38:23 | 显示全部楼层
能不能详细点啊。。楼主。
routeros
回复

使用道具 举报

头像被屏蔽
发表于 2005-9-26 14:34:58 | 显示全部楼层
提示: 作者被禁止或删除 内容自动屏蔽
routeros
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-12 07:42 , Processed in 0.063006 second(s), 5 queries , Gzip On, Redis On.

Powered by Discuz! X3.5 Licensed

© 2001-2023 Discuz! Team.

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