|
楼主 |
发表于 2010-1-18 14:11:38
|
显示全部楼层
适用与dd-wrt第3方路由器固件,多机共享流量控制。依据在线IP数动态更新QOS策略(比如只有一个人上网就不限速)。
一般PC做的linux路由器可以参考写法(高手就飘过),但需要对iptables熟悉和有一定shell基础。
用法:
N=2\n #表示你申请了2M的ADSL
export NET=192.168.1.0/24\n
export UIP="192.168.1."\n
export IPS=2\n
export IPE=8\n #表示网段192.168.1.*,开始IP192.168.1.2,结束IP192.168.1.8
UPN=40\n 总上传的带宽是40KB/S,非ADSL用户可以自定义
#Copyright by zhoutao0712
echo -e '\n
N=2\n
UPN=40\n
DOWNN=$((N*90))\n
export NET=192.168.1.0/24\n
export UIP="192.168.1."\n
export IPS=2\n
export IPE=8\n
export UP=$((UPN))kbps\n
export DOWN=$((DOWNN))kbps\n
export DOWN2R=$(($DOWNN*35/100))kbps\n
export DOWN2C=$(($DOWNN*80/100))kbps\n
export UP2R=$(($UPN/3))kbps\n
export UP2C=$(($UPN*80/100))kbps\n
ONLINE=$(ip neighbor show |grep -c \n
export UPLOADR=1kbps\n
export UPLOADC=$(($UPN*30/100))kbps\n
export DOWNLOADR=$(($DOWNN*60/100/$((ONLINE+4))))kbps\n
export DOWNLOADC=$(($DOWNN*80/100))kbps\n' >/tmp/regist_env.sh
. /tmp/regist_env.sh
echo 1 >/tmp/online.sh
insmod imq
insmod ipt_IMQ
ifconfig imq1 up
ifconfig imq0 up
iptables -t mangle -I FORWARD -d $NET -o br0 -j IMQ --todev 0
iptables -t mangle -I PREROUTING -s $NET -i br0 -j IMQ --todev 1
iptables -t mangle -I FORWARD -s $NET -d $NET -o br0 -j ACCEPT
iptables -t mangle -I PREROUTING -s $NET -d $NET -i br0 -j ACCEPT
echo -e '\n
tc qdisc del dev imq0 root\n
tc qdisc del dev imq1 root\n' >/tmp/del.sh
echo -e '\n
tc qdisc add dev imq1 root handle 1: htb\n
tc class add dev imq1 parent 1: classid 1:1 htb rate $UP\n
tc qdisc add dev imq0 root handle 1: htb\n
tc class add dev imq0 parent 1: classid 1:1 htb rate $DOWN\n
tc filter add dev imq0 parent 1:0 protocol ip prio 10 u32 match u16 0x0000 0xfc00 at 2 flowid 1:1000\n
tc class add dev imq0 parent 1:1 classid 1:1000 htb rate $DOWN2R ceil $DOWN2C prio 0\n
tc filter add dev imq1 parent 1:0 protocol ip prio 10 u32 match u16 0x0000 0xff80 at 2 flowid 1:1000\n
tc class add dev imq1 parent 1:1 classid 1:1000 htb rate $UP2R ceil $UP2C prio 0\n
i=$IPS;\n
while [ $i -le $IPE ] \n
do\n
tc class add dev imq1 parent 1:1 classid 1:2$i htb rate $UPLOADR ceil $UPLOADC prio 2\n
tc qdisc add dev imq1 parent 1:2$i handle 2$i: sfq perturb 15\n
tc class add dev imq0 parent 1:1 classid 1:1$i htb rate $DOWNLOADR ceil $DOWNLOADC prio 2\n
tc qdisc add dev imq0 parent 1:1$i handle 1$i: sfq perturb 15\n
tc filter add dev imq1 parent 1:0 protocol ip u32 match ip src 192.168.1.$i flowid 1:2$i
tc filter add dev imq0 parent 1:0 protocol ip u32 match ip dst 192.168.1.$i flowid 1:1$i
i=`expr $i + 1`\n
done\n' >/tmp/limit.sh
echo -e '\n
ip neigh flush dev br0\n
sleep 50s\n
ONLINE_OLD=$(cat /tmp/online.sh)\n
ONLINE_NEW=$(ip neighbor show |grep -c \n
SUB_ONLINE=$(($ONLINE_NEW-$ONLINE_OLD))\n
while test $SUB_ONLINE -eq 0\n
do\n
exit\n
done\n
while test $ONLINE_NEW -eq 1\n
do\n
echo $ONLINE_NEW >/tmp/online.sh\n
. /tmp/del.sh\n
exit\n
done\n
while test $ONLINE_OLD -eq 1\n
do\n
echo $ONLINE_NEW >/tmp/online.sh\n
. /tmp/regist_env.sh\n
. /tmp/del.sh\n
. /tmp/limit.sh\n
exit\n
done\n
case $SUB_ONLINE in\n
0)\n
exit;;\n
-1)\n
exit\n
;;\n
1)\n
exit\n
;;\n
*)\n
echo $ONLINE_NEW >/tmp/online.sh\n
. /tmp/regist_env.sh\n
. /tmp/del.sh\n
. /tmp/limit.sh\n
;;\n
esac\n' >/tmp/test_qos.sh
chmod +x /tmp/test_qos.sh
echo ' * * * * * root . /tmp/test_qos.sh' >> /tmp/crontab |
|