#!/usr/bin/env bash # This script performs speedtests over Wireguard and native connections and prints their output set -euo pipefail usage() { cat <<-EOF Usage: speedtest-compare [OPTIONS] Compare network speed between Wireguard and native connections. OPTIONS: -s, --server ID Specify server ID for native test (default: 17170) -u, --upload Include upload speed test -h, --help Display this help message EOF } # Parse options UPLOAD_FLAG="--no-upload" SERVER_ID="17170" while [[ $# -gt 0 ]]; do case "$1" in -s|--server) shift; SERVER_ID="$1"; shift ;; -u|--upload) UPLOAD_FLAG=""; shift ;; -h|--help) usage; exit 0 ;; *) echo "[ERROR] Unknown option: $1" >&2 usage exit 1 ;; esac done # Check if speedtest-cli is installed if ! command -v speedtest-cli &>/dev/null; then echo "[ERROR] speedtest-cli is not installed. Please install it first." >&2 exit 1 fi run_test() { local output pingBps pingPart bpsPart pingInt bpsInt mbpsInt # Run speedtest-cli and extract the 7th and 8th CSV fields if ! output=$(speedtest-cli $UPLOAD_FLAG --csv "$@" 2>/dev/null); then echo "[ERROR] Speedtest failed" >&2 return 1 fi pingBps=$(echo "$output" | cut -d"," -f7-8) # Extract ping value (as an integer) and bps (and convert to Mbps) pingPart="${pingBps%,*}" bpsPart="${pingBps#*,}" pingInt="${pingPart%.*}" bpsInt="${bpsPart%.*}" mbpsInt=$(( bpsInt / 1000000 )) echo "$pingInt $mbpsInt" } echo "[INFO] Running speedtest comparison..." echo "" # Test Wireguard using automatic server selection echo "Testing Wireguard connection..." if output=$(run_test); then read -r pingInt mbpsInt <<< "$output" echo " Ping: ${pingInt}ms" echo " Speed: ${mbpsInt}Mbps" else echo " [ERROR] Wireguard test failed" fi echo "" # Test native connection to ISP echo "Testing native connection (server: $SERVER_ID)..." if output=$(run_test --server "$SERVER_ID"); then read -r pingInt mbpsInt <<< "$output" echo " Ping: ${pingInt}ms" echo " Speed: ${mbpsInt}Mbps" else echo " [ERROR] Native test failed" fi