2023-05-07 20:23:09 +00:00
|
|
|
#!/bin/bash
|
|
|
|
|
|
2023-07-22 18:09:13 +00:00
|
|
|
# Settings
|
2023-07-06 19:10:58 +00:00
|
|
|
# Set WAN Number
|
2023-07-22 18:09:13 +00:00
|
|
|
WAN=SET_NUMBER
|
2023-07-06 19:10:58 +00:00
|
|
|
|
|
|
|
|
# Do you want the script to log to a CSV file? Set log variable to 1 for yes, 0 for no
|
|
|
|
|
log=SET_NUMBER
|
|
|
|
|
|
2023-05-07 20:23:09 +00:00
|
|
|
# Set the path to the log file
|
2023-07-22 18:41:32 +00:00
|
|
|
LOG_FILE="./speedtest.csv"
|
2023-07-06 19:10:58 +00:00
|
|
|
|
|
|
|
|
# Do you want the script to output to a Discord webhook? Set discord variable to 1 for yes, 0 for no
|
|
|
|
|
discord=SET_NUMBER
|
2023-05-07 20:23:09 +00:00
|
|
|
|
2023-05-21 21:03:43 +00:00
|
|
|
# Set your Discord webhook URL
|
2023-07-22 18:41:32 +00:00
|
|
|
WEBHOOK_URL="https://DISCORD_WEBHOOK_URL"
|
2023-07-06 19:10:58 +00:00
|
|
|
|
2023-07-22 18:09:13 +00:00
|
|
|
# Set the iPerf3 server IP that you wantr to use
|
|
|
|
|
IPERF3_SERVER_IP=SERVER.IP.ADDRESS.HERE
|
|
|
|
|
|
|
|
|
|
# The script that will run
|
2023-07-06 19:10:58 +00:00
|
|
|
# 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
|
2023-07-22 18:09:13 +00:00
|
|
|
ORG=$(echo "$INFO" | grep -oP '(?<="organization": ")[^"]+')
|
2023-06-18 20:26:30 +00:00
|
|
|
|
2023-07-22 18:09:13 +00:00
|
|
|
# Run iperf3 upload and parse the output
|
|
|
|
|
IPERF_UP=$(iperf3 -R -c "$IPERF3_SERVER_IP" -J)
|
2023-05-07 20:23:09 +00:00
|
|
|
|
2023-07-22 18:09:13 +00:00
|
|
|
# Check if the iperf3 output is empty or contains errors
|
|
|
|
|
if [ -z "$IPERF_UP" ] || [[ "$IPERF_UP" == *"error"* ]]; then
|
|
|
|
|
echo "Iperf3 test failed: $IPERF_UP"
|
2023-05-07 20:23:09 +00:00
|
|
|
exit 1
|
|
|
|
|
fi
|
|
|
|
|
|
2023-07-22 18:09:13 +00:00
|
|
|
# Run iperf3 and parse the output
|
|
|
|
|
IPERF_DOWN=$(iperf3 -c "$IPERF3_SERVER_IP" -J)
|
2023-05-07 20:23:09 +00:00
|
|
|
|
2023-07-22 18:09:13 +00:00
|
|
|
# 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"
|
2023-05-07 20:23:09 +00:00
|
|
|
exit 1
|
|
|
|
|
fi
|
2023-07-22 18:09:13 +00:00
|
|
|
# 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')
|
2023-05-07 20:23:09 +00:00
|
|
|
|
|
|
|
|
# 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) }")
|
|
|
|
|
|
2023-07-22 18:09:13 +00:00
|
|
|
# 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
|
|
|
|
|
|
2023-07-06 19:10:58 +00:00
|
|
|
if [ "$log" -eq 1 ]; then
|
2023-07-22 18:41:32 +00:00
|
|
|
echo "Log Variable is set to 1. Writing to log."
|
2023-07-06 19:10:58 +00:00
|
|
|
|
|
|
|
|
# Append the result to the log file
|
2023-07-22 18:09:13 +00:00
|
|
|
echo "$(date +"%Y-%m-%d %H:%M:%S"),$ORG,$IP,$DOWNLOAD_Mbps Mbps,$UPLOAD_Mbps Mbps" >> "$LOG_FILE"
|
2023-05-07 20:23:09 +00:00
|
|
|
|
2023-07-06 19:10:58 +00:00
|
|
|
# Check if the variable is set to 0
|
|
|
|
|
elif [ "$log" -eq 0 ]; then
|
2023-07-22 18:41:32 +00:00
|
|
|
echo "Log Variable is set to 0. Skipping log to file"
|
2023-07-06 19:10:58 +00:00
|
|
|
|
|
|
|
|
# Variable is not set to 1 or 0
|
|
|
|
|
else
|
2023-07-22 18:41:32 +00:00
|
|
|
echo "Discord Variable is not set to a valid value."
|
2023-07-06 19:10:58 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
if [ "$discord" -eq 1 ]; then
|
2023-07-22 18:41:32 +00:00
|
|
|
echo "Discord Variable is set to 1. Sending to Discord"
|
2023-07-06 19:10:58 +00:00
|
|
|
|
|
|
|
|
# Prepare the payload for the Discord webhook
|
2023-07-22 18:09:13 +00:00
|
|
|
curl -H "Content-Type: application/json" -X POST --data @- "$WEBHOOK_URL" <<EOF
|
2023-07-06 19:10:58 +00:00
|
|
|
{
|
|
|
|
|
"content": null,
|
|
|
|
|
"embeds": [
|
|
|
|
|
{
|
2023-07-22 18:09:13 +00:00
|
|
|
"title": "Iperf3 Result - WAN $WAN",
|
2023-07-06 19:10:58 +00:00
|
|
|
"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
|
2023-07-22 18:41:32 +00:00
|
|
|
echo "Discord Variable is set to 0. Skipping send to Discord"
|
2023-07-06 19:10:58 +00:00
|
|
|
|
|
|
|
|
# Variable is not set to 1 or 0
|
|
|
|
|
else
|
|
|
|
|
echo "Variable is not set to a valid value."
|
|
|
|
|
fi
|