Refactor loops and improve formatting in runtest.sh for better readability

This commit is contained in:
Yaro Kasear 2025-04-21 14:53:40 -05:00
parent d92621d2f0
commit 434add2694

View file

@ -18,7 +18,10 @@ fi
sudo -v sudo -v
while true; do sudo -n true; sleep 60; done 2>/dev/null & while true; do
sudo -n true
sleep 60
done 2>/dev/null &
SUDO_KEEPALIVE_PID=$! SUDO_KEEPALIVE_PID=$!
echo "Starting kismet..." echo "Starting kismet..."
@ -39,11 +42,11 @@ freq_to_channel() {
local channel=0 local channel=0
if [ "$freq" -ge 2412 ] && [ "$freq" -le 2472 ]; then if [ "$freq" -ge 2412 ] && [ "$freq" -le 2472 ]; then
channel=$(( (freq - 2407) / 5 )) channel=$(((freq - 2407) / 5))
elif [ "$freq" -eq 2484 ]; then elif [ "$freq" -eq 2484 ]; then
channel=14 channel=14
elif [ "$freq" -ge 5180 ] && [ "$freq" -le 5825 ]; then elif [ "$freq" -ge 5180 ] && [ "$freq" -le 5825 ]; then
channel=$(( (freq - 5000) / 5 )) channel=$(((freq - 5000) / 5))
else else
channel="Unknown" channel="Unknown"
fi fi
@ -83,8 +86,8 @@ run_iperf() {
end' || echo "execution-failed") end' || echo "execution-failed")
if [[ "$result" == "iperf3-error" || "$result" == "no-end" || "$result" == "unexpected-format" || "$result" == "execution-failed" ]]; then if [[ "$result" == "iperf3-error" || "$result" == "no-end" || "$result" == "unexpected-format" || "$result" == "execution-failed" ]]; then
echo "$(date -Iseconds),iperf $mode $direction to $target failed with '$result'" >> "$FAILURE_LOG" echo "$(date -Iseconds),iperf $mode $direction to $target failed with '$result'" >>"$FAILURE_LOG"
echo "[stderr] $(cat "$tmp_err")" >> "$FAILURE_LOG" echo "[stderr] $(cat "$tmp_err")" >>"$FAILURE_LOG"
echo "0" echo "0"
else else
echo "$result" echo "$result"
@ -101,15 +104,14 @@ FAILED_START=$(get_tx_failed)
# Create CSV header if needed # Create CSV header if needed
if [ ! -f "$TEST_FILE" ]; then 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,LocalTCPUp,LocalTCPDown,LocalUDPUp,LocalUDPDown,RemoteTCPUp,RemoteTCPDown,RemoteUDPUp,RemoteUDPDown" > "$TEST_FILE" echo "StartTimestamp,EndTimestamp,Link,Level,Noise,BSSID,TX Bitrate,RX Bitrate,$(speedtest --csv-header),TX Failures,Channel,Frequency,Packet Loss,Jitter,LocalTCPUp,LocalTCPDown,LocalUDPUp,LocalUDPDown,RemoteTCPUp,RemoteTCPDown,RemoteUDPUp,RemoteUDPDown" >"$TEST_FILE"
fi fi
while [ "$COUNTER" -lt "$NUM_TESTS" ]; do while [ "$COUNTER" -lt "$NUM_TESTS" ]; do
COUNTER=$((COUNTER + 1)) COUNTER=$((COUNTER + 1))
echo "Executing test $COUNTER of $NUM_TESTS..." echo "Executing test $COUNTER of $NUM_TESTS..."
for ((i = 1; i <= NUM_SAMPLES; i++)); do
for ((i=1; i<=NUM_SAMPLES; i++)); do
echo " Gathering sample $i of $NUM_SAMPLES..." echo " Gathering sample $i of $NUM_SAMPLES..."
START_TIME=$(date -Iseconds) START_TIME=$(date -Iseconds)
@ -118,7 +120,7 @@ while [ "$COUNTER" -lt "$NUM_TESTS" ]; do
bssid_and_bitrate=$(iw dev $INTERFACE link | awk '/Connected/ {bssid=$3} /tx bitrate/ {tx=$3} /rx bitrate/ {rx=$3} END {print bssid","tx","rx}') 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_results="" speed_results=""
for ((retry=1; retry<=MAX_RETRIES; retry++)); do for ((retry = 1; retry <= MAX_RETRIES; retry++)); do
echo " Attempting speed test (try $retry)..." echo " Attempting speed test (try $retry)..."
speed_results=$(speedtest --secure --csv 2>/dev/null) speed_results=$(speedtest --secure --csv 2>/dev/null)
@ -135,12 +137,11 @@ while [ "$COUNTER" -lt "$NUM_TESTS" ]; do
echo " [!] Speedtest permanently failed at $TIMESTAMP. Skipping sample $i of test $COUNTER." echo " [!] Speedtest permanently failed at $TIMESTAMP. Skipping sample $i of test $COUNTER."
# Optionally log failure to a sidecar file for later nerd rage # Optionally log failure to a sidecar file for later nerd rage
echo "$TIMESTAMP,Test $COUNTER,Sample $i" >> "${TEST_FILE%.csv}-failures.log" echo "$TIMESTAMP,Test $COUNTER,Sample $i" >>"${TEST_FILE%.csv}-failures.log"
continue # Skip this sample continue # Skip this sample
fi fi
# TX failure delta # TX failure delta
FAILED_NOW=$(get_tx_failed) FAILED_NOW=$(get_tx_failed)
FAILED_DELTA=$((FAILED_NOW - FAILED_START)) FAILED_DELTA=$((FAILED_NOW - FAILED_START))
@ -168,13 +169,13 @@ while [ "$COUNTER" -lt "$NUM_TESTS" ]; do
END_TIME=$(date -Iseconds) END_TIME=$(date -Iseconds)
# Log everything # Log everything
echo "$START_TIME,$END_TIME,$link_level_noise,$bssid_and_bitrate,$speed_results,$FAILED_DELTA,$channel,$freq,$packet_loss,$jitter,$LocalTCPUp,$LocalTCPDown,$LocalUDPUp,$LocalUDPDown,$RemoteTCPUp,$RemoteTCPDown,$RemoteUDPUp,$RemoteUDPDown" >> "$TEST_FILE" echo "$START_TIME,$END_TIME,$link_level_noise,$bssid_and_bitrate,$speed_results,$FAILED_DELTA,$channel,$freq,$packet_loss,$jitter,$LocalTCPUp,$LocalTCPDown,$LocalUDPUp,$LocalUDPDown,$RemoteTCPUp,$RemoteTCPDown,$RemoteUDPUp,$RemoteUDPDown" >>"$TEST_FILE"
if [ "$COUNTER" -lt "$NUM_TESTS" ]; then if [ "$COUNTER" -lt "$NUM_TESTS" ]; then
echo "Dozing off for $TIME_BETWEEN..." echo "Dozing off for $TIME_BETWEEN..."
sleep $TIME_BETWEEN sleep $TIME_BETWEEN
fi fi
done done
done done
echo "Stopping kismet..." echo "Stopping kismet..."