#!/bin/bash # Settings # Set WAN Number WAN=SET_NUMBER # Do you want the script to log to a CSV file? Set log variable to 1 for yes, 0 for no log=SET_NUMBER # Set the path to the log file LOG_FILE="./speedtest.csv" # Do you want the script to output to a Discord webhook? Set discord variable to 1 for yes, 0 for no discord=SET_NUMBER # Set your Discord webhook URL WEBHOOK_URL="https://DISCORD_WEBHOOK_URL" # Set the iPerf3 server IP that you wantr to use IPERF3_SERVER_IP=SERVER.IP.ADDRESS.HERE # The script that will run # Get IP info INFO=$(curl -s https://ipv4.am.i.mullvad.net/json | python -m json.tool) # Reported IP IP=$(echo "$INFO" | grep -oE '"ip":\s*"[^"]+"' | grep -oE '[0-9.]+') # Reported ISP ORG=$(echo "$INFO" | grep -oP '(?<="organization": ")[^"]+') # Run iperf3 upload and parse the output IPERF_UP=$(iperf3 -R -c "$IPERF3_SERVER_IP" -J) # Check if the iperf3 output is empty or contains errors if [ -z "$IPERF_UP" ] || [[ "$IPERF_UP" == *"error"* ]]; then echo "Iperf3 test failed: $IPERF_UP" exit 1 fi # Run iperf3 and parse the output IPERF_DOWN=$(iperf3 -c "$IPERF3_SERVER_IP" -J) # Check if the iperf3 download output is empty or contains errors if [ -z "$IPERF_DOWN" ] || [[ "$IPERF_DOWN" == *"error"* ]]; then echo "Iperf3 test failed: $IPERF_DOWN" exit 1 fi # Extract download and upload speeds from iperf3 output DOWNLOAD=$(echo "$IPERF_UP" | jq -r '.end.sum_received.bits_per_second') UPLOAD=$(echo "$IPERF_DOWN" | jq -r '.end.sum_sent.bits_per_second') # Convert the download and upload speeds to Mbps DOWNLOAD_Mbps=$(awk "BEGIN { printf \"%.1f\", $DOWNLOAD / (1024*1024) }") UPLOAD_Mbps=$(awk "BEGIN { printf \"%.1f\", $UPLOAD / (1024*1024) }") # Check if the required fields are missing or invalid if [ -z "$DOWNLOAD_Mbps" ] || [ -z "$UPLOAD_Mbps" ]; then echo "Missing or invalid iperf3 fields: DOWNLOAD=$DOWNLOAD_Mbps, UPLOAD=$UPLOAD_Mbps" exit 1 fi if [ "$log" -eq 1 ]; then echo "Log Variable is set to 1. Writing to log." # Append the result to the log file echo "$(date +"%Y-%m-%d %H:%M:%S"),$ORG,$IP,$DOWNLOAD_Mbps Mbps,$UPLOAD_Mbps Mbps" >> "$LOG_FILE" # Check if the variable is set to 0 elif [ "$log" -eq 0 ]; then echo "Log Variable is set to 0. Skipping log to file" # Variable is not set to 1 or 0 else echo "Discord Variable is not set to a valid value." fi if [ "$discord" -eq 1 ]; then echo "Discord Variable is set to 1. Sending to Discord" # Prepare the payload for the Discord webhook curl -H "Content-Type: application/json" -X POST --data @- "$WEBHOOK_URL" <