每个IP单独限制,好随时修改。如果是用拨号上网的,请把以下内容加到/etc/ppp/ip-up.local中,否则断线重拨后会没有上传限制,对BT光限制下载是不够的。
#!/bin/bash
#
# zyx@zyx.2288.org
#
# 定义上下带宽
# 注意是 Kbit
DOWNLOAD=800Kbit
UPLOAD=160Kbit
# 定义内网IP段
INET=192.168.0.
# 定义限制的IP范围
IPS=1
IPE=253
# 定义本服务器IP
ServerIP=254
# 定义进出设备
IDEV=eth0
ODEV=ppp0
#
#
#
/sbin/tc qdisc del dev $IDEV root handle 10:
/sbin/tc qdisc del dev $ODEV root handle 20:
#
/sbin/tc qdisc add dev $IDEV root handle 10: cbq bandwidth 100Mbit avpkt 1000
/sbin/tc qdisc add dev $ODEV root handle 20: cbq bandwidth 1Mbit avpkt 1000
#
/sbin/tc class add dev $IDEV parent 10:0 classid 10:1 cbq bandwidth 100Mbit rate 100Mbit allot 1514 weight 1Mbit prio 8 maxburst 20 avpkt 1000
/sbin/tc class add dev $ODEV parent 20:0 classid 20:1 cbq bandwidth 1Mbit rate 1Mbit allot 1514 weight 10Kbit prio 8 maxburst 20 avpkt 1000
#
# 不限制内网从本服务器下载。
# 注意如本服务器上有代理,用户可通过代理绕过带宽限制,
# 可取消以下三句限制从本服务器下载。
/sbin/tc class add dev $IDEV parent 10:1 classid 10:10 cbq bandwidth 100Mbit rate 95Mbit allot 1514 weight 20Kbit prio 5 maxburst 20 avpkt 1000 bounded
/sbin/tc qdisc add dev $IDEV parent 10:10 sfq quantum 1514b perturb 15
/sbin/tc filter add dev $IDEV parent 10:0 protocol ip prio 50 u32 match ip src $INET$ServerIP flowid 10:10
#
#限制下载速度
COUNTER=$IPS
while [ $COUNTER -le $IPE ]
do
# 以下三句限制各IP的下载带宽
/sbin/tc class add dev $IDEV parent 10:1 classid 10:1$COUNTER cbq bandwidth 100Mbit rate $DOWNLOAD allot 1514 weight 20Kbit prio 5 maxburst 20 avpkt 1000 bounded
/sbin/tc qdisc add dev $IDEV parent 10:1$COUNTER sfq quantum 1514b perturb 15
/sbin/tc filter add dev $IDEV parent 10:0 protocol ip prio 100 u32 match ip dst $INET$COUNTER flowid 10:1$COUNTER
COUNTER=` expr $COUNTER + 1 `
done
#
#限制上传速度
COUNTER=$IPS
while [ $COUNTER -le $IPE ]
do
# 以下三句限制各IP的上传带宽
/sbin/tc class add dev $ODEV parent 20:1 classid 20:1$COUNTER cbq bandwidth 1Mbit rate $UPLOAD allot 1514 weight 4Kbit prio 5 maxburst 20 avpkt 1000 bounded
/sbin/tc qdisc add dev $ODEV parent 20:1$COUNTER sfq quantum 1514b perturb 15
/sbin/tc filter add dev $ODEV parent 20:0 protocol ip prio 100 handle $COUNTER fw classid 20:1$COUNTER
COUNTER=` expr $COUNTER + 1 `
done
#特殊照顾的IP在以上范围的用户
NIP=78
#192.168.0.78 这家伙天天BT
ND=200Kbit
NU=50Kbit
/sbin/tc class change dev $IDEV parent 10:1 classid 10:1$NIP bandwidth 100Mbit rate $ND allot 1514 weight 20Kbit prio 5 maxburst 20 avpkt 1000 bounded
/sbin/tc class change dev $ODEV parent 20:1 classid 20:1$NIP cbq bandwidth 1Mbit rate $NU allot 1514 weight 4Kbit prio 5 maxburst 20 avpkt 1000 bounded
#
NIP=1
# 192.168.0.1 增加我自已的带宽
ND=1500Kbit
NU=500Kbit
/sbin/tc class change dev $IDEV parent 10:1 classid 10:1$NIP bandwidth 100Mbit rate $ND allot 1514 weight 20Kbit prio 5 maxburst 20 avpkt 1000 bounded
/sbin/tc class change dev $ODEV parent 20:1 classid 20:1$NIP cbq bandwidth 1Mbit rate $NU allot 1514 weight 4Kbit prio 5 maxburst 20 avpkt 1000 bounded
# ...................
#
#
# 修改防火墙,增加上传限制
COUNTER=$IPS
while [ $COUNTER -lt $IPE ]
do
iptables -t mangle -A PREROUTING -i $IDEV -s $INET$COUNTER -j MARK --set-mark $COUNTER
COUNTER=` expr $COUNTER + 1 `
done
# 这里是NAT
iptables -t nat -A POSTROUTING -o $EXTIF -s 192.168.0.0/24 -j MASQUERADE