Updated the Linux script to give the option of using speedtest-cli or iperf3

This commit is contained in:
Phil 2023-07-22 19:09:13 +01:00
parent a0992e46a1
commit d52af1436e
5 changed files with 140 additions and 3 deletions

View File

@ -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
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

View File

@ -0,0 +1 @@
Date,ISP,IP,Download,Upload
1 Date ISP IP Download Upload

124
Linux/iPerf/speedtest.sh Executable file
View File

@ -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" <<EOF
{
"content": null,
"embeds": [
{
"title": "Iperf3 Result - WAN $WAN",
"color": 5814783,
"fields": [
{
"name": "Time:",
"value": "$(date +'%Y-%m-%d %H:%M:%S')"
},
{
"name": "ISP:",
"value": "$ORG"
},
{
"name": "IP:",
"value": "$IP"
},
{
"name": "Download:",
"value": "$DOWNLOAD_Mbps mbps"
},
{
"name": "Upload:",
"value": "$UPLOAD_Mbps mbps"
}
]
}
],
"attachments": []
}
EOF
# Check if the variable is set to 0
elif [ "$discord" -eq 0 ]; then
echo "Skipping send to Discord"
# Variable is not set to 1 or 0
else
echo "Variable is not set to a valid value."
fi