Updated the linux version so the config settings are in their own config.env file, seperate to the speedtest file

This commit is contained in:
Phil 2025-09-07 21:15:35 +01:00
parent 5154d89f77
commit ce245b7d30
2 changed files with 103 additions and 281 deletions

23
Linux/config.env Normal file
View File

@ -0,0 +1,23 @@
# ---------------- WAN Number ----------------
WAN="SET WAN NUMBER"
# ---------------- Log File Settings ----------------
log=1
LOG_FILE="./speedtest.csv"
# ---------------- Speedtest-cli Settings ----------------
SPEEDTESTCLI=0 # set to 1 to enable
# ---------------- Iperf3 Settings ----------------
IPERF3=0 # set to 1 to enable
IPERF3_SERVER_IP="IPERF3.SERVER.IP.or.HOSTNAME"
# ---------------- InfluxDB Settings ----------------
INFLUX=0 # set to 1 to enable
INFLUXDB_NAME="SET_DB_NAME"
INFLUXDB_HOST="SET_HOSTNAME_OR_IP"
# ---------------- Discord Settings ----------------
DISCORD=0 # set to 1 to enable
WEBHOOK_URL="DISCORD_WEBHOOK_URL"

View File

@ -1,306 +1,105 @@
#!/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"
# Load environment variables
source "$(dirname "$0")/config.env"
# # --------------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.IP.or.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
# ---------------- Get Current IP Info ----------------
INFO=$(curl -s https://ipv4.am.i.mullvad.net/json | jq)
# Get Reported IP
IP=$(echo "$INFO" | jq -r '.ip')
# Get Reported ISP
ORG=$(echo "$INFO" | jq -r '.organization')
# # --------------IPERF3 Test Script--------------
# Do you want to use Iperf3?
# ---------------- Iperf3 Test ----------------
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" <<EOF
{
"content": null,
"embeds": [
{
"title": "Iperf3 Result - WAN $WAN",
"color": 5814783,
"fields": [
{
"name": "Date and Time of Test:",
"value": "$(date +'%Y-%m-%d %H:%M:%S')"
},
{
"name": "Iperf3 Server Used:",
"value": "$IPERF3_SERVER_IP"
},
{
"name": "ISP Tested:",
"value": "$ORG"
},
{
"name": "IP Tested:",
"value": "$IP"
},
{
"name": "Download Test Result:",
"value": "$DOWNLOAD_Mbps mbps"
},
{
"name": "Upload Test Result:",
"value": "$UPLOAD_Mbps mbps"
}
]
}
],
"attachments": []
}
EOF
# Check if the variable is set to 0
elif [ "$DISCORD" -eq 0 ]; then
echo "DISCORD Variable is set to 0. Skipping send to DISCORD"
# Variable is not set to 1 or 0
else
echo "DISCORD Variable is not set to a valid value."
echo "Running iperf3 test..."
IPERF_UP=$(iperf3 -R -c "$IPERF3_SERVER_IP" -J)
if [ -z "$IPERF_UP" ] || [[ "$IPERF_UP" == *"error"* ]]; then
echo "Iperf3 test failed"
exit 1
fi
IPERF_DOWN=$(iperf3 -c "$IPERF3_SERVER_IP" -J)
if [ -z "$IPERF_DOWN" ] || [[ "$IPERF_DOWN" == *"error"* ]]; then
echo "Iperf3 test failed"
exit 1
fi
DOWNLOAD=$(echo "$IPERF_UP" | jq -r '.end.sum_received.bits_per_second')
UPLOAD=$(echo "$IPERF_DOWN" | jq -r '.end.sum_sent.bits_per_second')
# Do you want the script to output to a INFLUX DB?
if [ "$INFLUX" -eq 1 ]; then
echo "INFLUX Variable is set to 1."
DOWNLOAD_Mbps=$(awk "BEGIN { printf \"%.1f\", $DOWNLOAD / (1024*1024) }")
UPLOAD_Mbps=$(awk "BEGIN { printf \"%.1f\", $UPLOAD / (1024*1024) }")
# Send Data to DB
influx --database='INFLUXDB_NAME' --host='INFLUXDB_HOST' --execute="INSERT 'INFLUXDB_NAME',wan=$WAN,test_type=,iperf3,isp=$ORG,ip=$IP download=$DOWNLOAD_Mbps,upload=$UPLOAD_Mbps"
if [ "$log" -eq 1 ]; then
echo "$(date +"%Y-%m-%d %H:%M:%S"),$WAN,iperf3,$ORG,$IP,$DOWNLOAD_Mbps Mbps,$UPLOAD_Mbps Mbps" >> "$LOG_FILE"
fi
# Check if INFLUX is set to 0
elif [ "$INFLUX" -eq 0 ]; then
echo "Skipping send to INFLUX"
# Variable for INFLUX is not set to 1 or 0
else
echo "Variable for INFLUX is not set to a valid value."
fi
fi
# # --------------SPEEDTEST-CLI Test Script--------------
# Do you want to use Speedtest-cli?
if [ "$SPEEDTESTCLI" -eq 1 ]; then
echo "SPEEDTESTCLI Variable is set to 1. Running SPEEDTEST-CLI test"
# Run the speedtest and parse the output
SPEEDTEST=$(speedtest-cli --csv)
# Check if the output is empty or contains errors
if [ -z "$SPEEDTEST" ] || [[ "$SPEEDTEST" == *"ERROR"* ]]; then
echo "Speedtest failed: $SPEEDTEST"
exit 1
fi
PING=$(echo "$SPEEDTEST" | awk -F',' '{print $6}')
DOWNLOAD=$(echo "$SPEEDTEST" | awk -F',' '{print $7}')
UPLOAD=$(echo "$SPEEDTEST" | awk -F',' '{print $8}')
# Check if the required fields are missing or invalid
if [ -z "$PING" ] || [ -z "$DOWNLOAD" ] || [ -z "$UPLOAD" ]; then
echo "Missing or invalid speedtest fields: PING=$PING, DOWNLOAD=$DOWNLOAD, UPLOAD=$UPLOAD"
exit 1
fi
# 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) }")
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"),$WAN,Speedtest.net,$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 "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" <<EOF
if [ "$DISCORD" -eq 1 ]; then
curl -H "Content-Type: application/json" -X POST --data @- "$WEBHOOK_URL" <<EOF
{
"content": null,
"embeds": [
{
"title": "Speedtest.net Result - WAN $WAN",
"color": 5814783,
"fields": [
{
"name": "Date and Time of Test:",
"value": "$(date +'%Y-%m-%d %H:%M:%S')"
},
{
"name": "ISP Tested:",
"value": "$ORG"
},
{
"name": "IP Tested:",
"value": "$IP"
},
{
"name": "Ping:",
"value": "$PING ms"
},
{
"name": "Download Test Result:",
"value": "$DOWNLOAD_Mbps mbps"
},
{
"name": "Upload Test Result:",
"value": "$UPLOAD_Mbps mbps"
}
]
}
],
"attachments": []
"embeds": [{
"title": "Iperf3 Result - WAN $WAN",
"color": 5814783,
"fields": [
{"name": "Date", "value": "$(date +'%Y-%m-%d %H:%M:%S')"},
{"name": "Server", "value": "$IPERF3_SERVER_IP"},
{"name": "ISP", "value": "$ORG"},
{"name": "IP", "value": "$IP"},
{"name": "Download", "value": "$DOWNLOAD_Mbps Mbps"},
{"name": "Upload", "value": "$UPLOAD_Mbps Mbps"}
]
}]
}
EOF
fi
# 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 "DIRCORD Variable is not set to a valid value."
if [ "$INFLUX" -eq 1 ]; then
influx --database="$INFLUXDB_NAME" --host="$INFLUXDB_HOST" \
--execute="INSERT $INFLUXDB_NAME,wan=$WAN,test_type=iperf3,isp=$ORG,ip=$IP download=$DOWNLOAD_Mbps,upload=$UPLOAD_Mbps"
fi
fi
# Do you want the script to output to a INFLUX DB?
if [ "$INFLUX" -eq 1 ]; then
echo "INFLUX Variable is set to 1."
# ---------------- Speedtest-cli Test ----------------
if [ "$SPEEDTESTCLI" -eq 1 ]; then
echo "Running speedtest-cli..."
SPEEDTEST=$(speedtest-cli --csv)
if [ -z "$SPEEDTEST" ] || [[ "$SPEEDTEST" == *"ERROR"* ]]; then
echo "Speedtest failed"
exit 1
fi
# Send Data to DB
influx --database='INFLUXDB_NAME' --host='INFLUXDB_HOST' --execute="INSERT 'INFLUXDB_NAME',wan=$WAN,test_type=,speedtest.net,isp=$ORG,ip=$IP download=$DOWNLOAD_Mbps,upload=$UPLOAD_Mbps"
PING=$(echo "$SPEEDTEST" | awk -F',' '{print $6}')
DOWNLOAD=$(echo "$SPEEDTEST" | awk -F',' '{print $7}')
UPLOAD=$(echo "$SPEEDTEST" | awk -F',' '{print $8}')
# Check if INFLUX is set to 0
elif [ "$INFLUX" -eq 0 ]; then
echo "Skipping send to INFLUX"
DOWNLOAD_Mbps=$(awk "BEGIN { printf \"%.1f\", $DOWNLOAD / (1024*1024) }")
UPLOAD_Mbps=$(awk "BEGIN { printf \"%.1f\", $UPLOAD / (1024*1024) }")
# Variable for INFLUX is not set to 1 or 0
else
echo "Variable for INFLUX is not set to a valid value."
if [ "$log" -eq 1 ]; then
echo "$(date +"%Y-%m-%d %H:%M:%S"),$WAN,Speedtest.net,$ORG,$IP,$DOWNLOAD_Mbps Mbps,$UPLOAD_Mbps Mbps" >> "$LOG_FILE"
fi
if [ "$DISCORD" -eq 1 ]; then
curl -H "Content-Type: application/json" -X POST --data @- "$WEBHOOK_URL" <<EOF
{
"embeds": [{
"title": "Speedtest.net Result - WAN $WAN",
"color": 5814783,
"fields": [
{"name": "Date", "value": "$(date +'%Y-%m-%d %H:%M:%S')"},
{"name": "ISP", "value": "$ORG"},
{"name": "IP", "value": "$IP"},
{"name": "Ping", "value": "$PING ms"},
{"name": "Download", "value": "$DOWNLOAD_Mbps Mbps"},
{"name": "Upload", "value": "$UPLOAD_Mbps Mbps"}
]
}]
}
EOF
fi
if [ "$INFLUX" -eq 1 ]; then
influx --database="$INFLUXDB_NAME" --host="$INFLUXDB_HOST" \
--execute="INSERT $INFLUXDB_NAME,wan=$WAN,test_type=speedtest.net,isp=$ORG,ip=$IP download=$DOWNLOAD_Mbps,upload=$UPLOAD_Mbps"
fi
fi
fi