Demo entry 2460963

bbb

   

Submitted by aaa on Aug 21, 2015 at 18:07
Language: Bash. Code size: 2.0 kB.

#!/bin/bash
if [ "$#" != "5" ]; then
	echo "This script dynamically change the delay parameter of netem"
	echo "Usage: $0 <period> <min> <max> <deviation> <loss>"
	echo " "
	echo "<period> period in minutes (integer) of the triangular wave."
	echo "<min> the minimum value (integer) of the delay in ms."
	echo "<max> the maximum value (integer) of the delay in ms."
	echo "<deviation> delay deviation (integer)"
	echo "<loss> packet loss (integer)"
	exit 1
fi

# trap ctrl-c and call ctrl_c()
trap ctrl_c INT

function ctrl_c() {
        echo "** Trapped CTRL-C."
        echo "** Deleting NetEm rules."
	sudo tc qdisc del dev eth1 root
        echo "** Exiting."
	sleep 1
	exit 0
}

PERIOD=$1
MIN=$2
MAX=$3
DEV=$4
LOSS=$5
# intertime = (period/2 * 60s) / (Max - Min)
SLEEP=$(python -c "print (${PERIOD}*30.0)/(${MAX}-${MIN})")

DATE=$(date +%Y-%m-%d:%H:%M:%S| tr -d "\n")
echo "$DATE - period=${PERIOD}m ; intertime=${SLEEP}s ; min=${MIN}ms ; max=${MAX}ms; deviation=${DEV}ms; loss=${LOSS}%"
count=$((MIN))
DATE=$(date +%Y-%m-%d:%H:%M:%S| tr -d "\n")
echo "$DATE - delay is now = $MIN ms"
#echo "launching: tc qdisc add dev eth1 root netem delay ${MIN}ms ${DEV}ms distribution normal loss ${LOSS}%"
sudo tc qdisc add dev eth1 root netem delay ${MIN}ms ${DEV}ms distribution normal loss ${LOSS}%

while true
do
	while [ $count -lt $MAX ]
	do
		DATE=$(date +%Y-%m-%d:%H:%M:%S| tr -d "\n")
		echo "$DATE - next increase in $SLEEP seconds"
		sleep $SLEEP
		count=$((count+1))
		sudo tc qdisc change dev eth1 root netem delay ${count}ms ${DEV}ms distribution normal loss ${LOSS}%
		echo "$DATE - delay is now = $count ms"

	done

	while [ $count -gt $MIN ]
	do
		DATE=$(date +%Y-%m-%d:%H:%M:%S| tr -d "\n")
		echo "$DATE - next decrease in $SLEEP seconds"
		sleep $SLEEP
		count=$((count-1))
		sudo tc qdisc change dev eth1 root netem delay ${count}ms ${DEV}ms distribution normal loss ${LOSS}%		
		echo "$DATE - delay is now = $count ms"

	done
done

exit 0

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).