From 6739ade2a615769d05c06b86070721dbf48671b9 Mon Sep 17 00:00:00 2001 From: Matteo Cypriani Date: Sun, 7 Aug 2011 20:07:11 +0200 Subject: [PATCH] [wifi] capture: test each channel Try to capture on each channel, in case there is no traffic on the first channel. --- wifi/capture.sh | 62 ++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 56 insertions(+), 6 deletions(-) diff --git a/wifi/capture.sh b/wifi/capture.sh index e3d0c70..d4ce263 100755 --- a/wifi/capture.sh +++ b/wifi/capture.sh @@ -32,12 +32,48 @@ #set -x set -e +# Displays the message in argument and exits error() { echo "$1" exit 1 } +# Switches the capture interface on the channel in argument +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) + ifconfig $IFACE chan $1 + ;; + *) + error "Your OS is not supported." + ;; + esac \ + && echo "OK." \ + || error "Cannot set the channel!" +} + +# Invokes tcpdump and returns the number of packets captured +invoke_tcpdump() +{ + return $(tcpdump -i $IFACE -c $NB_PKT -w "$FILE" 2>&1 \ + | sed -n 's/ packets captured$//p') +} + +# Waits for a number of seconds, then kills any tcpdump process +wait_tcpdump() +{ + sleep $1 + pkill tcpdump +} + [ $# -eq 1 ] \ || error "Usage: $0 " @@ -47,11 +83,13 @@ which tcpdump >/dev/null \ ## 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 -# Channel to scan +# First channel to scan: CHANNEL=1 ## Generated parameters ## @@ -73,10 +111,10 @@ case $OS in Linux) [ -x /sbin/iwconfig ] \ || error "iwconfig is required to run this program on Linux!" - iwconfig $IFACE mode monitor channel $CHANNEL + iwconfig $IFACE mode monitor ;; NetBSD) - ifconfig $IFACE media autoselect mediaopt monitor chan $CHANNEL + ifconfig $IFACE media autoselect mediaopt monitor ;; *) error "Your OS is not supported." @@ -89,12 +127,24 @@ echo -n "Turning up interface $IFACE... " ifconfig $IFACE up && echo "OK." \ || error "Cannot turn the interface up!" -echo "Capturing $NB_PKT packets..." +echo "Trying to capture $NB_PKT packets..." DIR=$(mktemp -d \ "$TMP"/capture_${OS}-${OS_RELEASE}_${IFACE}_${HOSTNAME}_${DATE}_XX) FILE="$DIR/$FILE" -tcpdump -i $IFACE -c $NB_PKT -w "$FILE" -echo "Capture file \"$FILE\" created." +CAPTURED=0 +while [ $CAPTURED -eq 0 -a $CHANNEL -le 14 ] ; do + set_channel $CHANNEL + wait_tcpdump $TIMEOUT & + invoke_tcpdump + CAPTURED=$? + [ $CAPTURED -eq 0 ] \ + && echo "No packet captured on channel $CHANNEL." + CHANNEL=`expr $CHANNEL + 1` +done + +[ $CAPTURED -gt 0 ] \ + && echo "Capture file \"$FILE\" created with $CAPTURED packets." \ + || error "Failed to capture any packet!" echo -n "Gathering system information... " # Kernel & other information: