-
Notifications
You must be signed in to change notification settings - Fork 2
/
random_netsim.sh
executable file
·152 lines (123 loc) · 3.98 KB
/
random_netsim.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#!/bin/bash
# Simulate network delay
printf "Welcome to the random network simulator:\n\n"
kill_all (){
# Clearing the netem configuration
for i in "${net_array[@]}"
do
sudo tc qdisc del dev "$i" root &> /dev/null
done
}
network_array (){
# Creating an array containing all network interfaces which are up
net_array=()
for iface in $(ifconfig | cut -d ' ' -f1| tr ':' '\n' | awk NF)
do
net_array+=("$iface")
done
unset "net_array[${#net_array[@]}-1]"
}
user_input (){
# Printing available interfaces
printf "Here is a list of your network interfaces:\n\n"
for iface in $(ifconfig | cut -d ' ' -f1| tr ':' '\n' | awk NF)
do
addr=$(ip -o -4 addr list "$iface" | awk '{print $4}' | cut -d/ -f1)
if [[ $iface != "lo" ]]
then
printf "$iface""%s\t""$addr""%s\n"
fi
done
# Network Interface selection
printf "\nPlease select the network interface you want to use:\n"
read -r user_iface
while ! [[ "${net_array[*]}" =~ (^| )"$user_iface"( |$) ]]; do # check if the user input is valid
echo "Please enter a valid network interface:"
read -r user_iface
done
# Maximum interval duation
printf "\nPlease define the maximum time intervals you want to use in seconds:\n"
read -r time_int
while ! [[ "$time_int" =~ ^[0-9]+$ && "$time_int" -gt 0 ]]; do
echo "Please enter a valid numerical maximum time interval in [s]:"
read -r time_int
done
# Time Delay Setup
echo "Please define the maximum time delay in [ms]:"
read -r max_delay
while ! [[ "$max_delay" =~ ^[0-9]+$ && "$max_delay" -ge 0 ]]; do
echo "Please enter a valid numerical maximum time delay value in [ms]:"
read -r max_delay
done
# Jitter Setup
echo "Please define the maximum jitter in [ms]:"
read -r max_jitter
while ! [[ "$max_jitter" =~ ^[0-9]+$ && "$max_jitter" -ge '0' ]]; do
echo "Please enter only a valid integer value for the maximum jitter greater or equal to 0ms:"
read -r max_jitter
done
# Packet Loss Setup
echo "Please define the maximum packet loss in [%]:"
read -r max_ploss
while ! [[ "$max_ploss" =~ ^[0-9]+$ && "$max_ploss" -lt '100' && "$max_ploss" -ge '0' ]]; do
echo "Please enter a valid packet loss value in [%], between 0% and 100%:"
read -r max_ploss
done
}
confirmation (){
read -p "Please confirm that these values are correct [Y/n]:"$'\n' -r -n 1 choice
case "$choice" in
y|Y|"" ) ;;
n|N ) printf "\nHere we go again:\n"
user_input
print_settings
confirmation;;
* ) printf "\nYou have entered invalid input!\n"
confirmation;;
esac
}
print_settings (){
printf "\nThese are the set parameters:\n\n"
printf "Selected Network interface:\t%s\n" "${user_iface}"
printf "Maximum Time Interval:\t\t%ss\n" "${time_int}"
printf "Maximum Delay:\t\t\t%sms\n" "${max_delay}"
printf "Maximum Jitter:\t\t\t%sms\n" "${max_jitter}"
printf "Maximum Packet Loss:\t\t${max_ploss}%%\n\n"
}
randgen () {
if [[ $1 == 0 ]];then
echo 0
else
echo "$((RANDOM % (("$1") + 1)))"
fi
}
netemu (){
rand_int="$(randgen "$time_int")"
rand_delay="$(randgen "$max_delay")"
rand_jitter="$(randgen "$max_jitter")"
rand_ploss="$(randgen "$max_ploss")"
if [[ "$rand_ploss" == 0 ]]; then
sudo tc qdisc add dev "$user_iface" root netem delay "$rand_delay"ms "$rand_jitter"ms
printf "Network interface: %s will be delayed with %sms and the jitter will be %sms for %s seconds.\n" "$user_iface" "$rand_delay" "$rand_jitter" "$rand_int"
else
sudo tc qdisc add dev "$user_iface" root netem delay "$rand_delay"ms "$rand_jitter"ms loss "$rand_ploss"%
printf "Network interface: %s will be affected by %d%% packet loss, the RTT will be %dms and the jitter %dms for %s seconds.\n" "$user_iface" "$rand_ploss" "$rand_delay" "$rand_jitter" "$rand_int"
fi
}
rand_netsim() {
while :
do
kill_all
netemu
sleep "$rand_int"
kill_all
sleep "$rand_int"
done
}
network_array
kill_all
user_input
print_settings
confirmation
print_settings
rand_netsim