Created
November 30, 2011 14:18
-
-
Save j-manu/1409218 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
export PATH=/bin:/usr/bin:/sbin:/usr/sbin | |
usage=$( | |
cat <<EOF | |
$0 [OPTIONS] start/stop | |
-s set speed default 256Kbit/s | |
-p set port default 3000 | |
-d set delay default 350ms | |
EOF | |
) | |
PORT="3000" | |
DELAY="350ms" | |
SPEED="256Kbit/s" | |
while getopts ":s:p:d:" opt ; do | |
case $opt in | |
s) | |
SPEED="$OPTARG" | |
;; | |
p) | |
PORT="$OPTARG" | |
;; | |
d) | |
DELAY="$OPTARG" | |
;; | |
:) | |
echo "Option -$OPTARG requires an argument." >&2 | |
exit 1 | |
;; | |
\?) | |
echo "" | |
echo "$usage" | |
exit 0 | |
;; | |
esac | |
done | |
# Makes the argument after all the options $1 | |
shift $(($OPTIND - 1)) | |
if [ "$1" != 'start' ] && [ "$1" != 'stop' ] ; then | |
echo "$usage" | |
exit 0 | |
fi | |
if [ $1 = "start" ]; then | |
echo "- Throttling $PORT to $SPEED with delay $DELAY" | |
sudo ipfw pipe 1 config bw $SPEED delay $DELAY | |
sudo ipfw add 100 pipe 1 src-port $PORT | |
exit 0 | |
fi | |
if [ $1 = "stop" ]; then | |
echo "- Back to normal" | |
sudo ipfw delete 100 | |
sudo ipfw pipe 1 delete | |
exit 0 | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For MAC OS X, this code is now outdated. The "ipfw" command is no longer in os x from yosemite onwards.