From f4028616193ef067c6f69c755a94d29970d3bef0 Mon Sep 17 00:00:00 2001 From: Phil Date: Sun, 18 Jun 2023 20:31:42 +0100 Subject: [PATCH] Created Windows script --- Linux/README.md | 3 +++ Windows/README.md | 7 +++++++ Windows/speedtest.bat | 46 +++++++++++++++++++++++++++++++++++++++++++ Windows/speedtest.sh | 41 -------------------------------------- 4 files changed, 56 insertions(+), 41 deletions(-) create mode 100644 Linux/README.md create mode 100644 Windows/README.md create mode 100755 Windows/speedtest.bat delete mode 100755 Windows/speedtest.sh diff --git a/Linux/README.md b/Linux/README.md new file mode 100644 index 0000000..c937ad2 --- /dev/null +++ b/Linux/README.md @@ -0,0 +1,3 @@ +# Speedtest_logger + +A basic script that uses Speedtest-cli and logs the result to a csv file \ No newline at end of file diff --git a/Windows/README.md b/Windows/README.md new file mode 100644 index 0000000..d828846 --- /dev/null +++ b/Windows/README.md @@ -0,0 +1,7 @@ +# Speedtest_logger + +A basic script that uses Speedtest-cli and logs the result to a csv file and post's the output speed to a Dicord Web Hook. +This script was converted from the linix version and will require curl and speedtest-cli to be installed on Windows in order for it to post to Discord. + +Speedtest CLI: https://www.speedtest.net/apps/cli +Curl: https://curl.se/windows/ \ No newline at end of file diff --git a/Windows/speedtest.bat b/Windows/speedtest.bat new file mode 100755 index 0000000..07b1c33 --- /dev/null +++ b/Windows/speedtest.bat @@ -0,0 +1,46 @@ +@echo off + +REM Set the path to the log file +set LOG_FILE=./speedtest.csv + +REM Set your Discord webhook URL +set WEBHOOK_URL=DISCORD_WEBHOOK_URL + +REM Set WAN Number +set WAN=NUMBER + +REM Run the speedtest and parse the output +for /f "tokens=6,7,8 delims=," %%A in ('speedtest-cli --csv') do ( + set PING=%%A + set DOWNLOAD=%%B + set UPLOAD=%%C +) + +REM Check if the output is empty or contains errors +if "%PING%"=="" ( + echo Speedtest failed: Empty output + exit /b 1 +) +if "%PING%"=="ERROR" ( + echo Speedtest failed: Error occurred + exit /b 1 +) + +REM Check if the required fields are missing or invalid +if "%PING%"=="" ( + echo Missing or invalid speedtest fields: PING=%PING%, DOWNLOAD=%DOWNLOAD%, UPLOAD=%UPLOAD% + exit /b 1 +) + +REM Convert the download and upload speeds to Mbps +set /a DOWNLOAD_Mbps=DOWNLOAD / (1024*1024) +set /a UPLOAD_Mbps=UPLOAD / (1024*1024) + +REM Append the result to the log file +echo %date:~0,10% %time:~0,8%,%PING%,%DOWNLOAD_Mbps% Mbps,%UPLOAD_Mbps% Mbps >> "%LOG_FILE%" + +REM Prepare the payload for the Discord webhook +set PAYLOAD={"content": "Speedtest Result:\nPing: %PING%\nDownload: %DOWNLOAD_Mbps% Mbps\nUpload: %UPLOAD_Mbps% Mbps"} + +REM Send the payload to the Discord webhook - If you don't want this, comment out this command +curl -H "Content-Type: application/json" -X POST --data "{\"content\": null, \"embeds\": [{\"title\": \"Speedtest Result - WAN %WAN%\", \"description\": \"Time: %date:~0,10% %time:~0,8%\nPing: %PING% ms\nDownload: %DOWNLOAD_Mbps% mbps\nUpload: %UPLOAD_Mbps% mbps\", \"color\": 5814783}], \"attachments\": []}" %WEBHOOK_URL% diff --git a/Windows/speedtest.sh b/Windows/speedtest.sh deleted file mode 100755 index 65a6305..0000000 --- a/Windows/speedtest.sh +++ /dev/null @@ -1,41 +0,0 @@ -#!/bin/bash - -# Set the path to the log file -LOG_FILE="./speedtest.csv" - -# Set your Discord webhook URL -WEBHOOK_URL="DISCORD_WEBHOOK_URL" - -# Set WAN Number -WAN="NUMBER" -# 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) }") - -# Append the result to the log file -echo "$(date +"%Y-%m-%d %H:%M:%S"),$PING,$DOWNLOAD_Mbps Mbps,$UPLOAD_Mbps Mbps" >> "$LOG_FILE" - -# Prepare the payload for the Discord webhook -PAYLOAD='{"content": "Speedtest Result:\nPing: '"$PING"'\nDownload: '"$DOWNLOAD_Mbps"' Mbps\nUpload: '"$UPLOAD_Mbps"' Mbps"}' - -# Send the payload to the Discord webhook - If you dont want this, comment out this command -curl -H "Content-Type: application/json" -X POST --data "{\"content\": null, \"embeds\": [{\"title\": \"Speedtest Result - WAN $WAN\", \"description\": \"Time: $(date +"%Y-%m-%d %H:%M:%S")\nPing: $PING ms\nDownload: $DOWNLOAD_Mbps mbps\nUpload: $UPLOAD_Mbps mbps\", \"color\": 5814783}], \"attachments\": []}" $WEBHOOK_URL