diff --git a/Linux/README.md b/Linux/README.md index 0f58c7d..e46b80c 100644 --- a/Linux/README.md +++ b/Linux/README.md @@ -1,6 +1,18 @@ # Speedtest_logger -A basic script that uses Speedtest-cli and logs the result to a csv file +A basic script that uses Speedtest-cli and logs the result to a csv file. -### Dependencies -Speedtest CLI: https://www.speedtest.net/apps/cli \ No newline at end of file +Within this folder there is 2 different versions of the speedtest script. + + +## speedtest-cli +This script uses the speedtest-cli test and requires the following dependencies. +#### Dependencies +Speedtest CLI: https://www.speedtest.net/apps/cli + + +## iperf3 +This script uses the iperf3 software and required a iperf3 server ip to be used to test. This version requires the following dependencies. +#### Dependencies +iperf3: https://iperf.fr/iperf-download.php +jq (lightweight and flexible command-line JSON processor): https://github.com/jqlang/jq diff --git a/Linux/iPerf/speedtest.csv b/Linux/iPerf/speedtest.csv new file mode 100644 index 0000000..c242fc1 --- /dev/null +++ b/Linux/iPerf/speedtest.csv @@ -0,0 +1 @@ +Date,ISP,IP,Download,Upload diff --git a/Linux/iPerf/speedtest.sh b/Linux/iPerf/speedtest.sh new file mode 100755 index 0000000..ef7ec58 --- /dev/null +++ b/Linux/iPerf/speedtest.sh @@ -0,0 +1,124 @@ +#!/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 file" + +# 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="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 "Variable is set to 1. Running command A." + + # 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 "Skipping log to file" + +# Variable is not set to 1 or 0 +else + echo "Variable is not set to a valid value." +fi + +if [ "$discord" -eq 1 ]; then + echo "Variable is set to 1." + + # Prepare the payload for the Discord webhook + curl -H "Content-Type: application/json" -X POST --data @- "$WEBHOOK_URL" <