TC Main script:
#!/bin/bash
#
. /root/tc_functions
num=2
Dev=eth0
INIT_TC $NUM
While:; Do
#IP = ' Netstat-antup|grep established |grep 80|cut-d ': '-f8 '
ip= ' Netstat-antup|grep established|grep 80|awk ' {print $ {} ' |grep-o ' \ ([0-9]\{1,3\}\.\] \{1,3\}[0-9]\{1,3\} "|sort-rn |uniq-c|awk ' {print $} '
Free=0
For I in $IP
Do
ip16= ' Ip_to_16 $i '
If tc-s filter ls dev $DEV | grep $IP &>/dev/null;
Continue
Fi
For x in ' seq $NUM '
Do
Change $x
If [$free-eq 1];then
TC Filter Add dev $DEV parent 1:protocol IP prio u32 match IP DST $i flowid 1: $x;
free=0;
Break 1;
Fi
Done
Done
TC Filter Add dev $DEV parent 1:protocol IP prio u32 match IP DST 0.0.0.0/0 flowid 1:$[$NUM +1];
Sleep 30
TC Filter del dev eth0 parent 1:protocol IP prio u32;
Done
Tc_functions:
function HD () {
Case $ in
10)
echo A;;
11)
echo b;;
12)
echo c;;
13)
Echo D;;
14)
echo e;;
15)
echo F;;
*)
echo $;;
Esac
}
function Ip_to_16 () {
A= ' echo $1|cut-d '. '-f1 '
B= ' echo $1|cut-d '. '-f2 '
C= ' echo $1|cut-d '. '-f3 '
D= ' echo $1|cut-d '. '-f4 '
a1=$[$a/16]
a2=$[$a%16]
b1=$[$b/16]
b2=$[$b%16]
c1=$[$c/16]
c2=$[$c%16]
d1=$[$d/16]
d2=$[$d%16]
Q1= ' HD $a 1 '
Q2= ' HD $a 2 '
Q3= ' HD $b 1 '
Q4= ' HD $b 2 '
Q5= ' HD $c 1 '
Q6= ' HD $c 2 '
Q7= ' HD $d 1 '
Q8= ' HD $d 2 '
Echo $q 1$q2$q3$q4$q5$q6$q7$q8
}
function Change () {
past= ' tc-s class ls dev $DEV |grep Sent |cut-d '-f3|head-$1|tail-1 '
Sleep 1
now= ' tc-s class ls dev $DEV |grep Sent |cut-d '-f3|head-$1|tail-1 '
[$NOW-eq $PAST]&& free=1
}
function Init_tc () {
TC Qdisc del Dev eth0 root
TC Qdisc Add dev eth0 root handle 1:htb r2q 1
Local I
For i in ' seq $ '
Do
TC class Add dev eth0 parent 1:classid 1: $i HTB rate 1mbit ceil 1mbit
Done
TC class Add dev eth0 parent 1:classid 1:$[$i +1] HTB rate 2mbit ceil 2mbit
}
This article is from the "single Season rice" blog, please be sure to keep this source http://linzb.blog.51cto.com/5192423/1766222
TC Flow control script, dynamically guaranteed 1M bandwidth per IP