Skip to content

Instantly share code, notes, and snippets.

@Simbaclaws
Last active January 8, 2023 00:30
Show Gist options
  • Save Simbaclaws/45684511687aab230e093ca276a57e69 to your computer and use it in GitHub Desktop.
Save Simbaclaws/45684511687aab230e093ca276a57e69 to your computer and use it in GitHub Desktop.
Quick and dirty ping interval tester for sysadmins
:: --- Turn off output in console at initialization ---
@echo off
:: --- Set the ping interval ---
set INTERVAL=30
:: --- Set the output path ---
set OUTPUTDIR=C:\PING
set OUTPUTFILE=result.txt
:: --- Internal network to ping ---
:: Start with the router
set IPROUTER=192.168.1.1
set ROUTERNAME=Router
:: Any other devices and device names in 2 Arrays
:: PiHole
set DEVICE[0]=192.168.1.10
set DEVICENAME[0]=PiHole
:: Home Assistant
set DEVICE[1]=192.168.1.20
set DEVICENAME[1]=Home Assistant
:: Gaming Desktop
set DEVICE[2]=192.168.4.10
set DEVICENAME[2]=Gaming Desktop
:: Mobile Phone
set DEVICE[3]=192.168.1.189
set DEVICENAME[3]=Mobile Phone
:: --- Online servers to ping ---
:: Any other clouds and cloud names in 2 Arrays
:: Google
set CLOUD[0]=8.8.8.8
set CLOUDNAME[0]=Google Servers
:: Cloudflare
set CLOUD[1]=1.1.1.1
set CLOUDNAME[1]=Cloudflare Servers
:: --- Magic happens here ---
:PINGINTERVAL
setlocal enabledelayedexpansion
set "cloudkey=0"
set "devicekey=0"
if not exist "%OUTPUTDIR%" mkdir "%OUTPUTDIR%"
:: Forgot how to put a newline here
echo %time% %date% >> "%OUTPUTDIR%\%OUTPUTFILE%"
echo Pinging:
echo %IPROUTER% %ROUTERNAME%
for /f "delims=" %%i in ('ping -n 1 %IPROUTER% ^| findstr "TTL timed request unreachable"') do set "res=%%~i"
if not "%res:timed=%" == "%res%" (echo Time out)
if not "%res:request=%" == "%res%" (echo Unknown Host)
if not "%res:unreachable=%" == "%res%" (echo Destination host is unreachable)
if not "%res:TTL=%" == "%res%" (echo Successful ping)
ping %IPROUTER% -n 1 >> "%OUTPUTDIR%\%OUTPUTFILE%"
:CLOUDTESTLOOP
if defined CLOUD[%cloudkey%] (
echo !CLOUD[%cloudkey%]! !CLOUDNAME[%cloudkey%]!
for /f "delims=" %%i in ('ping -n 1 !CLOUD[%cloudkey%]! ^| findstr "TTL timed request unreachable"') do set "res=%%~i"
if not "!res:timed=!" == "!res!" (echo Time out)
if not "!res:request=!" == "!res!" (echo Unknown Host)
if not "!res:unreachable=!" == "!res!" (echo Destination host is unreachable)
if not "!res:TTL=!" == "!res!" (echo Successful ping)
ping !CLOUD[%cloudkey%]! -n 1 >> "%OUTPUTDIR%\%OUTPUTFILE%"
set /a "cloudkey+=1"
if not defined CLOUD[%cloudkey%] goto :endLoopTestCloud
GOTO :CLOUDTESTLOOP
)
:endLoopTestCloud
:DEVICETESTLOOP
if defined DEVICE[%devicekey%] (
echo !DEVICE[%devicekey%]! !DEVICENAME[%devicekey%]!
for /f "delims=" %%i in ('ping -n 1 !DEVICE[%devicekey%]! ^| findstr "TTL timed request unreachable"') do set "res=%%~i"
if not "!res:timed=!" == "!res!" (echo Time out)
if not "!res:request=!" == "!res!" (echo Unknown Host)
if not "!res:unreachable=!" == "!res!" (echo Destination host is unreachable)
if not "!res:TTL=!" == "!res!" (echo Successful ping)
ping !DEVICE[%devicekey%]! -n 1 >> "%OUTPUTDIR%\%OUTPUTFILE%"
set /a "devicekey+=1"
if not defined DEVICE[%devicekey%] goto :endLoopTestDevice
GOTO :DEVICETESTLOOP
)
:endLoopTestDevice
@echo on
timeout %INTERVAL%
@echo off
GOTO PINGINTERVAL
@Simbaclaws
Copy link
Author

It might make sense to switch the 2 loops around, so that the internal network is tested before the cloud is tested.
Unless the first thing you want to know is whether you are online ofcourse.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment