Throttle Bandwidth on Mac OS X

14 Oct 2012. comments

Let’s say your home has an internet connection that isn’t all that great and you’re in the middle of a non-resumable download on your computer, and you want to stream a movie from a different media device. You start the stream and it buffers like crazy, stutters, re-buffers, and is therefor generally unwatchable. There isn’t enough bandwidth to both support your download and stream the movie. Ideally you could just tone down the bandwidth on your computer so that the download is restricted to a certain subset of your overall bandwidth.

This is actually easy to do on Mac OS X. Let’s say you want to slow your bandwidth consumption to 30kb/sec:

ipfw pipe 1 config bw 30Kbytes/s
ipfw add 1 pipe 1 tcp from any to me

And after you’re done with your movie you want to unthrottle?

ipfw delete 1

But we’re developers. Lets automate it. Here’s the throttle script:

#!/bin/sh

LIMITDOWN="30Kbytes/s"
LIMITUP="10Kbytes/s"

if [[ $EUID -ne 0 ]]; then
  echo "This script must be run as root." 1>&2
  exit 1
fi

ipfw pipe 1 config bw $LIMITDOWN
ipfw pipe 2 config bw $LIMITUP
ipfw add 1 pipe 1 tcp from any to me
ipfw add 2 pipe 2 tcp from me to any

And the unthrottle:

#!/bin/sh

if [[ $EUID -ne 0 ]]; then
  echo "This script must be run as root." 1>&2
  exit 1
fi

ipfw delete 1
ipfw delete 2

comments

Tagged: bash sh network

2017 Ben Lakey

The words here do not reflect those of my employer.