-
Notifications
You must be signed in to change notification settings - Fork 0
/
netcrap
executable file
·111 lines (83 loc) · 1.99 KB
/
netcrap
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
#!/bin/bash
# SPDX-License-Identifier: GPL-3.0-only
# Copyright 2024, Mattias Bengtsson <[email protected]>
set -e
set -o pipefail
################################################################################
function add-impairment {
sudo tc qdisc add dev "${ARG_DEV}" root netem "${@}"
}
# shellcheck disable=SC2317
function show-impairments {
sudo tc qdisc show dev "${ARG_DEV}"
}
function turn-on {
echo Turning on network impairments...
add-impairment delay 100000ms
}
function turn-off {
echo Turning off network impairments...
sudo tc qdisc delete dev "${ARG_DEV}" root netem
}
function is-netdev {
test -L "/sys/class/net/${1}"
}
################################################################################
function error {
echo "ERROR: ${*}" >/dev/stderr
}
function usage {
cat <<EOM
Usage: $(basename "${0}") [-h | --help] <on|off>
-h, --help Show this help message.
EOM
}
function parse-args {
local opts flags script
script="$(basename "${0}")"
flags="help,on,off"
opts="$(getopt -o h --long "${flags}" -n "${script}" -- "${@}")"
eval set -- "$opts"
OPT_STATE=on
ARG_DEV=
while true; do
case "${1}" in
-h | --help)
usage
exit
;;
--on)
OPT_STATE=on
shift
;;
--off)
OPT_STATE=off
shift
;;
--) shift; break ;;
*) break ;;
esac
done
if [ "${#}" -lt 1 ]; then
usage
echo
error "No device!"
return 2
fi
ARG_DEV="${1}"
shift
if ! is-netdev "${ARG_DEV}"; then
error "Not a network device: ${ARG_DEV}!"
return 3
fi
export OPT_STATE
export ARG_DEV
}
function main {
parse-args "${@}"
case "${OPT_STATE}" in
on) turn-on ;;
off) turn-off ;;
esac
}
main "${@}"; exit