Skip to content

Instantly share code, notes, and snippets.

@dannycroft
Created February 5, 2013 12:42
Show Gist options
  • Select an option

  • Save dannycroft/4714229 to your computer and use it in GitHub Desktop.

Select an option

Save dannycroft/4714229 to your computer and use it in GitHub Desktop.
Network throttling for OSX
#!/bin/bash
#
# Throttles your Mac OS X internet connection on one port.
# Handy for testing
set -e
RATE=15KByte/s
PORT=80
PIPE_NUMBER=1
ACTION=
function usage()
{
echo $1
echo
echo "Usage: `basename "$0"` <action> [options]"
echo " Action:"
echo " on"
echo " off"
echo
echo " Options:"
echo " --rate <rate>"
echo " Example: --rate 100KByte/s"
echo " --port <port> (default is 80 if you don't specify --port)"
echo " Example: --port 80"
exit 1
}
function turn_throttling_off()
{
echo "Turning off network throttling"
sudo ipfw delete $PIPE_NUMBER || echo "Is it already turned off?"
}
function turn_throttling_on()
{
echo "Throttling traffic to port $PORT: $RATE"
sudo ipfw pipe $PIPE_NUMBER config bw $RATE >/dev/null
sudo ipfw add $PIPE_NUMBER pipe $PIPE_NUMBER src-port $PORT >/dev/null
sudo ipfw add $PIPE_NUMBER pipe $PIPE_NUMBER dst-port $PORT >/dev/null
}
# Grab command line args:
while [ -n "$1" ]; do
case $1 in
--rate)
shift
RATE=$1
;;
--port)
shift
PORT=$1
;;
*)
ACTION=$1
esac
shift
done
[ -n "$ACTION" ] || usage "Error: no action specified"
case $ACTION in
on)
turn_throttling_off >/dev/null 2>&1 # in case it's already on, clear out the old one
turn_throttling_on
;;
off)
turn_throttling_off
;;
*)
usage "Error: Bad action specified"
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment