-
Notifications
You must be signed in to change notification settings - Fork 0
/
latency.sh
executable file
·91 lines (80 loc) · 2.54 KB
/
latency.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!/bin/bash
#Network Delay using Linux Traffic Control command
#Script requires tc command, if not present install iproute package
#Reference: https://netbeez.net/blog/how-to-use-the-linux-traffic-control/
# https://www.cs.unm.edu/~crandall/netsfall13/TCtutorial.pdf
if [[ $EUID -ne 0 ]]; then
echo "This script must be run as root"
exit 1
fi
if ! [ -x "$(command -v tc)" ]; then
echo 'Error: tc command is not present, please install iproute' >&2
exit 1
fi
pri=$(route | grep '^default' | grep -o '[^ ]*$')
echo "$pri" "is primary interface"
echo "Existing settings of $pri"
echo
tc qdisc show dev "$pri"
echo
echo
read -r -p "By Proceeding above existing rules will be removed. Continue? [y/N] " response
case "$response" in
[yY][eE][sS]|[yY])
echo "Proceed"
;;
*)
exit 1
;;
esac
PS3='Please enter your choice: '
options=("Low 100ms Delays" "Medium 500ms Delays" "High 1000ms Delays" "Custom input" "Clear all Rules" "Quit")
select opt in "${options[@]}"
do
case $opt in
"Low 100ms Delays")
echo "you chose choice 100ms"
tc qdisc del dev "$pri" root > /dev/null 2>&1
tc qdisc add dev "$pri" root netem delay 100ms
tc qdisc show dev "$pri"
exit
;;
"Medium 500ms Delays")
echo "you chose choice 500ms"
tc qdisc del dev "$pri" root > /dev/null 2>&1
tc qdisc add dev "$pri" root netem delay 500ms
tc qdisc show dev "$pri"
exit
;;
"High 1000ms Delays")
echo "you chose choice 1000ms"
tc qdisc del dev "$pri" root > /dev/null 2>&1
tc qdisc add dev "$pri" root netem delay 1000ms
tc qdisc show dev "$pri"
exit
;;
"Custom input")
read -r -p "Enter your choice in number only: " choice
re='^[0-9]+$'
if ! [[ $choice =~ $re ]] ; then
echo "error: Not a number" >&2; exit 1
fi
tc qdisc del dev "$pri" root > /dev/null 2>&1
tc qdisc add dev "$pri" root netem delay "$choice""ms"
tc qdisc show dev "$pri"
exit
;;
"Clear all Rules")
echo "Cleared existing rule for delay"
tc qdisc del dev "$pri" root > /dev/null 2>&1
tc qdisc show dev "$pri"
exit
;;
"Quit")
echo "No Changes made"
tc qdisc show dev "$pri"
break
;;
*) echo "invalid option $REPLY";;
esac
done