From edc7cc4dfd3baaede017cc7a6f3d4bd2973033c7 Mon Sep 17 00:00:00 2001 From: Matteo Cypriani Date: Sun, 7 Aug 2011 20:41:54 +0200 Subject: [PATCH] [wifi] capture: refactoring --- wifi/capture.sh | 198 +++++++++++++++++++++++++++++------------------- 1 file changed, 120 insertions(+), 78 deletions(-) diff --git a/wifi/capture.sh b/wifi/capture.sh index d4ce263..8bd2a6a 100755 --- a/wifi/capture.sh +++ b/wifi/capture.sh @@ -32,6 +32,23 @@ #set -x set -e + +## Parameters (tune them if you want) ## + +# Number of packet to capture: +NB_PKT=10 +# Maximal time of a capture on a given channel: +TIMEOUT=15 +# Temporary directory: +TMP=/tmp +# Name of the capture file: +FILE=capture.pcap +# First channel to scan: +CHANNEL=1 + + +## Functions ## + # Displays the message in argument and exits error() { @@ -39,14 +56,59 @@ error() exit 1 } +# Verifies the presence of the needed programs +check_dependencies() +{ + which tcpdump >/dev/null \ + || error "tcpdump is required to run this program!" + + if [ "$OS" = Linux ] ; then + [ -x /sbin/iwconfig ] \ + || error "iwconfig is required to run this program on Linux!" + fi +} + +# Turns on the capture interface +iface_up() +{ + echo -n "Turning up interface $IFACE... " + ifconfig $IFACE up && echo "OK." \ + || error "Cannot turn the interface up!" +} + +# Shuts down the capture interface +iface_down() +{ + echo -n "Shuting down interface $IFACE... " + ifconfig $IFACE down && echo "OK." \ + || error "Cannot shut down the interface!" +} + +# Switches the capture interface to monitor mode +iface_monitor() +{ + echo -n "Switching interface $IFACE to monitor mode... " + case $OS in + Linux) + iwconfig $IFACE mode monitor + ;; + NetBSD) + ifconfig $IFACE media autoselect mediaopt monitor + ;; + *) + error "Your OS is not supported." + ;; + esac \ + && echo "OK." \ + || error "Cannot switch the interface to monitor mode!" +} + # Switches the capture interface on the channel in argument -set_channel() +iface_set_channel() { echo -n "Setting interface $IFACE on channel $1... " case $OS in Linux) - [ -x /sbin/iwconfig ] \ - || error "iwconfig is required to run this program on Linux!" iwconfig $IFACE channel $1 ;; NetBSD) @@ -74,25 +136,57 @@ wait_tcpdump() pkill tcpdump } +# Gets some information about the running system +gather_system_information() +{ + echo -n "Gathering system information... " + # Kernel & other information: + uname -a >"$DIR"/uname-a + # Wi-Fi interface information: + ifconfig $IFACE >"$DIR"/ifconfig_$IFACE + # PCI devices: + which lspci >/dev/null \ + && lspci >"$DIR"/lspci \ + || echo "lspci not available! Please install pciutils. " + # Loaded kernel modules & USB devices: + case $OS in + Linux) + lsmod >"$DIR"/lsmod + which lsusb >/dev/null \ + && lsusb >"$DIR"/lsusb \ + || echo "lsusb not available! Please install usbutils. " + ;; + NetBSD) + modstat >"$DIR"/modstat + which usbstats >/dev/null \ + && usbstats >"$DIR"/usbstats \ + || echo "usbstats not available! Please install usbutil. " + ;; + DragonFly) + kldstat >"$DIR"/kldstat + which usbstats >/dev/null \ + && usbstats >"$DIR"/usbstats \ + || echo "usbstats not available! Please install usbutil. " + ;; + esac + echo "OK." +} + +# Compresses the destination directory to a tarball +create_archive() +{ + FILE="${DIR}.tar.gz" + DIR=`basename "$DIR"` + tar -C "$TMP" -czf "$FILE" "$DIR" + echo "Archive \"$FILE\" created." +} + + +## Main program ## + [ $# -eq 1 ] \ || error "Usage: $0 " -which tcpdump >/dev/null \ - || error "tcpdump is required to run this program!" - -## Parameters (tune them if you want) ## -# Number of packet to capture: -NB_PKT=10 -# Maximal time of a capture on a given channel: -TIMEOUT=15 -# Temporary directory: -TMP=/tmp -# Name of the capture file: -FILE=capture.pcap -# First channel to scan: -CHANNEL=1 - -## Generated parameters ## # Interface to capture from: IFACE=$1 # Machine information: @@ -102,30 +196,11 @@ HOSTNAME=`uname -n` # Current date: DATE=`date +%FT%H%M%S` -echo -n "Shuting down interface $IFACE... " -ifconfig $IFACE down && echo "OK." \ - || error "Cannot shut down the interface!" +check_dependencies -echo -n "Switching interface $IFACE to monitor mode... " -case $OS in - Linux) - [ -x /sbin/iwconfig ] \ - || error "iwconfig is required to run this program on Linux!" - iwconfig $IFACE mode monitor - ;; - NetBSD) - ifconfig $IFACE media autoselect mediaopt monitor - ;; - *) - error "Your OS is not supported." - ;; -esac \ - && echo "OK." \ - || error "Cannot configure the interface!" - -echo -n "Turning up interface $IFACE... " -ifconfig $IFACE up && echo "OK." \ - || error "Cannot turn the interface up!" +iface_down +iface_monitor +iface_up echo "Trying to capture $NB_PKT packets..." DIR=$(mktemp -d \ @@ -133,7 +208,7 @@ DIR=$(mktemp -d \ FILE="$DIR/$FILE" CAPTURED=0 while [ $CAPTURED -eq 0 -a $CHANNEL -le 14 ] ; do - set_channel $CHANNEL + iface_set_channel $CHANNEL wait_tcpdump $TIMEOUT & invoke_tcpdump CAPTURED=$? @@ -146,42 +221,9 @@ done && echo "Capture file \"$FILE\" created with $CAPTURED packets." \ || error "Failed to capture any packet!" -echo -n "Gathering system information... " -# Kernel & other information: -uname -a >"$DIR"/uname-a -# Wi-Fi interface information: -ifconfig $IFACE >"$DIR"/ifconfig_$IFACE -# PCI devices: -which lspci >/dev/null \ - && lspci >"$DIR"/lspci \ - || echo "lspci not available! Please install pciutils. " -# Loaded kernel modules & USB devices: -case $OS in - Linux) - lsmod >"$DIR"/lsmod - which lsusb >/dev/null \ - && lsusb >"$DIR"/lsusb \ - || echo "lsusb not available! Please install usbutils. " - ;; - NetBSD) - modstat >"$DIR"/modstat - which usbstats >/dev/null \ - && usbstats >"$DIR"/usbstats \ - || echo "usbstats not available! Please install usbutil. " - ;; - DragonFly) - kldstat >"$DIR"/kldstat - which usbstats >/dev/null \ - && usbstats >"$DIR"/usbstats \ - || echo "usbstats not available! Please install usbutil. " - ;; -esac -echo "OK." +gather_system_information +create_archive -FILE="${DIR}.tar.gz" -DIR=`basename "$DIR"` -tar -C "$TMP" -czf "$FILE" "$DIR" -echo "Archive \"$FILE\" created." rm -fr "$TMP/$DIR" echo