Skip to content

Instantly share code, notes, and snippets.

@moinsam
Created July 9, 2024 04:40
Show Gist options
  • Save moinsam/bba59cd4526e55be48af5c01926bfe88 to your computer and use it in GitHub Desktop.
Save moinsam/bba59cd4526e55be48af5c01926bfe88 to your computer and use it in GitHub Desktop.
speedtest_report.sh
#!/bin/bash
# Define log file
LOGFILE="$HOME/speedtest_report.log"
# Ensure the speedtest-cli Python module is installed
pip3 install speedtest-cli --user
# Get the public IP address
PUBLIC_IP=$(curl -s https://api.ipify.org)
# Get the network name (SSID)
if [ "$(uname)" == "Darwin" ]; then
# macOS
SSID=$(networksetup -getairportnetwork en0 | awk -F': ' '{print $2}')
elif [ "$(expr substr $(uname -s) 1 5)" == "Linux" ]; then
# Linux
SSID=$(iwgetid -r)
fi
# Create a Python wrapper script to suppress warnings
cat <<EOF > /tmp/speedtest_no_warning.py
import warnings
warnings.filterwarnings("ignore", category=DeprecationWarning)
import speedtest
s = speedtest.Speedtest()
s.get_best_server()
s.download()
s.upload()
results = s.results.dict()
print(f"Ping: {results['ping']} ms")
print(f"Download: {results['download'] / 1_000_000} Mbps")
print(f"Upload: {results['upload'] / 1_000_000} Mbps")
EOF
# Run the speed test and capture the output
SPEEDTEST_OUTPUT=$(python3 /tmp/speedtest_no_warning.py)
# Extract values
TIMESTAMP=$(date +"%Y-%m-%d %H:%M:%S")
PING=$(echo "$SPEEDTEST_OUTPUT" | grep "Ping" | awk '{print $2}')
DOWNLOAD=$(echo "$SPEEDTEST_OUTPUT" | grep "Download" | awk '{print $2}')
UPLOAD=$(echo "$SPEEDTEST_OUTPUT" | grep "Upload" | awk '{print $2}')
# Write to log file
echo "$TIMESTAMP, Public IP: $PUBLIC_IP, SSID: $SSID, Ping: $PING ms, Download: $DOWNLOAD Mbps, Upload: $UPLOAD Mbps" >> $LOGFILE
# Notify user
echo "Speed test completed and results logged in $LOGFILE"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment