Speedtest_logger/Windows/speedtest.bat

226 lines
6.9 KiB
Batchfile
Executable File

@echo off
setlocal enabledelayedexpansion
:: Settings
:: Set WAN Number
set WAN=SET WAN NUMBER
:: 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
:: 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:"=!
)
:: 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.
)
)
:: 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.
)
)