Skip to content

Instantly share code, notes, and snippets.

@fuji246
Created March 19, 2020 18:55
Show Gist options
  • Select an option

  • Save fuji246/7eda05bbcbbc6ae9cb49e85634563602 to your computer and use it in GitHub Desktop.

Select an option

Save fuji246/7eda05bbcbbc6ae9cb49e85634563602 to your computer and use it in GitHub Desktop.
osx network emulator with dummynet
#!/bin/bash
# ./dummynet.sh -b 500 -q 1000 -i 1 -t out -f 10.22.133.53 prepare
# ./dummynet.sh -b 500 -q 1000 -i 2 -t out -f 10.22.133.25 prepare
# ./dummynet.sh start
# ./dummynet.sh stop
#
# To avoid sudo with password, adding the following into /etc/sudoers
#
# yourusername ALL=(ALL) NOPASSWD: /sbin/pfctl
# yourusername ALL=(ALL) NOPASSWD: /usr/sbin/dnctl
#
FILTER_IP="any"
PIPE_IDX="1"
# kbit
LINK_BW="8000"
RAND_LOSS="0.00"
# ms
DELAY=1
# ms
QDELAY=100
DIRECTION="out"
PROTO="udp"
OPTIONS=i:,b:,l:,d:,q:,f:,t:,p:
PARSED=$(getopt $OPTIONS $*)
if [ $? -ne 0 ]; then
exit 2
fi
eval set -- "$PARSED"
while true; do
case "$1" in
-i)
PIPE_IDX=$2
shift 2
;;
-b)
LINK_BW=$2
shift 2
;;
-l)
RAND_LOSS=$2
shift 2
;;
-d)
DELAY=$2
shift 2
;;
-q)
QDELAY=$2
shift 2
;;
-f)
FILTER_IP=$2
shift 2
;;
-t)
DIRECTION=$2
shift 2
;;
-p)
PROTO=$2
shift 2
;;
--)
shift
break
;;
*)
echo "option not found!"
exit 3
;;
esac
done
TB_QSIZE=`expr $LINK_BW \* $QDELAY / 8000` # KB
prepare()
{
sudo dnctl pipe $PIPE_IDX config plr $RAND_LOSS bw "$LINK_BW"Kbit/s delay $DELAY queue "$TB_QSIZE"KB
if [ "$DIRECTION" = "out" ]; then
echo "dummynet out proto $PROTO from any to $FILTER_IP pipe $PIPE_IDX" >> /tmp/dummynet
elif [ "$DIRECTION" = "in" ]; then
echo "dummynet in proto $PROTO from $FILTER_IP to any pipe $PIPE_IDX" >> /tmp/dummynet
else
echo "wrong direction: $DIRECTION"
fi;
}
start()
{
sudo pfctl -f /tmp/dummynet
sudo pfctl -e
sudo dnctl show
}
stop()
{
rm -rf /tmp/dummynet
sudo pfctl -f /etc/pf.conf
sudo pfctl -d
sudo dnctl -q flush
sudo dnctl show
}
case "$1" in
prepare)
prepare
;;
start)
start
;;
stop)
stop
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment