Skip to content

Instantly share code, notes, and snippets.

@digitaltonic
Forked from jgdavey/hinder.sh
Last active September 21, 2016 12:07
Show Gist options
  • Save digitaltonic/6132421 to your computer and use it in GitHub Desktop.
Save digitaltonic/6132421 to your computer and use it in GitHub Desktop.
#!/bin/bash
set -e
function printUsage() {
echo -e USAGE="\nUsage: $0 ip_address delay_in_ms percentage_of_packet_loss \nor\nUsage: $0 reset\n"
}
if [ "$#" == "0" ] || [ "$#" == "1" ] && [ "$1" != "reset" ] || [ "$1" != "reset" ] && [ "$#" -ne 3 ]; then
printUsage
exit 1
fi
IP=$1
DELAY=$2
PACKET_LOSS=$(echo "scale=1;10/100" | bc -l)
if [ "$IP" = "reset" ]; then
printf 'Resetting... '
sudo ipfw list | grep 'pipe' | awk '{print $1}' | while read name; do sudo ipfw delete $name ; done
echo 'Done'
else
echo "Creating rules to simulate latency to ${IP}... "
# Create 2 pipes and assigned traffic to and from our webserver to each:
sudo ipfw add pipe 1 ip from any to $IP
sudo ipfw add pipe 2 ip from $IP to any
# Configure the pipes we just created:
sudo ipfw pipe 1 config delay ${DELAY}ms bw 1Mbit/s plr $PACKET_LOSS
sudo ipfw pipe 2 config delay ${DELAY}ms bw 1Mbit/s plr $PACKET_LOSS
echo "Done"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment