Updated Windows script to a converted version of the Linux version. Some programs such as curl and influx will need installing on Windows.

This commit is contained in:
Phil 2023-09-12 21:56:43 +01:00
parent 923a3e2ef1
commit ea5494c700

View File

@ -1,46 +1,225 @@
@echo off @echo off
setlocal enabledelayedexpansion
REM Set the path to the log file :: Settings
set LOG_FILE=./speedtest.csv :: 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 set WEBHOOK_URL=DISCORD_WEBHOOK_URL
REM Set WAN Number :: Get Current IP Info
set WAN=NUMBER :: Get IP info
for /f "delims=" %%i in ('curl -s https://ipv4.am.i.mullvad.net/json') do (
REM Run the speedtest and parse the output set INFO=%%i
for /f "tokens=6,7,8 delims=," %%A in ('speedtest-cli --csv') do ( )
set PING=%%A :: Get Reported IP
set DOWNLOAD=%%B for /f "tokens=2 delims=:" %%i in ('echo !INFO! ^| find "ip"') do (
set UPLOAD=%%C 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 :: IPERF3 Test Script
if "%PING%"=="" ( :: Do you want to use Iperf3?
echo Speedtest failed: Empty output 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 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.
)
)
:: 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" ( if "%PING%" == "ERROR" (
echo Speedtest failed: Error occurred echo Speedtest failed: %PING%
exit /b 1 exit /b 1
) )
REM Check if the required fields are missing or invalid :: Check if the required fields are missing or invalid
if "%PING%"=="" ( if "%PING%"=="" (
echo Missing or invalid speedtest fields: PING=%PING%, DOWNLOAD=%DOWNLOAD%, UPLOAD=%UPLOAD% echo Missing or invalid speedtest fields: PING=%PING%, DOWNLOAD=%DOWNLOAD%, UPLOAD=%UPLOAD%
exit /b 1 exit /b 1
) )
REM Convert the download and upload speeds to Mbps :: Convert the download and upload speeds to Mbps
set /a DOWNLOAD_Mbps=DOWNLOAD / (1024*1024) set /a DOWNLOAD=%DOWNLOAD% / (1024*1024)
set /a UPLOAD_Mbps=UPLOAD / (1024*1024) set /a UPLOAD=%UPLOAD% / (1024*1024)
REM Append the result to the log file if %log% == 1 (
echo %date:~0,10% %time:~0,8%,%PING%,%DOWNLOAD_Mbps% Mbps,%UPLOAD_Mbps% Mbps >> "%LOG_FILE%" echo Variable is set to 1. Running command A.
REM Prepare the payload for the Discord webhook :: Append the result to the log file
set PAYLOAD={"content": "Speedtest Result:\nPing: %PING%\nDownload: %DOWNLOAD_Mbps% Mbps\nUpload: %UPLOAD_Mbps% Mbps"} 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 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%