tc
1 #!/bin/bash 2 3 4 case "$1" in 5 status) 6 tc -s qdisc ls dev lo 7 tc class show dev lo 8 ;; 9 10 on) 11 if [ -z "$2" ] ; then 12 echo "Usage: $0 on [#ms latency] [optional max Mbps]" 13 exit 1 14 fi 15 16 # cribbed from http://serverfault.com/questions/507658/limit-incoming-and-outgoing-bandwidth-and-latency-in-linux 17 18 if [ ! -z "$3" ] ; then 19 sudo tc qdisc add dev lo handle 1: root htb default 11 20 sudo tc class add dev lo parent 1: classid 1:1 htb rate 1000Mbps 21 sudo tc class add dev lo parent 1:1 classid 1:11 htb rate "$3"Mbit 22 sudo tc qdisc add dev lo parent 1:11 handle 10: netem delay "$2"ms limit 100000 23 else 24 sudo tc qdisc add dev lo handle 1: root netem delay "$2"ms limit 100000 25 fi 26 27 ;; 28 29 off) 30 sudo tc qdisc del dev lo root 31 ;; 32 33 *) 34 echo "Use this script to add latency or bandwidth limit to the loopback device" 35 echo 36 echo "Usage:" 37 echo " $0 status" 38 echo " $0 on [#ms latency] [max Mbps]" 39 echo " $0 off" 40 exit 1 41 esac
弱者就是会被欺负呀