From ea5494c700bf15b1e82b2555cede2b761b697900 Mon Sep 17 00:00:00 2001 From: Phil Date: Tue, 12 Sep 2023 21:56:43 +0100 Subject: [PATCH] Updated Windows script to a converted version of the Linux version. Some programs such as curl and influx will need installing on Windows. --- Windows/speedtest.bat | 249 ++++++++++++++++++++++++++++++++++++------ 1 file changed, 214 insertions(+), 35 deletions(-) diff --git a/Windows/speedtest.bat b/Windows/speedtest.bat index 07b1c33..ce37e7d 100755 --- a/Windows/speedtest.bat +++ b/Windows/speedtest.bat @@ -1,46 +1,225 @@ @echo off +setlocal enabledelayedexpansion -REM Set the path to the log file -set LOG_FILE=./speedtest.csv +:: Settings +:: Set WAN Number +set WAN=SET WAN NUMBER -REM Set your Discord webhook URL +:: Log File Settings +:: Set log variable to 1 for yes, 0 for no +set log=1 +:: Set the path to the log file +set LOG_FILE=speedtest.csv + +:: Speedtest-cli Settings +:: Set log variable to 1 for yes, 0 for no +set SPEEDTESTCLI=0 + +:: Iperf3 Settings +:: Set log variable to 1 for yes, 0 for no +set IPERF3=0 +:: Set the iPerf3 server IP or Hostname +set IPERF3_SERVER_IP=IPERF3.SERVER.IPor.HOSTNAME + +:: InfluxDB Settings +:: Set log variable to 1 for yes, 0 for no +set INFLUX=0 +:: Set Influx DB Name +set INFLUXDB_NAME=SET_DB_NAME +:: Set Influx DB Hostname or IP +set INFLUXDB_HOST=SET_HOSTNAME_OR_IP + +:: Discord Settings +:: Set log variable to 1 for yes, 0 for no +set DISCORD=0 +:: 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 +:: Get Current IP Info +:: Get IP info +for /f "delims=" %%i in ('curl -s https://ipv4.am.i.mullvad.net/json') do ( + set INFO=%%i +) +:: Get Reported IP +for /f "tokens=2 delims=:" %%i in ('echo !INFO! ^| find "ip"') do ( + set IP=%%i + set IP=!IP:"=! +) +:: Get Reported ISP +for /f "tokens=2 delims=:" %%i in ('echo !INFO! ^| find "organization"') do ( + set ORG=%%i + set ORG=!ORG:"=! ) -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 +:: IPERF3 Test Script +:: Do you want to use Iperf3? +if %IPERF3% == 1 ( + echo IPERF3 Variable is set to 1. Running IPERF3 test + + :: Run iperf3 upload and parse the output + for /f "tokens=*" %%i in ('iperf3 -R -c %IPERF3_SERVER_IP% -J') do ( + set IPERF_UP=%%i + ) + + :: Check if the iperf3 output is empty or contains errors + echo !IPERF_UP! | find "error" > nul + if not errorlevel 1 ( + echo Iperf3 test failed: !IPERF_UP! + exit /b 1 + ) + + :: Run iperf3 and parse the output + for /f "tokens=*" %%i in ('iperf3 -c %IPERF3_SERVER_IP% -J') do ( + set IPERF_DOWN=%%i + ) + + :: Check if the iperf3 download output is empty or contains errors + echo !IPERF_DOWN! | find "error" > nul + if not errorlevel 1 ( + echo Iperf3 test failed: !IPERF_DOWN! + exit /b 1 + ) + + :: Extract download and upload speeds from iperf3 output + for /f "tokens=2 delims=:" %%i in ('echo !IPERF_UP! ^| find "bits_per_second"') do ( + set DOWNLOAD=%%i + set DOWNLOAD=!DOWNLOAD:"=! + ) + for /f "tokens=2 delims=:" %%i in ('echo !IPERF_DOWN! ^| find "bits_per_second"') do ( + set UPLOAD=%%i + set UPLOAD=!UPLOAD:"=! + ) + + :: Convert the download and upload speeds to Mbps + set /a DOWNLOAD=!DOWNLOAD! / (1024*1024) + set /a UPLOAD=!UPLOAD! / (1024*1024) + + :: Check if the required fields are missing or invalid + if "!DOWNLOAD!"=="" ( + echo Missing or invalid iperf3 fields: DOWNLOAD=!DOWNLOAD!, UPLOAD=!UPLOAD! + exit /b 1 + ) + + if %log% == 1 ( + echo Log Variable is set to 1. Writing to log. + + :: Append the result to the log file + echo !DATE:~0,4!-!DATE:~5,2!-!DATE:~8,2! !TIME:~0,2!:!TIME:~3,2!:!TIME:~6,2!,iperf3,!ORG!,!IP!,!DOWNLOAD! Mbps,!UPLOAD! Mbps>> "%LOG_FILE%" + ) else if %log% == 0 ( + echo Log Variable is set to 0. Skipping log to file + ) else ( + echo DISCORD Variable is not set to a valid value. + ) + + :: Do you want the script to output to a DISCORD webhook? + if %DISCORD% == 1 ( + 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 "{ + \"content\": null, + \"embeds\": [ + { + \"title\": \"Iperf3 Result - WAN %WAN%\", + \"color\": 5814783, + \"fields\": [ + { + \"name\": \"Date and Time of Test:\", + \"value\": \"!DATE:~0,4!-!DATE:~5,2!-!DATE:~8,2! !TIME:~0,2!:!TIME:~3,2!:!TIME:~6,2!\" + }, + { + \"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\" + }, + { + \"name\": \"Upload Test Result:\", + \"value\": \"!UPLOAD! mbps\" + } + ] + } + ], + \"attachments\": [] + }" -k "!WEBHOOK_URL!" + ) else if %DISCORD% == 0 ( + echo DISCORD Variable is set to 0. Skipping send to DISCORD + ) else ( + echo DISCORD Variable is not set to a valid value. + ) + + :: Do you want the script to output to an INFLUX DB? + if %INFLUX% == 1 ( + echo INFLUX Variable is set to 1. + + :: 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" + ) else if %INFLUX% == 0 ( + echo Skipping send to INFLUX + ) else ( + echo Variable for INFLUX is not set to a valid value. + ) ) -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 +:: SPEEDTEST-CLI Test Script +:: Do you want to use Speedtest-cli? +if %SPEEDTESTCLI% == 1 ( + echo SPEEDTESTCLI Variable is set to 1. Running SPEEDTEST-CLI test + + :: Run the speedtest and parse the output + for /f "tokens=1-4 delims=," %%a in ('speedtest-cli --csv') do ( + set PING=%%b + set DOWNLOAD=%%c + set UPLOAD=%%d + ) + + :: Check if the output is empty or contains errors + if "%PING%" == "ERROR" ( + echo Speedtest failed: %PING% + exit /b 1 + ) + + :: 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 + ) + + :: Convert the download and upload speeds to Mbps + set /a DOWNLOAD=%DOWNLOAD% / (1024*1024) + set /a UPLOAD=%UPLOAD% / (1024*1024) + + if %log% == 1 ( + echo Variable is set to 1. Running command A. + + :: Append the result to the log file + echo !DATE:~0,4!-!DATE:~5,2!-!DATE:~8,2! !TIME:~0,2!:!TIME:~3,2!:!TIME:~6,2!,Speedtest.net,%ORG%,%IP%,!DOWNLOAD! Mbps,!UPLOAD! Mbps >> "%LOG_FILE%" + ) else if %log% == 0 ( + echo Skipping log to file + ) else ( + echo Log to File Variable is not set to a valid value. + ) + + :: Do you want the script to output to an INFLUX DB? + if %INFLUX% == 1 ( + echo INFLUX Variable is set to 1. + + :: 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" + ) else if %INFLUX% == 0 ( + echo Skipping send to INFLUX + ) else ( + echo Variable for INFLUX is not set to a valid value. + ) ) -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%