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

Simbaclaws commented Jan 8, 2023

I needed this for a quick test on why my network was having issues, wanted to know what caused it... A friend came in and gave me a simplified version of this, which I've since turned into this little script for sysadmins to use... It basically goes through 2 arrays for cloud and for local devices in your network to ping, and displays in the console whether it was timed out, an unknown host, unreachable or succesful, all of the pings are kept in the output log.

Just alter or add the variables at the top of the file like how they are used now.

The interval will make it automatically ping every x amount of seconds so that you can check whether your network is stable.

I'll create a POSIX compliant UNIX variant later in case a sysadmin needs to execute this on a UNIX system instead.
Maybe even a Powershell variant eventually.

@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