Skip to content

Instantly share code, notes, and snippets.

@RobAtticus
Forked from jterrace/shaper.sh
Created May 1, 2012 18:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RobAtticus/2570184 to your computer and use it in GitHub Desktop.
Save RobAtticus/2570184 to your computer and use it in GitHub Desktop.
A utility script for traffic shaping with tc
#!/bin/bash
#
# shaper.sh
# ---------
# A utility script for traffic shaping using tc
#
# Usage
# -----
# shape.sh start - starts the shaper
# shape.sh stop - stops the shaper
# shape.sh restart - restarts the shaper
# shape.sh show - shows the rules currently being shaped
#
# tc uses the following units when passed as a parameter.
# kbps: Kilobytes per second
# mbps: Megabytes per second
# kbit: Kilobits per second
# mbit: Megabits per second
# bps: Bytes per second
# Amounts of data can be specified in:
# kb or k: Kilobytes
# mb or m: Megabytes
# mbit: Megabits
# kbit: Kilobits
#
# AUTHORS
# -------
# Aaron Blankstein
# Jeff Terrace
#
# Original script written by: Scott Seong
# Taken from URL: http://www.topwebhosts.org/tools/traffic-control.php
#
# Name of the traffic control command.
TC=/sbin/tc
# Rate to throttle to
RATE=1mbit
# Interface to shape
IF=eth0
# Average to delay packets by
LATENCY=100ms
# Jitter value for packet delay
# Packets will be delayed by $LATENCY +/- $JITTER
JITTER=10ms
start() {
$TC qdisc add dev $IF root handle 1:0 tbf rate $RATE burst 10kb latency 2ms peakrate 10mbit mtu 100000
$TC qdisc add dev $IF parent 1:1 handle 10: netem delay $LATENCY $JITTER
}
stop() {
$TC qdisc del dev $IF root
}
restart() {
stop
sleep 1
start
}
show() {
$TC -s qdisc ls dev $IF
}
case "$1" in
start)
echo -n "Starting bandwidth shaping: "
start
echo "done"
;;
stop)
echo -n "Stopping bandwidth shaping: "
stop
echo "done"
;;
restart)
echo -n "Restarting bandwidth shaping: "
restart
echo "done"
;;
show)
echo "Bandwidth shaping status for $IF:"
show
echo ""
;;
*)
pwd=$(pwd)
echo "Usage: shaper.sh {start|stop|restart|show}"
;;
esac
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment