diff --git a/Linux/config.env b/Linux/config.env new file mode 100644 index 0000000..aa49157 --- /dev/null +++ b/Linux/config.env @@ -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" + diff --git a/Linux/speedtest.sh b/Linux/speedtest.sh index 27d7130..81eaae5 100755 --- a/Linux/speedtest.sh +++ b/Linux/speedtest.sh @@ -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" <> "$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" <> "$LOG_FILE" + fi + + if [ "$DISCORD" -eq 1 ]; then + curl -H "Content-Type: application/json" -X POST --data @- "$WEBHOOK_URL" <