-
Notifications
You must be signed in to change notification settings - Fork 0
/
slow
executable file
·364 lines (345 loc) · 8.67 KB
/
slow
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
#!/bin/bash
#
# slow
#
# Simulates a low bandwidth, high-latency network connection
#
# Requires a Linux operating system with the 'tc' traffic control tool
#
# Author: Richard Bullington-McGuire <[email protected]>
# Author: Mike Schwartz <[email protected]>
#
# Copyright 2012 Modus Create, Inc.
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject
# to the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
# Version Change
# 0.1 first version
# 0.2 @aloysius implements packet loss
# 0.3 @sunapi386 implements the active device selected by default
# 0.4 @sunapi386 implements packet duplication, corruption, reordering
# 0.5 @sunapi386 implements reset after timeout; to recover device without a reboot
VERSION=0.5
current_used_device() {
gateway=$(traceroute -m 1 google.com | tail -n 1 | cut -d' ' -f4)
device=$(/sbin/route | grep $gateway | awk '{ print $NF }')
echo $device
}
device=$(current_used_device)
bandwidth=100kbps
latency=350ms
packetloss=0%
duplication=0%
corruption=0%
reordering=0%
self_reset_delay_s=0
command=slow
retcode=0
PATH=/sbin:/bin:/usr/sbin:/usr/bin
echorun() {
echo -E '#' "$@"
"$@"
}
if ! which tc > /dev/null; then
echo "Aborting: No 'tc' iptables firewall traffic conditioner found" 1>&2
echo "This requires the Linux iptables firewall." 1>&2
exit 1
fi
if [ $EUID -ne 0 ]; then
echo "Aborting: you must run this as root."
exit 2
fi
# Option processing courtesy of Stack Overflow question
# http://stackoverflow.com/questions/402377/using-getopts-in-bash-shell-script-to-get-long-and-short-command-line-options/7680682#7680682
# Questioner: gagneet http://stackoverflow.com/users/35416/gagneet
# Answer: http://stackoverflow.com/a/9899366
# Answerer: http://stackoverflow.com/users/375889/jakesandlund
while test $# -gt 0
do
case $1 in
# Normal option processing
-h | --help)
# usage and help
command=help
break
;;
-v | --version)
command=version
break
;;
-d | --device)
shift
device=$1
;;
-b | --bandwidth)
shift
bandwidth=$1
;;
-l | --latency)
shift
latency=$1
;;
-p | --packetloss)
shift
packetloss=$1
;;
-u | --duplication)
shift
duplication=$1
;;
-c | --corruption)
shift
corruption=$1
;;
-r | --reordering)
shift
reordering=$1
;;
-s | --self_reset_delay_s)
shift
self_reset_delay_s=$1
;;
# ...
# Special cases
--)
break
;;
--*)
# error unknown (long) option $1
;;
-?)
# error unknown (short) option $1
;;
#
# Shortcuts
AMPS|amps)
command=slow
bandwidth=14kbps
latency=250ms
;;
EDGE|edge|2.5G|GPRS|gprs)
command=slow
bandwidth=50kbps
latency=200ms
;;
3G|3g)
command=slow
bandwidth=1000kbps
latency=200ms
;;
4G|4g)
command=slow
bandwidth=10000kbps
latency=100ms
;;
modem-0.1k|modem-110)
command=slow
bandwidth=110bps
latency=350ms
;;
modem-0.3k|modem-300)
command=slow
bandwidth=300bps
latency=300ms
;;
modem-1.2k|modem-1200)
command=slow
bandwidth=1200bps
latency=280ms
;;
modem-2.4k|modem-2400)
command=slow
bandwidth=2400bps
latency=250ms
;;
modem-9.6k|modem-9600)
command=slow
bandwidth=9600bps
latency=200ms
;;
modem-14.4k|modem-14400)
command=slow
bandwidth=14400bps
latency=150ms
;;
modem-28.8k|modem-28800)
command=slow
bandwidth=28800bps
latency=150ms
;;
modem-56k|modem-56000)
command=slow
bandwidth=56kbps
latency=120ms
;;
56k)
command=slow
bandwidth=56kbps
latency=40ms
;;
T1|t1)
command=slow
bandwidth=1500kbps
latency=20ms
;;
T3|t3)
command=slow
bandwidth=45mbps
latency=10ms
;;
DSL|dsl)
command=slow
bandwidth=2mbps
latency=40ms
;;
cablemodem)
command=slow
bandwidth=10mbps
latency=20ms
;;
wifi-a|wifi-g)
command=slow
bandwidth=54mbps
latency=5ms
;;
wifi-b)
command=slow
bandwidth=11mbps
latency=10ms
;;
wifi-n)
command=slow
bandwidth=110mbps
latency=2ms
;;
vsat)
command=slow
bandwidth=5mbps
latency=500ms
;;
clear|reset)
command=clear
;;
status)
command=status
break
;;
# FUN STUFF HERE:
# Split apart combined short options
-*)
split=$1
shift
set -- $(echo "$split" | cut -c 2- | sed 's/./-& /g') "$@"
continue
;;
# Done with options
*)
break
;;
esac
# for testing purposes:
echo "param $1"
shift
done
echo "command=$command"
echo "-d device=$device"
echo "-b bandwidth=$bandwidth"
echo "-l latency=$latency"
echo "-p packetloss=$packetloss"
echo "-u duplication=$duplication"
echo "-c corruption=$corruption"
echo "-r reordering=$reordering"
echo "-s self_reset_delay_s=$self_reset_delay_s"
case $command in
help)
echo $0
echo 'Usage: slow <network-type> [-d device] [-b bandwidth] [-l latency] [-p %drop] [-u %duplication] [-c %corruption] [-r %reordering] [-s self_reset_delay_s]'
echo ' slow reset'
echo ' slow status'
echo
echo '"network-type" type can be:'
echo ' GPRS'
echo ' GSM'
echo ' EDGE'
echo ' 2.5G'
echo ' GPRS'
echo ' 3G'
echo ' 4G'
echo ' modem-2.4k'
echo ' modem-9.6k'
echo ' modem-14.4k'
echo ' modem-28.8k'
echo ' modem-56k'
echo ' 56k'
echo ' T1'
echo ' T3'
echo ' DSL'
echo ' cablemodem'
echo ' wifi-a'
echo ' wifi-b'
echo ' wifi-g'
echo ' wifi-n'
echo ' eth-10'
echo ' eth-100'
echo ' eth-1000'
echo ' vsat'
echo ' vsat-busy'
;;
version)
# version info
echo "slow version $VERSION"
echo "Copyright (c) 2012 Modus Create"
;;
slow)
# Credit to Superuser for the basic commands that run this
# Question: http://superuser.com/questions/147156/simulating-a-low-bandwidth-high-latency-network-connection-on-linux
# Author: Justin L. http://superuser.com/users/38740/justin-l
# Answer: http://superuser.com/a/147434/159810
if tc qdisc show dev $device | fgrep -q "qdisc htb 1: root"; then
verb=change
echo "Changing existing queuing discipline"
else
verb=add
echo "Adding new queuing discipline"
echorun tc qdisc $verb dev $device root handle 1: htb default 12
fi
if ! lsmod | fgrep -q "sch_netem"; then
modprobe sch_netem
fi
echorun tc class $verb dev $device parent 1:1 classid 1:12 htb rate $bandwidth ceil $bandwidth &&
echorun tc qdisc $verb dev $device parent 1:12 netem delay $latency loss $packetloss duplicate $duplication corrupt $corruption reorder $reordering
if [ ${self_reset_delay_s} -ne 0 ]; then
echo "Resetting after ${self_reset_delay_s}s of slow"
sleep ${self_reset_delay_s}
echo resetting queueing discipline
echorun tc qdisc del dev ${device} root
fi
retcode=$?
;;
clear)
echo resetting queueing discipline
echorun tc qdisc del dev $device root
retcode=$?
;;
status)
echorun tc qdisc
retcode=$?
;;
esac
exit $retcode