[wifi] capture: refactoring
This commit is contained in:
parent
6739ade2a6
commit
edc7cc4dfd
198
wifi/capture.sh
198
wifi/capture.sh
|
@ -32,6 +32,23 @@
|
||||||
#set -x
|
#set -x
|
||||||
set -e
|
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
|
# Displays the message in argument and exits
|
||||||
error()
|
error()
|
||||||
{
|
{
|
||||||
|
@ -39,14 +56,59 @@ error()
|
||||||
exit 1
|
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
|
# Switches the capture interface on the channel in argument
|
||||||
set_channel()
|
iface_set_channel()
|
||||||
{
|
{
|
||||||
echo -n "Setting interface $IFACE on channel $1... "
|
echo -n "Setting interface $IFACE on channel $1... "
|
||||||
case $OS in
|
case $OS in
|
||||||
Linux)
|
Linux)
|
||||||
[ -x /sbin/iwconfig ] \
|
|
||||||
|| error "iwconfig is required to run this program on Linux!"
|
|
||||||
iwconfig $IFACE channel $1
|
iwconfig $IFACE channel $1
|
||||||
;;
|
;;
|
||||||
NetBSD)
|
NetBSD)
|
||||||
|
@ -74,25 +136,57 @@ wait_tcpdump()
|
||||||
pkill 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 ] \
|
[ $# -eq 1 ] \
|
||||||
|| error "Usage: $0 <wifi_interface>"
|
|| error "Usage: $0 <wifi_interface>"
|
||||||
|
|
||||||
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:
|
# Interface to capture from:
|
||||||
IFACE=$1
|
IFACE=$1
|
||||||
# Machine information:
|
# Machine information:
|
||||||
|
@ -102,30 +196,11 @@ HOSTNAME=`uname -n`
|
||||||
# Current date:
|
# Current date:
|
||||||
DATE=`date +%FT%H%M%S`
|
DATE=`date +%FT%H%M%S`
|
||||||
|
|
||||||
echo -n "Shuting down interface $IFACE... "
|
check_dependencies
|
||||||
ifconfig $IFACE down && echo "OK." \
|
|
||||||
|| error "Cannot shut down the interface!"
|
|
||||||
|
|
||||||
echo -n "Switching interface $IFACE to monitor mode... "
|
iface_down
|
||||||
case $OS in
|
iface_monitor
|
||||||
Linux)
|
iface_up
|
||||||
[ -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!"
|
|
||||||
|
|
||||||
echo "Trying to capture $NB_PKT packets..."
|
echo "Trying to capture $NB_PKT packets..."
|
||||||
DIR=$(mktemp -d \
|
DIR=$(mktemp -d \
|
||||||
|
@ -133,7 +208,7 @@ DIR=$(mktemp -d \
|
||||||
FILE="$DIR/$FILE"
|
FILE="$DIR/$FILE"
|
||||||
CAPTURED=0
|
CAPTURED=0
|
||||||
while [ $CAPTURED -eq 0 -a $CHANNEL -le 14 ] ; do
|
while [ $CAPTURED -eq 0 -a $CHANNEL -le 14 ] ; do
|
||||||
set_channel $CHANNEL
|
iface_set_channel $CHANNEL
|
||||||
wait_tcpdump $TIMEOUT &
|
wait_tcpdump $TIMEOUT &
|
||||||
invoke_tcpdump
|
invoke_tcpdump
|
||||||
CAPTURED=$?
|
CAPTURED=$?
|
||||||
|
@ -146,42 +221,9 @@ done
|
||||||
&& echo "Capture file \"$FILE\" created with $CAPTURED packets." \
|
&& echo "Capture file \"$FILE\" created with $CAPTURED packets." \
|
||||||
|| error "Failed to capture any packet!"
|
|| error "Failed to capture any packet!"
|
||||||
|
|
||||||
echo -n "Gathering system information... "
|
gather_system_information
|
||||||
# Kernel & other information:
|
create_archive
|
||||||
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."
|
|
||||||
|
|
||||||
FILE="${DIR}.tar.gz"
|
|
||||||
DIR=`basename "$DIR"`
|
|
||||||
tar -C "$TMP" -czf "$FILE" "$DIR"
|
|
||||||
echo "Archive \"$FILE\" created."
|
|
||||||
rm -fr "$TMP/$DIR"
|
rm -fr "$TMP/$DIR"
|
||||||
|
|
||||||
echo
|
echo
|
||||||
|
|
Loading…
Reference in New Issue