diff --git a/wifi/capture.sh b/wifi/capture.sh index c9d7eb6..9aa45b5 100755 --- a/wifi/capture.sh +++ b/wifi/capture.sh @@ -75,25 +75,36 @@ check_dependencies() fi } -# Turns on the capture interface -iface_up() +# Switches the capture interface to monitor mode, on the channel in +# argument +iface_set_channel() { - echo -n "Turning up interface $IFACE... " - ifconfig $IFACE up && echo "OK." \ - || error "Cannot turn the interface up!" -} + iface_down -# Shuts down the capture interface -iface_down() -{ - echo -n "Shuting down interface $IFACE... " - ifconfig $IFACE down && echo "OK." \ - || error "Cannot shut down the interface!" + echo -n "Setting interface $IFACE on channel $1... " + case $OS in + Linux) + iwconfig $IFACE mode managed channel $1 + ;; + NetBSD) + ifconfig $IFACE media autoselect chan $1 + ;; + *) + error "Your OS is not supported." + ;; + esac \ + && echo "OK." \ + || error "Cannot set the channel!" + + iface_up + iface_monitor } # Switches the capture interface to monitor mode iface_monitor() { + iface_down + echo -n "Switching interface $IFACE to monitor mode... " case $OS in Linux) @@ -108,25 +119,24 @@ iface_monitor() esac \ && echo "OK." \ || error "Cannot switch the interface to monitor mode!" + + iface_up } -# Switches the capture interface on the channel in argument -iface_set_channel() +# Shuts down the capture interface +iface_down() { - echo -n "Setting interface $IFACE on channel $1... " - case $OS in - Linux) - iwconfig $IFACE channel $1 - ;; - NetBSD) - ifconfig $IFACE chan $1 - ;; - *) - error "Your OS is not supported." - ;; - esac \ - && echo "OK." \ - || error "Cannot set the channel!" + echo -n "Shuting down interface $IFACE... " + ifconfig $IFACE down && echo "OK." \ + || error "Cannot shut down the interface!" +} + +# Turns on the capture interface +iface_up() +{ + echo -n "Turning up interface $IFACE... " + ifconfig $IFACE up && echo "OK." \ + || error "Cannot turn the interface up!" } # Invokes tcpdump and returns the number of packets captured @@ -210,13 +220,10 @@ FILE="$DESTDIR/$FILE" check_dependencies -iface_down -iface_monitor -iface_up - echo "Trying to capture $NB_PKT packets..." CAPTURED=0 while [ $CAPTURED -eq 0 -a $CHANNEL -le 14 ] ; do + echo iface_set_channel $CHANNEL wait_tcpdump $TIMEOUT & invoke_tcpdump @@ -226,6 +233,7 @@ while [ $CAPTURED -eq 0 -a $CHANNEL -le 14 ] ; do CHANNEL=`expr $CHANNEL + 1` done +echo [ $CAPTURED -gt 0 ] \ && echo "Capture file \"$FILE\" created with $CAPTURED packets." \ || error "Failed to capture any packet!"