LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 1605|回复: 5

iptables的威力---智能QOS

[复制链接]
发表于 2010-1-18 14:07:11 | 显示全部楼层 |阅读模式
自己占沙发啦
!
 楼主| 发表于 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
回复 支持 反对

使用道具 举报

发表于 2010-1-18 14:12:01 | 显示全部楼层
强烈BS标题党、太监贴!

汗,居然早我2秒钟。。。

你发的东西不是很实用哈,需要给内核以及iptables打上IMQ补丁。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2010-1-18 14:23:14 | 显示全部楼层
使用DD-WRT的用户肯定实用。

PC版linux路由器自己只能参考。

脚本中的两个表情符号是冒号+反括号   : )
回复 支持 反对

使用道具 举报

发表于 2010-1-18 14:26:01 | 显示全部楼层
看到这句就知道,设计者的脚本不是很科学:
echo ' * * * * * root . /tmp/test_qos.sh' >> /tmp/crontab
回复 支持 反对

使用道具 举报

 楼主| 发表于 2010-1-18 14:29:57 | 显示全部楼层
汗!!!!!!!!!!!!!!
回复 支持 反对

使用道具 举报

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

本版积分规则

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