在路上 ……

Linux系统运维与架构

#!/bin/sh
  
export PATH=/bin:/usr/bin:/usr/local/bin:/sbin:/usr/sbin:/usr/local/sbin
export LANG=C

#
#Get Internal network adaptor Name and IP
#
DEV=`/sbin/ifconfig | grep -B 1 "inet addr:" |awk '{if(match($3,"Ethernet")) print $1}'`
IPS=`/sbin/ifconfig | grep -B 1 "inet addr:" |awk '{if(match($2,"addr")) {print substr($2,6)}}'`



tc_add ()
{

BURST=`expr ${3} / 10`
if [ $# -eq 3  ];then 
case "$2" in
   IN)
   tc qdisc add dev ${DEV} ingress
   tc filter add dev ${DEV} parent ffff: protocol ip prio 10 u32 match ip dst ${IPS}/32 police rate ${3}kbit burst ${BURST}k drop flowid :1
   ;;
   OUT)
   tc qdisc add dev $DEV root handle 1: htb default 20
   tc class add dev $DEV parent 1: classid 1:1 htb rate ${3}kbit ceil ${3}kbit burst ${BURST}k
   tc filter add dev $DEV parent 1: prio 1 protocol ip u32 match ip src ${IPS}/32 flowid 1:1
   ;;
   *) 
    exit 1
esac
fi

if [ $# -eq 4  ];then 
case "$2" in
   IN)
   tc qdisc add dev ${DEV} ingress
   tc filter add dev ${DEV} parent ffff: protocol ip prio 10 u32 match ip dst ${IPS}/32 match ip dport ${4} 0xffff police rate ${3}kbit burst ${BURST}k drop flowid :1
   ;;
   OUT)
   tc qdisc add dev $DEV root handle 1: htb default 20
   tc class add dev $DEV parent 1: classid 1:1 htb rate ${3}kbit ceil ${3}kbit burst ${BURST}k
   tc filter add dev $DEV parent 1: prio 1 protocol ip u32 match ip src ${IPS}/32 match ip sport ${4} 0xffff flowid 1:1
   ;;
   *) 
    exit 1
esac
fi

}


#show 
tc_show()
{
echo "**********Current QDISC config:**********"
tc qdisc show dev $DEV
echo "**********Current CLASS config:**********"
tc class show dev $DEV
echo "**********Current FILTER config:**********"
tc filter show dev $DEV
} 

#clear 
tc_clear() {
echo "**********Clear QDISC config:**********"
tc qdisc del dev $DEV root
tc qdisc del dev $DEV ingress
} 

#main 
 
case "$1" in 
   add) 
      echo "start add!" 
      if [ $# -le 2  ]; then 
        echo "Usage: $0 add {IN|OUT} {Bandwidth}(kbits) [Port]"
        echo "Usage: $0 add OUT 800 80"
        echo "Usage: $0 add IN 800"
        exit 1 
      else 
       tc_add $@
       exit 0
      fi
       ;; 
   show) 
      echo "show!" 
      if [ ! $# -eq 1  ];then 
        echo "Usage: $0 show" 
        exit 1 
      else 
        tc_show
      fi 
      ;;
   clear) 
      echo "WARNNING!!! Will clear all TC Rules! Are You Sure?(yes/no)"
      read ANS
      if [ $ANS ==  "yes" ]; then
      if [ ! $# -eq 1  ]; then 
        echo "Usage: $0 clear" 
        exit 1 
      else 
        tc_clear
      fi
      fi
      ;;
    *) 
       echo "Usage: $0 {add|show|clear}" 
       exit 1 
esac

转载请注明出处:http://dp2u.com/2014/simple-tc-shell.html

添加新评论 »

在这里输入你的评论...

Typecho 强力驱动