Last active
January 30, 2023 00:14
-
-
Save tetsuharu-kono/ce5f347c4ccaa7750b90001c7a76349a to your computer and use it in GitHub Desktop.
Wi-Fi site survey script for macOS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/zsh | |
# Wi-Fi Site Survey | |
# 2020.07.22 Rev. 0.1 | |
# First release | |
# 2023.01.30 Rev. 0.2 | |
# Fixed channel and BSSID not being retrieved by Monterey | |
# Fail on unset variables and command errors | |
set -ue -o pipefail | |
# Prevent commands misbehaving due to locale differences | |
export LC_ALL=C | |
# Signal Strength | |
S_MIN=100 | |
S_MAX=-100 | |
S_SUM=0 | |
# Noise | |
N_MIN=100 | |
N_MAX=-100 | |
N_SUM=0 | |
# Signal-to-Noise Ratio | |
SNR_MIN=100 | |
SNR_MAX=-100 | |
SNR_SUM=0 | |
NUM=0 | |
while true; do | |
# 2023.01.30 Monterey now requires sudo to get BSSID | |
wireless_info=(`(IFS=$'\n';sudo /System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport -I | tr -d ' ')`) | |
NUM=$((NUM+1)) | |
# 必要な情報を抽出 | |
# 2023.01.30 Channel information changed to 17th element in array in Monterey | |
S="${wireless_info[1]##*:}" | |
N="${wireless_info[3]##*:}" | |
AUTH="${wireless_info[11]#*:}" | |
BSSID="${wireless_info[12]#*:}" | |
SSID="${wireless_info[13]##*:}" | |
CH="${wireless_info[17]##*:}" | |
# Signal Strength min | |
if [ ${S} -lt ${S_MIN} ]; then | |
S_MIN=${S} | |
fi | |
# Signal Strength max | |
if [ ${S} -gt ${S_MAX} ]; then | |
S_MAX=${S} | |
fi | |
# Signal Strength avg | |
S_SUM=$((S_SUM+S)) | |
S_AVG=$((S_SUM/NUM)) | |
# Noise min | |
if [ ${N} -lt ${N_MIN} ]; then | |
N_MIN=${N} | |
fi | |
# Noise max | |
if [ ${N} -gt ${N_MAX} ]; then | |
N_MAX=${N} | |
fi | |
# Noise avg | |
N_SUM=$((N_SUM+N)) | |
N_AVG=$((N_SUM/NUM)) | |
# SNR min | |
SNR=$((S-N)) | |
if [ ${SNR} -lt ${SNR_MIN} ]; then | |
SNR_MIN=${SNR} | |
fi | |
# SNR max | |
if [ ${SNR} -gt ${SNR_MAX} ]; then | |
SNR_MAX=${SNR} | |
fi | |
# SNR avg | |
SNR_SUM=$((SNR_SUM+SNR)) | |
SNR_AVG=$((SNR_SUM/NUM)) | |
# ASCIIグラフ(50桁) | |
LENGTH_S=$(((100+S)/2)); | |
LENGTH_N=$(((100+N)/2)); | |
LENGTH_SNR=$((SNR/2)); | |
GRAPH_S=$(printf "%-50s" `printf %${LENGTH_S}s | sed "s/ /\|/g"`) | |
GRAPH_N=$(printf "%-50s" `printf %${LENGTH_N}s | sed "s/ /\|/g"`) | |
GRAPH_SNR=$(printf "%-50s" `printf %${LENGTH_SNR}s | sed "s/ /\|/g"`) | |
# Signal Strength ASCIIグラフカラー | |
if [ $S -gt -40 ]; then | |
# Excellent - blue | |
GRAPH_S="\e[34m${GRAPH_S}\e[m" | |
elif [ $S -gt -50 ]; then | |
# Good - green | |
GRAPH_S="\e[32m${GRAPH_S}\e[m" | |
elif [ $S -gt -60 ]; then | |
# Fair - yellow | |
GRAPH_S="\e[33m${GRAPH_S}\e[m" | |
elif [ $S -gt -70 ]; then | |
# Poor - orange | |
GRAPH_S="\033[38;5;202m${GRAPH_S}\033[m" | |
else | |
# Bad - red | |
GRAPH_S="\e[31m${GRAPH_S}\e[m" | |
fi | |
# Signal-to-Noise Ratio ASCIIグラフカラー | |
if [ $SNR -ge 30 ]; then | |
# Excellent - blue | |
GRAPH_SNR="\e[34m${GRAPH_SNR}\e[m" | |
elif [ $SNR -ge 25 ]; then | |
# Good - green | |
GRAPH_SNR="\e[32m${GRAPH_SNR}\e[m" | |
elif [ $SNR -ge 20 ]; then | |
# Fair - yellow | |
GRAPH_SNR="\e[33m${GRAPH_SNR}\e[m" | |
elif [ $SNR -ge 15 ]; then | |
# Poor - orange | |
GRAPH_SNR="\033[38;5;202m${GRAPH_SNR}\033[m" | |
else | |
# Bad - red | |
# GRAPH_SNR="\e[31m${GRAPH_SNR}\e[m" | |
GRAPH_SNR="\033[38;5;160m${GRAPH_SNR}\033[m" | |
fi | |
# 画面表示 | |
clear | |
echo "Wi-Fi Site Survey(elapsed time: ${NUM} sec)" | |
echo -e "SSID: ${SSID}(${AUTH:u}/ch=${CH}MHz/${BSSID})" | |
echo -e " S: ${GRAPH_S} ${S} dBm (min/avg/max = ${S_MIN}/${S_AVG}/${S_MAX} dBm)" | |
echo -e " N: ${GRAPH_N} ${N} dBm (min/avg/max = ${N_MIN}/${N_AVG}/${N_MAX} dBm)" | |
echo -e " SNR: ${GRAPH_SNR} ${SNR} dB (min/avg/max = ${SNR_MIN}/ ${SNR_AVG}/ ${SNR_MAX} dB)" | |
sleep 1 | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment