wifi_test/runtest.sh

117 lines
3.4 KiB
Bash
Executable file

#!/bin/bash
source settings.env
# Check if email recipient is set
if [ -z "$RECIPIENT" ]; then
echo "[!] Please set the RECIPIENT variable in settings.env."
exit 1
fi
sudo -v
while true; do sudo -n true; sleep 60; done 2>/dev/null &
SUDO_KEEPALIVE_PID=$!
echo "Starting kismet..."
sudo systemctl start kismet
echo "Saturating the capture..."
sleep $LEAD_TIME
# Function to get current TX failed count
get_tx_failed() {
iw dev $INTERFACE station dump | awk '/tx failed/ {print $3}'
}
freq_to_channel() {
local freq=$1
local channel=0
if [ "$freq" -ge 2412 ] && [ "$freq" -le 2472 ]; then
channel=$(( (freq - 2407) / 5 ))
elif [ "$freq" -eq 2484 ]; then
channel=14
elif [ "$freq" -ge 5180 ] && [ "$freq" -le 5825 ]; then
channel=$(( (freq - 5000) / 5 ))
else
channel="Unknown"
fi
echo "$channel"
}
# Start test email
echo -e "Subject: Test ${BOOT_ID} Started\n\nThis is to inform you that the tests have commenced for test ${BOOT_ID}." | msmtp $RECIPIENT
COUNTER=0
FAILED_START=$(get_tx_failed)
# Create CSV header if needed
if [ ! -f "$TEST_FILE" ]; then
echo "StartTimestamp,EndTimestamp,Link,Level,Noise,BSSID,TX Bitrate,RX Bitrate,$(speedtest --csv-header),TX Failures,Channel,Frequency,Packet Loss,Jitter" > "$TEST_FILE"
fi
while [ "$COUNTER" -lt "$NUM_TESTS" ]; do
COUNTER=$((COUNTER + 1))
echo "Executing test $COUNTER of $NUM_TESTS..."
for ((i=1; i<=NUM_SAMPLES; i++)); do
echo " Gathering sample $i of $NUM_SAMPLES..."
START_TIME=$(date -Iseconds)
# Wireless stats
link_level_noise=$(awk 'NR==3 {gsub(/\./, "", $3); gsub(/\./, "", $4); gsub(/\./, "", $5); print $3","$4","$5}' /proc/net/wireless)
bssid_and_bitrate=$(iw dev $INTERFACE link | awk '/Connected/ {bssid=$3} /tx bitrate/ {tx=$3} /rx bitrate/ {rx=$3} END {print bssid","tx","rx}')
# Speed test
speed_results=$(speedtest --secure --csv)
# TX failure delta
FAILED_NOW=$(get_tx_failed)
FAILED_DELTA=$((FAILED_NOW - FAILED_START))
FAILED_START=$FAILED_NOW # Update for next sample
freq=$(iw dev $INTERFACE link | awk '/freq:/ {print $2}')
channel=$(freq_to_channel "$freq")
packet_loss=$(ping -c $PING_COUNT -q $PING_TARGET | grep -oP '\d+(?=% packet loss)')
jitter=$(ping -c $PING_COUNT $PING_TARGET | grep "time=" | awk '{print $(NF-1)}' | sed 's/time=//g' | awk '{sum+=$1; sumsq+=$1*$1} END {if (NR>1) print sqrt(sumsq/NR - (sum/NR)**2); else print 0}')
END_TIME=$(date -Iseconds)
# Log everything
echo "$START_TIME,$END_TIME,$link_level_noise,$bssid_and_bitrate,$speed_results,$FAILED_DELTA,$channel,$freq,$packet_loss,$jitter" >> "$TEST_FILE"
done
if [ "$COUNTER" -lt "$NUM_TESTS" ]; then
echo "Dozing off for $TIME_BETWEEN..."
sleep $TIME_BETWEEN
fi
done
echo "Stopping kismet..."
sudo systemctl stop kismet
# Let's enrich the data with passive metrics.
echo "Enriching the data..."
KISMET_LOG=$(find ~/kismet_logs -type f -name "*.pcapng" -printf "%T@ %p\n" | sort -n | tail -1 | cut -d' ' -f2-)
if [ -z "$KISMET_LOG" ] || [ ! -f "$KISMET_LOG" ]; then
echo "[!] Packet capture not found."
exit 1
fi
python3 $SCRIPT_DIRECTORY/enrich.py --csv $TEST_FILE --pcapng "$KISMET_LOG" --output "$ENRICHED_FILE"
# Final email with attachment
echo "The test with UID ${BOOT_ID} is complete. Please collect the probe. Data is attached." | \
mutt -s "Test ${BOOT_ID} Complete" -a "$ENRICHED_FILE" -- "$RECIPIENT"
sudo kill $SUDO_KEEPALIVE_PID