Skip to content

Instantly share code, notes, and snippets.

@nacho4d
Created November 15, 2011 06:51
Show Gist options
  • Save nacho4d/1366350 to your computer and use it in GitHub Desktop.
Save nacho4d/1366350 to your computer and use it in GitHub Desktop.
[Shell] Use ipfw to limit internet speed
#!/bin/bash
# configuration
#host1 is for metadata, host2 is for data
host1="api.dropbox.com"
host2="api-content.dropbox.com"
# usage
if [ "$*" == "" ]; then
echo "usage: $0 [restore|off|fast|medium|slow]"
exit
fi
# remove any previous firewall rules
sudo ipfw list 10 > /dev/null 2>&1
if [ $? -eq 0 ]; then
sudo ipfw delete 10 > /dev/null 2>&1
fi
sudo ipfw list 11 > /dev/null 2>&1
if [ $? -eq 0 ]; then
sudo ipfw delete 11 > /dev/null 2>&1
fi
sudo ipfw list 12 > /dev/null 2>&1
if [ $? -eq 0 ]; then
sudo ipfw delete 12 > /dev/null 2>&1
fi
sudo ipfw list 13 > /dev/null 2>&1
if [ $? -eq 0 ]; then
sudo ipfw delete 12 > /dev/null 2>&1
fi
# process the command line option
if [ "$1" == "restore" ]; then
echo "$host1 and $host2 are restored"
exit
fi
if [ "$1" == "off" ]; then
# add rules to deny any connections to configured host
sudo ipfw add 10 deny tcp from $host1 to me
sudo ipfw add 11 deny tcp from me to $host1
sudo ipfw add 12 deny tcp from $host2 to me
sudo ipfw add 13 deny tcp from me to $host2
echo "$host1 and $host2 are denied"
else
# create a pipe with limited bandwidth
bw1="1Kbit"
bw2="100Kbit"
if [ "$1" == "fast" ]; then
bw1="3Kbit"
bw2="300Kbit"
elif [ "$1" == "slow" ]; then
bw1="10bit"
bw2="1Kbit"
fi
sudo ipfw pipe 1 config bw $bw1
sudo ipfw pipe 2 config bw $bw2
# add rules to use bandwidth limited pipe
sudo ipfw add 10 pipe 1 tcp from $host1 to me
sudo ipfw add 11 pipe 1 tcp from me to $host1
sudo ipfw add 12 pipe 2 tcp from $host2 to me
sudo ipfw add 13 pipe 2 tcp from me to $host2
echo "$host1 and $host2 are limited to $bw1 and $bw2"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment