[wifi] capture-sample: shellcheck & checkbashisms

This commit is contained in:
Matteo Cypriani 2018-04-13 20:21:03 +02:00
parent 9ba0a794aa
commit f9ccbd3f92
1 changed files with 46 additions and 45 deletions

View File

@ -52,7 +52,7 @@ CHANNEL=1
# Displays the message in argument on the error output and exits # Displays the message in argument on the error output and exits
error() error()
{ {
echo "$1" >&2 printf '%s\n' "$1" >&2
clean clean
exit 1 exit 1
} }
@ -66,7 +66,7 @@ clean()
# Verifies the presence of the needed programs # Verifies the presence of the needed programs
check_dependencies() check_dependencies()
{ {
which tcpdump >/dev/null \ command -v tcpdump >/dev/null \
|| error "tcpdump is required to run this program!" || error "tcpdump is required to run this program!"
if [ "$OS" = Linux ] ; then if [ "$OS" = Linux ] ; then
@ -80,25 +80,25 @@ check_dependencies()
iface_set_channel() iface_set_channel()
{ {
echo "Setting interface $IFACE on channel $1..." echo "Setting interface $IFACE on channel $1..."
case $OS in case "$OS" in
Linux) Linux)
iface_down iface_down
iwconfig $IFACE mode managed iwconfig "$IFACE" mode managed
iface_up iface_up
iwconfig $IFACE channel $1 iwconfig "$IFACE" channel "$1"
;; ;;
NetBSD | OpenBSD) NetBSD | OpenBSD)
iface_down iface_down
ifconfig $IFACE media autoselect chan $1 ifconfig "$IFACE" media autoselect chan "$1"
iface_up iface_up
;; ;;
*) *)
error "Your OS is not supported." error "Your OS is not supported."
;; ;;
esac \ esac \
&& echo "Channel set." \
|| error "Cannot set the channel!" || error "Cannot set the channel!"
echo "Channel set."
iface_monitor iface_monitor
} }
@ -107,54 +107,56 @@ iface_monitor()
{ {
iface_down iface_down
echo -n "Switching interface $IFACE to monitor mode... " printf 'Switching interface %s to monitor mode... ' "$IFACE"
case $OS in case "$OS" in
Linux) Linux)
iwconfig $IFACE mode monitor iwconfig "$IFACE" mode monitor
;; ;;
NetBSD | OpenBSD) NetBSD | OpenBSD)
ifconfig $IFACE media autoselect mediaopt monitor ifconfig "$IFACE" media autoselect mediaopt monitor
;; ;;
*) *)
error "Your OS is not supported." error "Your OS is not supported."
;; ;;
esac \ esac \
&& echo "OK." \
|| error "Cannot switch the interface to monitor mode!" || error "Cannot switch the interface to monitor mode!"
echo "OK."
iface_up iface_up
} }
# Shuts down the capture interface # Shuts down the capture interface
iface_down() iface_down()
{ {
echo -n "Shuting down interface $IFACE... " printf 'Shuting down interface %s... ' "$IFACE"
ifconfig $IFACE down && echo "OK." \ ifconfig "$IFACE" down \
|| error "Cannot shut down the interface!" || error "Cannot shut down the interface!"
echo "OK."
} }
# Turns on the capture interface # Turns on the capture interface
iface_up() iface_up()
{ {
echo -n "Turning up interface $IFACE... " printf 'Turning up interface %s... ' "$IFACE"
ifconfig $IFACE up && echo "OK." \ ifconfig "$IFACE" up \
|| error "Cannot turn the interface up!" || error "Cannot turn the interface up!"
echo "OK."
} }
# Invokes tcpdump and displays the number of packets captured # Invokes tcpdump and displays the number of packets captured
invoke_tcpdump() invoke_tcpdump()
{ {
NCAP=$(tcpdump -i $IFACE -c $NB_PKT -w "$FILE" 2>&1 \ NCAP=$(tcpdump -i "$IFACE" -c "$NB_PKT" -w "$FILE" 2>&1 \
| sed -nr 's/([[:digit:]]+) packets received by filter$/\1/p') | sed -nr 's/([[:digit:]]+) packets received by filter$/\1/p')
[ "$NCAP" = "" ] \ [ -z "$NCAP" ] \
&& error "Error parsing the tcpdump messages! (NCAP=\"$NCAP\")" && error "Error parsing the tcpdump messages! (NCAP=\"$NCAP\")"
echo $NCAP echo "$NCAP"
} }
# Waits for a number of seconds, then kills any tcpdump process # Waits for a number of seconds, then kills any tcpdump process
wait_tcpdump() wait_tcpdump()
{ {
sleep $1 sleep "$1"
echo "$1 seconds passed, killing all tcpdump processes..." echo "$1 seconds passed, killing all tcpdump processes..."
pkill tcpdump pkill tcpdump
} }
@ -162,15 +164,15 @@ wait_tcpdump()
# Gets some information about the running system # Gets some information about the running system
gather_system_information() gather_system_information()
{ {
echo -n "Gathering system information... " printf "Gathering system information... "
# Kernel & other information: # Kernel & other information:
uname -a >"$DESTDIR"/uname-a uname -a >"$DESTDIR"/uname-a
# Wi-Fi interface information: # Wi-Fi interface information:
ifconfig $IFACE >"$DESTDIR"/ifconfig_$IFACE ifconfig "$IFACE" >"$DESTDIR/ifconfig_$IFACE"
[ $OS = Linux ] \ [ "$OS" = Linux ] \
&& iwconfig $IFACE >"$DESTDIR"/iwconfig_$IFACE && iwconfig "$IFACE" >"$DESTDIR/iwconfig_$IFACE"
# PCI devices: # PCI devices:
which lspci >/dev/null \ command -v lspci >/dev/null \
&& lspci >"$DESTDIR"/lspci \ && lspci >"$DESTDIR"/lspci \
|| echo "lspci not available! Please install pciutils. " || echo "lspci not available! Please install pciutils. "
# USB devices: # USB devices:
@ -183,14 +185,14 @@ gather_system_information()
# Gets information about the plugged usb devices # Gets information about the plugged usb devices
gather_usb_devices() gather_usb_devices()
{ {
case $OS in case "$OS" in
Linux) Linux)
which lsusb >/dev/null \ command -v lsusb >/dev/null \
&& lsusb >"$DESTDIR"/lsusb \ && lsusb >"$DESTDIR"/lsusb \
|| echo "lsusb not available! Please install usbutils. " || echo "lsusb not available! Please install usbutils. "
;; ;;
NetBSD | OpenBSD | DragonFly) NetBSD | OpenBSD | DragonFly)
which usbstats >/dev/null \ command -v usbstats >/dev/null \
&& usbstats >"$DESTDIR"/usbstats \ && usbstats >"$DESTDIR"/usbstats \
|| echo "usbstats not available! Please install usbutil. " || echo "usbstats not available! Please install usbutil. "
;; ;;
@ -200,7 +202,7 @@ gather_usb_devices()
# Gets information about the loaded kernel modules # Gets information about the loaded kernel modules
gather_kernel_modules() gather_kernel_modules()
{ {
case $OS in case "$OS" in
Linux) Linux)
lsmod >"$DESTDIR"/lsmod lsmod >"$DESTDIR"/lsmod
;; ;;
@ -217,7 +219,7 @@ gather_kernel_modules()
create_archive() create_archive()
{ {
TARBALL="${DESTDIR}.tar.gz" TARBALL="${DESTDIR}.tar.gz"
DIR=`basename "$DESTDIR"` DIR=$(basename "$DESTDIR")
tar -C "$TMP" -czf "$TARBALL" "$DIR" tar -C "$TMP" -czf "$TARBALL" "$DIR"
echo "Archive \"$TARBALL\" created." echo "Archive \"$TARBALL\" created."
} }
@ -231,14 +233,14 @@ create_archive()
# Interface to capture from: # Interface to capture from:
IFACE=$1 IFACE=$1
# Machine information: # Machine information:
OS=`uname` OS=$(uname)
OS_RELEASE=`uname -r` OS_RELEASE=$(uname -r)
HOSTNAME=`uname -n` HOSTNAME=$(uname -n)
# Current date: # Current date:
DATE=`date +%FT%H%M%S` DATE=$(date +%FT%H%M%S)
# Temporary destination directory: # Temporary destination directory:
DESTDIR=$(mktemp -d \ DESTDIR=$(mktemp -d \
"$TMP"/capture_${OS}-${OS_RELEASE}_${IFACE}_${HOSTNAME}_${DATE}_XXX) "$TMP/capture_${OS}-${OS_RELEASE}_${IFACE}_${HOSTNAME}_${DATE}_XXX")
# Update capture file with full path: # Update capture file with full path:
FILE="$DESTDIR/$FILE" FILE="$DESTDIR/$FILE"
@ -246,26 +248,25 @@ check_dependencies
echo "Trying to capture $NB_PKT packets..." echo "Trying to capture $NB_PKT packets..."
CAPTURED=0 CAPTURED=0
while [ $CAPTURED -eq 0 -a $CHANNEL -le 14 ] ; do while [ "$CAPTURED" -eq 0 ] && [ "$CHANNEL" -le 14 ] ; do
echo echo
iface_set_channel $CHANNEL iface_set_channel "$CHANNEL"
wait_tcpdump $TIMEOUT & wait_tcpdump "$TIMEOUT" &
CAPTURED=$(invoke_tcpdump) CAPTURED=$(invoke_tcpdump)
[ $CAPTURED -eq 0 ] \ [ "$CAPTURED" -eq 0 ] \
&& echo "No packet captured on channel $CHANNEL." \ && echo "No packet captured on channel $CHANNEL." \
|| echo "$CAPTURED packets captured on channel $CHANNEL." || echo "$CAPTURED packets captured on channel $CHANNEL."
CHANNEL=`expr $CHANNEL + 1` CHANNEL=$((CHANNEL + 1))
done done
echo echo
[ $CAPTURED -gt 0 ] \ [ "$CAPTURED" -gt 0 ] \
&& echo "Capture file \"$FILE\" created." \
|| error "Failed to capture any packet!" || error "Failed to capture any packet!"
echo "Capture file \"$FILE\" created."
gather_system_information gather_system_information
create_archive create_archive
clean clean
echo printf '\nYou can now shut down the interface %s if you would like:\n' "$IFACE"
echo "You can now shut down the interface $IFACE if you want:" printf '\tifconfig %s down\n' "$IFACE"
echo " ifconfig $IFACE down"