Files
zorruno-homeassistant/scripts/internet_speed_test.sh
T
2026-08-15 14:58:19 +12:00

20 lines
823 B
Bash
Executable File

#!/usr/bin/env bash
set -euo pipefail
download_bytes=2000000
upload_bytes=500000
download_url="https://speed.cloudflare.com/__down?bytes=${download_bytes}"
upload_url="https://speed.cloudflare.com/__up"
download_bps=$(curl --fail --silent --show-error --location --max-time 45 \
--output /dev/null --write-out "%{speed_download}" "${download_url}")
upload_bps=$(head -c "${upload_bytes}" /dev/zero | curl --fail --silent --show-error \
--location --max-time 45 --output /dev/null --request POST \
--header "Content-Type: application/octet-stream" --data-binary @- \
--write-out "%{speed_upload}" "${upload_url}")
awk -v download_bps="${download_bps}" -v upload_bps="${upload_bps}" 'BEGIN {
printf "{\"download_mbps\":%.2f,\"upload_mbps\":%.2f}\n", \
download_bps * 8 / 1000000, upload_bps * 8 / 1000000
}'