Batch Files - Using ping to test network connectivity

Viewed 87772

Using a batch file would it be possible to do something like:

ping google.com

if return success do ECHO You are connected to the internet

else return ECHO You are not connected to the internet

9 Answers

Based on the answer from @CShulz, here's a script that'll print "Not connected" only when there's no connection, else it'll silently loop through the test every 30 seconds. First ping tests for connectivity & prints an error message if there's a problem. The second ping adds a 30 second wait by pinging the localhost.

@echo off
:loop
ping www.google.com -n 1 -w 5000 > nul
if errorlevel 1 echo Not connected
ping -n 30 127.0.0.1 > nul
goto loop
echo Testing Internet Connection of google.com
@echo off
:loop
ping google.com -n 1 -w 5000 > nul
if errorlevel 1 echo %date% - %time% Not connected >> pingtestlog.txt
ping -n 30 127.0.0.1 > nul
goto loop

I understand this is an old thread, but here's my contribution anyways.

@echo off

TITLE Network Connection Watchdog

:: Use Windows "Task Scheduler".
:: Set to run at "Startup" with a delay and interval of your choice.
:: Remember to tick "Run with highest privileges".

:: Last command will only be executed if all pings fail.

set /a pc=0
set /a total=0

:: Your preferences
set if="Wi-fi" &:: Find your interface name with [netsh interface show interface].
set ip=192.168.0.1
set /a pt=10 &:: Set amount of pings to be executed.

:loop
set /a pc+=1
ping %ip% -n 1 -w 100 | find "TTL=" >NUL 2>&1
for /f %%a in ('echo %errorlevel%') do set p%pc%=%%a

set /a total+=p%pc%
if not %pc%==%pt% goto loop

if not %total%==%pt% (
goto eof
) else (
netsh interface set interface %if% disable
ping localhost -n 5 >NUL 2>&1
netsh interface set interface %if% enable
)

:eof
exit

This basic script can ping google.com continuously and test your internet connection. script will run in a loop until you close the window.

@echo off
:loop
ping www.google.com -n 1 -w 5000 > null
    if not errorlevel 1 set msg=Your are connected with internet...
    if errorlevel 1 set msg=No Internet...
cls
color 0a
echo %msg%
goto loop
Related