Browse Source

[wifi] capture-sample: shellcheck & checkbashisms

master
Matteo Cypriani 1 year 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
# Displays the message in argument on the error output and exits
error()
{
echo "$1" >&2
printf '%s\n' "$1" >&2
clean
exit 1
}
@@ -66,7 +66,7 @@ clean()
# Verifies the presence of the needed programs
check_dependencies()
{
which tcpdump >/dev/null \
command -v tcpdump >/dev/null \
|| error "tcpdump is required to run this program!"

if [ "$OS" = Linux ] ; then
@@ -80,25 +80,25 @@ check_dependencies()
iface_set_channel()
{
echo "Setting interface $IFACE on channel $1..."
case $OS in
case "$OS" in
Linux)
iface_down
iwconfig $IFACE mode managed
iwconfig "$IFACE" mode managed
iface_up
iwconfig $IFACE channel $1
iwconfig "$IFACE" channel "$1"
;;
NetBSD | OpenBSD)
iface_down
ifconfig $IFACE media autoselect chan $1
ifconfig "$IFACE" media autoselect chan "$1"
iface_up
;;
*)
error "Your OS is not supported."
;;
esac \
&& echo "Channel set." \
|| error "Cannot set the channel!"

echo "Channel set."
iface_monitor
}

@@ -107,54 +107,56 @@ iface_monitor()
{
iface_down

echo -n "Switching interface $IFACE to monitor mode... "
case $OS in
printf 'Switching interface %s to monitor mode... ' "$IFACE"
case "$OS" in
Linux)
iwconfig $IFACE mode monitor
iwconfig "$IFACE" mode monitor
;;
NetBSD | OpenBSD)
ifconfig $IFACE media autoselect mediaopt monitor
ifconfig "$IFACE" media autoselect mediaopt monitor
;;
*)
error "Your OS is not supported."
;;
esac \
&& echo "OK." \
|| error "Cannot switch the interface to monitor mode!"

echo "OK."
iface_up
}

# Shuts down the capture interface
iface_down()
{
echo -n "Shuting down interface $IFACE... "
ifconfig $IFACE down && echo "OK." \
printf 'Shuting down interface %s... ' "$IFACE"
ifconfig "$IFACE" down \
|| error "Cannot shut down the interface!"
echo "OK."
}

# Turns on the capture interface
iface_up()
{
echo -n "Turning up interface $IFACE... "
ifconfig $IFACE up && echo "OK." \
printf 'Turning up interface %s... ' "$IFACE"
ifconfig "$IFACE" up \
|| error "Cannot turn the interface up!"
echo "OK."
}

# Invokes tcpdump and displays the number of packets captured
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')
[ "$NCAP" = "" ] \
[ -z "$NCAP" ] \
&& error "Error parsing the tcpdump messages! (NCAP=\"$NCAP\")"
echo $NCAP
echo "$NCAP"
}

# Waits for a number of seconds, then kills any tcpdump process
wait_tcpdump()
{
sleep $1
sleep "$1"
echo "$1 seconds passed, killing all tcpdump processes..."
pkill tcpdump
}
@@ -162,15 +164,15 @@ wait_tcpdump()
# Gets some information about the running system
gather_system_information()
{
echo -n "Gathering system information... "
printf "Gathering system information... "
# Kernel & other information:
uname -a >"$DESTDIR"/uname-a
# Wi-Fi interface information:
ifconfig $IFACE >"$DESTDIR"/ifconfig_$IFACE
[ $OS = Linux ] \
&& iwconfig $IFACE >"$DESTDIR"/iwconfig_$IFACE
ifconfig "$IFACE" >"$DESTDIR/ifconfig_$IFACE"
[ "$OS" = Linux ] \
&& iwconfig "$IFACE" >"$DESTDIR/iwconfig_$IFACE"
# PCI devices:
which lspci >/dev/null \
command -v lspci >/dev/null \
&& lspci >"$DESTDIR"/lspci \
|| echo "lspci not available! Please install pciutils. "
# USB devices:
@@ -183,14 +185,14 @@ gather_system_information()
# Gets information about the plugged usb devices
gather_usb_devices()
{
case $OS in
case "$OS" in
Linux)
which lsusb >/dev/null \
command -v lsusb >/dev/null \
&& lsusb >"$DESTDIR"/lsusb \
|| echo "lsusb not available! Please install usbutils. "
;;
NetBSD | OpenBSD | DragonFly)
which usbstats >/dev/null \
command -v usbstats >/dev/null \
&& usbstats >"$DESTDIR"/usbstats \
|| echo "usbstats not available! Please install usbutil. "
;;
@@ -200,7 +202,7 @@ gather_usb_devices()
# Gets information about the loaded kernel modules
gather_kernel_modules()
{
case $OS in
case "$OS" in
Linux)
lsmod >"$DESTDIR"/lsmod
;;
@@ -217,7 +219,7 @@ gather_kernel_modules()
create_archive()
{
TARBALL="${DESTDIR}.tar.gz"
DIR=`basename "$DESTDIR"`
DIR=$(basename "$DESTDIR")
tar -C "$TMP" -czf "$TARBALL" "$DIR"
echo "Archive \"$TARBALL\" created."
}
@@ -231,14 +233,14 @@ create_archive()
# Interface to capture from:
IFACE=$1
# Machine information:
OS=`uname`
OS_RELEASE=`uname -r`
HOSTNAME=`uname -n`
OS=$(uname)
OS_RELEASE=$(uname -r)
HOSTNAME=$(uname -n)
# Current date:
DATE=`date +%FT%H%M%S`
DATE=$(date +%FT%H%M%S)
# Temporary destination directory:
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:
FILE="$DESTDIR/$FILE"

@@ -246,26 +248,25 @@ check_dependencies

echo "Trying to capture $NB_PKT packets..."
CAPTURED=0
while [ $CAPTURED -eq 0 -a $CHANNEL -le 14 ] ; do
while [ "$CAPTURED" -eq 0 ] && [ "$CHANNEL" -le 14 ] ; do
echo
iface_set_channel $CHANNEL
wait_tcpdump $TIMEOUT &
iface_set_channel "$CHANNEL"
wait_tcpdump "$TIMEOUT" &
CAPTURED=$(invoke_tcpdump)
[ $CAPTURED -eq 0 ] \
[ "$CAPTURED" -eq 0 ] \
&& echo "No packet captured on channel $CHANNEL." \
|| echo "$CAPTURED packets captured on channel $CHANNEL."
CHANNEL=`expr $CHANNEL + 1`
CHANNEL=$((CHANNEL + 1))
done

echo
[ $CAPTURED -gt 0 ] \
&& echo "Capture file \"$FILE\" created." \
[ "$CAPTURED" -gt 0 ] \
|| error "Failed to capture any packet!"
echo "Capture file \"$FILE\" created."

gather_system_information
create_archive
clean

echo
echo "You can now shut down the interface $IFACE if you want:"
echo " ifconfig $IFACE down"
printf '\nYou can now shut down the interface %s if you would like:\n' "$IFACE"
printf '\tifconfig %s down\n' "$IFACE"

Loading…
Cancel
Save