#!/bin/bash # Settings # # --------------WAN Number-------------- # Set WAN Number. This is usefull if you are testing multiple connections and having # them log to the same location WAN="SET WAN NUMBER" # # --------------Log File Settings-------------- # Do you want the script to log to a CSV file? Set log variable to 1 for yes, 0 for no # If you set 0 for no, you dont have to fill in LOG_FILE setting log=1 # Set the path to the log file LOG_FILE="./speedtest.csv" # # --------------Speedtest-cli Settings-------------- # Do you want to use Speedtest-cli? Set log variable to 1 for yes, 0 for no SPEEDTESTCLI="SET TO 0 or 1" # # --------------Iperf3 Settings-------------- # Do you want to use Iperf3? Set log variable to 1 for yes, 0 for no # If you set 0 for no, you dont have to fill in the IPERF3_SERVER_IP setting IPERF3="SET TO 0 or 1" # Set the iPerf3 server IP or Hostname that you want to use IPERF3_SERVER_IP="IPERF3.SERVER.IPor.HOSTNAME" # # --------------InflixDB Settings-------------- # Do you want the script to output to a inflix Database? Set INFLUX variable to 1 for yes, 0 for no # If you set 0 for no, you dont have to fill in the INFLUXDB_NAME or INFLUXDB_HOST settings INFLUX="SET TO 0 or 1" # Set Inflix DB Name INFLUXDB_NAME="SET_DB_NAME" #Set Influx DB Hostname or IP INFLUXDB_HOST="SET_HOSTNAME_OR_IP" # # --------------DISCORD Settings-------------- # Do you want the script to output to a DISCORD webhook? Set DISCORD variable to 1 for yes, 0 for no # If you set 0 for no, you dont have to fill in the WEBHOOK_URL setting DISCORD="SET TO 0 or 1" # Set your DISCORD webhook URL WEBHOOK_URL="DISCORD_WEBHOOK_URL" # # --------------SpeedTest Script----------------- # # --------------Get Current IP Info-------------- # Get IP info INFO=$(curl -s https://ipv4.am.i.mullvad.net/json | python -m json.tool) # Get Reported IP IP=$(echo "$INFO" | grep -oE '"ip":\s*"[^"]+"' | grep -oE '[0-9.]+') # Get Reported ISP ORG=$(echo "$INFO" | grep -oP '(?<="organization":")[^"]+') # # --------------IPERF3 Test Script-------------- # Do you want to use Iperf3? if [ "$IPERF3" -eq 1 ]; then echo "IPERF3 Variable is set to 1. Running IPERF3 test" # 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"),$WAN,iperf3,$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 # Do you want the script to output to a DISCORD webhook? 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" <> "$LOG_FILE" # Check if the variable is set to 0 elif [ "$log" -eq 0 ]; then echo "Skipping log to file" # Variable is not set to 1 or 0 else echo "Log to File Variable is not set to a valid value." fi if [ "$DISCORD" -eq 1 ]; then echo "DISCORD Variable is set to 1." # Prepare the payload for the Discord webhook curl -H "Content-Type: application/json" -X POST --data @- "$WEBHOOK_URL" <