Better Windows Batch (.bat) File for Testing Internet Connections

I wrote a post yesterday about testing your internet connectivity using a Windows batch file, but since I cannot leave well-enough alone, I decided to keep working on it.

A few things to keep in mind…

  • The line “set ESC=” ends with a non-printable ASCII ESC character (ASCII 27; Hexadecimal 0x1B) that may not be visible in your browser. Editors like Notepad++ show non-printable characters.
  • If you add “hosts” you should also add “names” – hosts entry will be displayed otherwise.
  • hosts index should start at zero with no sequence gaps
  • The “for” loop’s “end” should match count(hosts + 1)

Here is my finished code…

@echo off
cls

rem ANSI Colors
set ESC=
set red=%ESC%[31m
set green=%ESC%[32m
set default=%ESC%[0m

rem Domain names or IPs
set hosts[0]=192.168.0.1
set hosts[1]=google.com
set hosts[2]=bing.com

rem Human-friendly names (optional)
set names[0]=Router
set names[1]=Google
set names[2]=Bing  

:startover
set /p "hosts[0]=Router IP address or [ENTER] for default (%hosts[0]%): "

:loophosts
echo,
echo Checking hosts...
echo,
for /l %%i in (0,1,3) do ( 
   call :pinghost %%i
)
echo,
choice /M "Check hosts again?"
if %errorlevel% == 1 goto :startover
exit /B %errorlevel%

:pinghost
setlocal EnableDelayedExpansion
set j=%~1
@ping !hosts[%j%]! -n 1 -w 1000 | FIND "TTL=" >nul
if %errorlevel% == 0 (
	if "!names[%j%]!" == "" (
		echo !hosts[%j%]!: %green%Online%default%
	) else (
		echo !names[%j%]!: %green%Online%default%
	)
) else (
	if "!names[%j%]!" == "" (
		echo !hosts[%j%]!: %red%OFFLINE%default%
	) else (
		echo !names[%j%]!: %red%OFFLINE%default%
	)
)
exit /B 0

3 thoughts on “Better Windows Batch (.bat) File for Testing Internet Connections

  1. Bri

    A better way to set up the ANSI char would be as follows:

    for /F “tokens=1,2 delims=#” %%a in (‘”prompt #$H#$E# & echo on & for %%b in (1) do rem”‘) do (
    set ESC=%%b
    exit /B 0
    )

    You can then simply use something like this:
    echo. %ESC%[31mERROR%ESC%[30m

    or is you still want to keep your vars then:
    echo. %red%ERROR%default%

    Reply
  2. Bri

    For example:

    @echo off &cls
    setlocal

    rem ANSI Colors
    call :setESC

    set red=%ESC%[31m
    set green=%ESC%[32m
    set default=%ESC%[0m

    rem Domain names or IPs
    set hosts[0]=192.168.0.1
    set hosts[1]=google.com
    set hosts[2]=bbc.co.uk

    rem Human-friendly names (optional)
    set names[0]=Router –
    set names[1]=Google –
    set names[2]=BBC –

    :startover
    set /p “hosts[0]=Router IP address or [ENTER] for default (%hosts[0]%): ”

    :loophosts
    echo,
    echo Checking hosts…
    echo,
    REM If you add more hosts above then alter the following to match:
    for /l %%i in (0,1,2) do (
    call :pinghost %%i
    )
    echo,
    choice /M “Check hosts again?”
    if %errorlevel% == 1 goto :startover
    exit /B %errorlevel%

    :pinghost
    setlocal EnableDelayedExpansion
    set j=%~1
    @ping !hosts[%j%]! -n 1 -w 1000 | FIND “TTL=” >nul
    if %errorlevel% == 0 (
    if “!names[%j%]!” == “” (
    echo !hosts[%j%]! %green%Online%default%
    ) else (
    echo !names[%j%]! %green%Online%default%
    )
    ) else (
    if “!names[%j%]!” == “” (
    echo !hosts[%j%]! %red%OFFLINE%default%
    ) else (
    echo !names[%j%]! %red%OFFLINE%default%
    )
    )
    exit /B 0

    :setESC
    for /F “tokens=1,2 delims=#” %%a in (‘”prompt #$H#$E# & echo on & for %%b in (1) do rem”‘) do (
    set ESC=%%b
    exit /B 0
    )

    Reply

Leave a Reply to Bri Cancel reply

Your email address will not be published. Required fields are marked *