Skip to content

Instantly share code, notes, and snippets.

@ChinaXing
Created October 24, 2016 06:08
Show Gist options
  • Save ChinaXing/d2f061e688f59b8ac8b1ed71aa193d23 to your computer and use it in GitHub Desktop.
Save ChinaXing/d2f061e688f59b8ac8b1ed71aa193d23 to your computer and use it in GitHub Desktop.
packet loose mock use tc
#!/bin/env bash
# --------------------------------------------
#
# 网络丢包模拟工具
#
# --------------------------------------------
block(){
interface=$1
dest=$2
[ -z $interface -o -z $dest ] && usage
percentage=${3:-80}
tc qdisc del dev $interface root
tc qdisc add dev $interface root handle 1: prio bands 4
tc qdisc add dev $interface parent 1:4 handle 20: netem delay 10ms 1ms loss ${percentage}%
tc filter add dev $interface parent 1:0 protocol ip pref 55 handle ::55 u32 match ip dst $dest flowid 1:4
}
clear(){
interface=$1
[ -z $interface ] && usage
tc qdisc del dev $interface root
}
usage() {
cat <<EOF
Network disaster mocking tool : packet send loose at some ratio
$0 block|clear <OPTIONS>
block <interface> <destination-ip-address> [percentage]
clear <interface>
examples :
$0 block eth0 192.168.100.2 80 # loose 80% of send packet to 192.168.100.2 via eth0
$0 clear eth0 # cleanup the previouse block rule
EOF
}
# ------------------------------------
case $1 in
block)
shift
block $@
;;
clear)
clear $2
;;
*)
usage
;;
esac
# ------------------------------------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment