Browse Source

[wifi] capture-sample: shellcheck & checkbashisms

Matteo Cypriani 11 months ago
parent
commit
f9ccbd3f92
1 changed files with 46 additions and 45 deletions
  1. 46
    45
      wifi/capture-sample.sh

+ 46
- 45
wifi/capture-sample.sh View File

@@ -52,7 +52,7 @@ CHANNEL=1
52 52
 # Displays the message in argument on the error output and exits
53 53
 error()
54 54
 {
55
-	echo "$1" >&2
55
+	printf '%s\n' "$1" >&2
56 56
 	clean
57 57
 	exit 1
58 58
 }
@@ -66,7 +66,7 @@ clean()
66 66
 # Verifies the presence of the needed programs
67 67
 check_dependencies()
68 68
 {
69
-	which tcpdump >/dev/null \
69
+	command -v tcpdump >/dev/null \
70 70
 		|| error "tcpdump is required to run this program!"
71 71
 
72 72
 	if [ "$OS" = Linux ] ; then
@@ -80,25 +80,25 @@ check_dependencies()
80 80
 iface_set_channel()
81 81
 {
82 82
 	echo "Setting interface $IFACE on channel $1..."
83
-	case $OS in
83
+	case "$OS" in
84 84
 		Linux)
85 85
 			iface_down
86
-			iwconfig $IFACE mode managed
86
+			iwconfig "$IFACE" mode managed
87 87
 			iface_up
88
-			iwconfig $IFACE channel $1
88
+			iwconfig "$IFACE" channel "$1"
89 89
 			;;
90 90
 		NetBSD | OpenBSD)
91 91
 			iface_down
92
-			ifconfig $IFACE media autoselect chan $1
92
+			ifconfig "$IFACE" media autoselect chan "$1"
93 93
 			iface_up
94 94
 			;;
95 95
 		*)
96 96
 			error "Your OS is not supported."
97 97
 			;;
98 98
 	esac \
99
-		&& echo "Channel set." \
100 99
 		|| error "Cannot set the channel!"
101 100
 
101
+	echo "Channel set."
102 102
 	iface_monitor
103 103
 }
104 104
 
@@ -107,54 +107,56 @@ iface_monitor()
107 107
 {
108 108
 	iface_down
109 109
 
110
-	echo -n "Switching interface $IFACE to monitor mode... "
111
-	case $OS in
110
+	printf 'Switching interface %s to monitor mode... ' "$IFACE"
111
+	case "$OS" in
112 112
 		Linux)
113
-			iwconfig $IFACE mode monitor
113
+			iwconfig "$IFACE" mode monitor
114 114
 			;;
115 115
 		NetBSD | OpenBSD)
116
-			ifconfig $IFACE media autoselect mediaopt monitor
116
+			ifconfig "$IFACE" media autoselect mediaopt monitor
117 117
 			;;
118 118
 		*)
119 119
 			error "Your OS is not supported."
120 120
 			;;
121 121
 	esac \
122
-		&& echo "OK." \
123 122
 		|| error "Cannot switch the interface to monitor mode!"
124 123
 
124
+	echo "OK."
125 125
 	iface_up
126 126
 }
127 127
 
128 128
 # Shuts down the capture interface
129 129
 iface_down()
130 130
 {
131
-	echo -n "Shuting down interface $IFACE... "
132
-	ifconfig $IFACE down && echo "OK." \
131
+	printf 'Shuting down interface %s... ' "$IFACE"
132
+	ifconfig "$IFACE" down \
133 133
 		|| error "Cannot shut down the interface!"
134
+	echo "OK."
134 135
 }
135 136
 
136 137
 # Turns on the capture interface
137 138
 iface_up()
138 139
 {
139
-	echo -n "Turning up interface $IFACE... "
140
-	ifconfig $IFACE up && echo "OK." \
140
+	printf 'Turning up interface %s... ' "$IFACE"
141
+	ifconfig "$IFACE" up \
141 142
 		|| error "Cannot turn the interface up!"
143
+	echo "OK."
142 144
 }
143 145
 
144 146
 # Invokes tcpdump and displays the number of packets captured
145 147
 invoke_tcpdump()
146 148
 {
147
-	NCAP=$(tcpdump -i $IFACE -c $NB_PKT -w "$FILE" 2>&1 \
149
+	NCAP=$(tcpdump -i "$IFACE" -c "$NB_PKT" -w "$FILE" 2>&1 \
148 150
 		| sed -nr 's/([[:digit:]]+) packets received by filter$/\1/p')
149
-	[ "$NCAP" = "" ] \
151
+	[ -z "$NCAP" ] \
150 152
 		&& error "Error parsing the tcpdump messages! (NCAP=\"$NCAP\")"
151
-	echo $NCAP
153
+	echo "$NCAP"
152 154
 }
153 155
 
154 156
 # Waits for a number of seconds, then kills any tcpdump process
155 157
 wait_tcpdump()
156 158
 {
157
-	sleep $1
159
+	sleep "$1"
158 160
 	echo "$1 seconds passed, killing all tcpdump processes..."
159 161
 	pkill tcpdump
160 162
 }
@@ -162,15 +164,15 @@ wait_tcpdump()
162 164
 # Gets some information about the running system
163 165
 gather_system_information()
164 166
 {
165
-	echo -n "Gathering system information... "
167
+	printf "Gathering system information... "
166 168
 	# Kernel & other information:
167 169
 	uname -a >"$DESTDIR"/uname-a
168 170
 	# Wi-Fi interface information:
169
-	ifconfig $IFACE >"$DESTDIR"/ifconfig_$IFACE
170
-	[ $OS = Linux ] \
171
-		&& iwconfig $IFACE >"$DESTDIR"/iwconfig_$IFACE
171
+	ifconfig "$IFACE" >"$DESTDIR/ifconfig_$IFACE"
172
+	[ "$OS" = Linux ] \
173
+		&& iwconfig "$IFACE" >"$DESTDIR/iwconfig_$IFACE"
172 174
 	# PCI devices:
173
-	which lspci >/dev/null \
175
+	command -v lspci >/dev/null \
174 176
 		&& lspci >"$DESTDIR"/lspci \
175 177
 		|| echo "lspci not available! Please install pciutils. "
176 178
 	# USB devices:
@@ -183,14 +185,14 @@ gather_system_information()
183 185
 # Gets information about the plugged usb devices
184 186
 gather_usb_devices()
185 187
 {
186
-	case $OS in
188
+	case "$OS" in
187 189
 		Linux)
188
-			which lsusb >/dev/null \
190
+			command -v lsusb >/dev/null \
189 191
 				&& lsusb >"$DESTDIR"/lsusb \
190 192
 				|| echo "lsusb not available! Please install usbutils. "
191 193
 			;;
192 194
 		NetBSD | OpenBSD | DragonFly)
193
-			which usbstats >/dev/null \
195
+			command -v usbstats >/dev/null \
194 196
 				&& usbstats >"$DESTDIR"/usbstats \
195 197
 				|| echo "usbstats not available! Please install usbutil. "
196 198
 			;;
@@ -200,7 +202,7 @@ gather_usb_devices()
200 202
 # Gets information about the loaded kernel modules
201 203
 gather_kernel_modules()
202 204
 {
203
-	case $OS in
205
+	case "$OS" in
204 206
 		Linux)
205 207
 			lsmod >"$DESTDIR"/lsmod
206 208
 			;;
@@ -217,7 +219,7 @@ gather_kernel_modules()
217 219
 create_archive()
218 220
 {
219 221
 	TARBALL="${DESTDIR}.tar.gz"
220
-	DIR=`basename "$DESTDIR"`
222
+	DIR=$(basename "$DESTDIR")
221 223
 	tar -C "$TMP" -czf "$TARBALL" "$DIR"
222 224
 	echo "Archive \"$TARBALL\" created."
223 225
 }
@@ -231,14 +233,14 @@ create_archive()
231 233
 # Interface to capture from:
232 234
 IFACE=$1
233 235
 # Machine information:
234
-OS=`uname`
235
-OS_RELEASE=`uname -r`
236
-HOSTNAME=`uname -n`
236
+OS=$(uname)
237
+OS_RELEASE=$(uname -r)
238
+HOSTNAME=$(uname -n)
237 239
 # Current date:
238
-DATE=`date +%FT%H%M%S`
240
+DATE=$(date +%FT%H%M%S)
239 241
 # Temporary destination directory:
240 242
 DESTDIR=$(mktemp -d \
241
-	"$TMP"/capture_${OS}-${OS_RELEASE}_${IFACE}_${HOSTNAME}_${DATE}_XXX)
243
+	"$TMP/capture_${OS}-${OS_RELEASE}_${IFACE}_${HOSTNAME}_${DATE}_XXX")
242 244
 # Update capture file with full path:
243 245
 FILE="$DESTDIR/$FILE"
244 246
 
@@ -246,26 +248,25 @@ check_dependencies
246 248
 
247 249
 echo "Trying to capture $NB_PKT packets..."
248 250
 CAPTURED=0
249
-while [ $CAPTURED -eq 0 -a $CHANNEL -le 14 ] ; do
251
+while [ "$CAPTURED" -eq 0 ] && [ "$CHANNEL" -le 14 ] ; do
250 252
 	echo
251
-	iface_set_channel $CHANNEL
252
-	wait_tcpdump $TIMEOUT &
253
+	iface_set_channel "$CHANNEL"
254
+	wait_tcpdump "$TIMEOUT" &
253 255
 	CAPTURED=$(invoke_tcpdump)
254
-	[ $CAPTURED -eq 0 ] \
256
+	[ "$CAPTURED" -eq 0 ] \
255 257
 		&& echo "No packet captured on channel $CHANNEL." \
256 258
 		|| echo "$CAPTURED packets captured on channel $CHANNEL."
257
-	CHANNEL=`expr $CHANNEL + 1`
259
+	CHANNEL=$((CHANNEL + 1))
258 260
 done
259 261
 
260 262
 echo
261
-[ $CAPTURED -gt 0 ] \
262
-	&& echo "Capture file \"$FILE\" created." \
263
+[ "$CAPTURED" -gt 0 ] \
263 264
 	|| error "Failed to capture any packet!"
265
+echo "Capture file \"$FILE\" created."
264 266
 
265 267
 gather_system_information
266 268
 create_archive
267 269
 clean
268 270
 
269
-echo
270
-echo "You can now shut down the interface $IFACE if you want:"
271
-echo "	ifconfig $IFACE down"
271
+printf '\nYou can now shut down the interface %s if you would like:\n' "$IFACE"
272
+printf '\tifconfig %s down\n' "$IFACE"

Loading…
Cancel
Save