Skip to content

Instantly share code, notes, and snippets.

@addyosmani
Forked from arrel/Throttling
Last active October 23, 2016 10:43
Show Gist options
  • Save addyosmani/6a7abef1e3cb413fb95060cadd8a4d95 to your computer and use it in GitHub Desktop.
Save addyosmani/6a7abef1e3cb413fb95060cadd8a4d95 to your computer and use it in GitHub Desktop.
Throttle bandwidth to individual domains on os x
#!/bin/bash
# if you do not have access to run the script, run "chmod 755 throttling"
# to run enter in terminal "./throttling [speed]"
# full (no throttling)
# fast (3000Kbit)
# medium (100Kbit)
# slow (10Kbit)
# wwdc (1Kbit)
# off (blocks connection)
# configuration
host1="google.com"
host2="localhost:3000"
# usage
if [ "$*" == "" ]; then
echo "usage: $0 [full|fast|medium|slow|wwdc|off]"
exit
fi
# remove any previous firewall rules
sudo ipfw list 100 > /dev/null 2>&1
if [ $? -eq 0 ]; then
sudo ipfw delete 100 > /dev/null 2>&1
fi
sudo ipfw list 110 > /dev/null 2>&1
if [ $? -eq 0 ]; then
sudo ipfw delete 110 > /dev/null 2>&1
fi
sudo ipfw list 200 > /dev/null 2>&1
if [ $? -eq 0 ]; then
sudo ipfw delete 200 > /dev/null 2>&1
fi
sudo ipfw list 210 > /dev/null 2>&1
if [ $? -eq 0 ]; then
sudo ipfw delete 210 > /dev/null 2>&1
fi
# process the command line option
if [ "$1" == "full" ]; then
echo "full speed"
elif [ "$1" == "off" ]; then
# add rules to deny any connections to configured host
if [ -n "$host1" ]; then
sudo ipfw add 100 deny tcp from $host1 to me
sudo ipfw add 110 deny tcp from me to $host1
fi
if [ -n "$host2" ]; then
sudo ipfw add 200 deny tcp from $host2 to me
sudo ipfw add 210 deny tcp from me to $host2
fi
else
# create a pipe with limited bandwidth
bandwidth="100Kbit"
if [ "$1" == "fast" ]; then
bandwidth="3000Kbit"
elif [ "$1" == "slow" ]; then
bandwidth="10Kbit"
elif [ "$1" == "wwdc" ]; then
bandwidth="1Kbit"
fi
sudo ipfw pipe 1 config bw $bandwidth
# add rules to use bandwidth limited pipe
if [ -n "$host1" ]; then
sudo ipfw add 100 pipe 1 tcp from $host1 to me
sudo ipfw add 110 pipe 1 tcp from me to $host1
fi
if [ -n "$host2" ]; then
sudo ipfw add 200 pipe 1 tcp from $host2 to me
sudo ipfw add 210 pipe 1 tcp from me to $host2
fi
fi
sudo ipfw list
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment