Skip to content

Instantly share code, notes, and snippets.

@hnsol
Last active March 31, 2024 03:54
Show Gist options
  • Save hnsol/bde3ef4c5f76790f2fe93c0d7a1a0919 to your computer and use it in GitHub Desktop.
Save hnsol/bde3ef4c5f76790f2fe93c0d7a1a0919 to your computer and use it in GitHub Desktop.
Monitor Wi-Fi speed: Automatic tests, monthly logs, and menu bar results.
#!/bin/bash
# <bitbar.title>Speedtest and Log</bitbar.title>
# <bitbar.version>v1.0</bitbar.version>
# <bitbar.author>hann-solo</bitbar.author>
# <bitbar.author.github>hnsol</bitbar.author.github>
# <bitbar.desc>Displays the last 10 lines of speedtest. Keeps a log of speedtest results.</bitbar.desc>
# <bitbar.image>http://www.hosted-somewhere/pluginimage</bitbar.image>
# <bitbar.dependencies>bash</bitbar.dependencies>
# <bitbar.abouturl>http://url-to-about.com/</bitbar.abouturl>
# 変数の設定
# logfile="/Users/masatora/Documents/MyDevelop/230100/230516_SwiftBarPlugin/230516_speedtest_swiftbar.log"
current_month=$(date '+%Y-%m')
logfile="/Users/masatora/Documents/MyDevelop/230100/230516_SwiftBarPlugin/230516_speedtest_swiftbar_${current_month}.log"
icon=":wifi.square.fill: | sfsize=16"
# Speedtestを実施して結果を変数に代入
ssid=$(system_profiler SPAirPortDataType | awk '/Current Network Information:/{getline; gsub(/^ +|:$/,""); print "SSID: " $0; exit}')
output=$(networkQuality)
uplink=$(echo "$output" | awk -F': ' '/Uplink capacity/{print $2}')
downlink=$(echo "$output" | awk -F': ' '/Downlink capacity/ {print $2}')
res=$(echo "$output" | awk -F': ' '/Responsiveness/ {print $2}')
idle=$(echo "$output" | awk -F': ' '/Idle Latency/ {print $2}')
# 現在時刻を取得して変数に代入
timestamp=$(date '+%Y-%m-%d %H:%M')
# 結果をログファイルに追記
printf "(%s)\t%s\t%s\t%s\t%s\t%s\n" "$timestamp" "$ssid" "↑ $uplink" "↓ $downlink" "$res" "$idle" >> "$logfile"
# メニューバーに表示する内容を出力
echo "$icon"
echo "---"
# 最新の10行を取得
latest_lines=$(tail -n 10 "$logfile")
# 各行の内容を必要な情報に分割して表示
while IFS= read -r line; do
timestamp=$(echo "$line" | awk -F '\t' '{print $1}')
ssid=$(echo "$line" | awk -F '\t' '{print $2}' | awk -F ' ' '{print $2}')
uplink=$(echo "$line" | awk -F '\t' '{print $3}')
downlink=$(echo "$line" | awk -F '\t' '{print $4}')
res=$(echo "$line" | awk -F '\t' '{print $5}' | awk -F ' ' '{print $1}')
idle=$(echo "$line" | awk -F '\t' '{print $6}' | awk -F ' ' '{print $1 "ms"}')
echo "$timestamp $ssid $uplink $downlink $res $idle"
done <<< "$latest_lines"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment