This page is edited from the 1cmdfaq.txt faq-file contained in
my tscmd.zip
command line interface (CLI) collection. That zipped file has much
additional material, including a number of detached .cmd script
files. It is recommended that you also get the zipped version as a
companion.
Please see "
The Description and
the Index page" for the conditions of usage and other such
information.
153} How do I test if my Internet connection to an IP is working?
There are so many different ways of writing a code. Here is one of
the many alternatives in XP
@echo off & setlocal enableextensions
set ok_=true
ping -n 1 %1|find "Lost = 0">nul||set ok_=
if defined ok_ (
echo You have an active connection to %1
) else (
echo You have NO active connection to %1
)
endlocal & goto :EOF
Output
C:\_D\TEST>cmdfaq www.google.com
You have an active connection to www.google.com
However, since ping returns an errorlevel directly, at least in XP,
one can also write
@echo off & setlocal enableextensions
ping -n 1 194.109.6.66>nul
if %errorlevel% EQU 0 (
echo You have an active connection to the internet
) else (
echo You have NO active connection to the internet
)
endlocal & goto :EOF
Alternatively, just
@echo off & setlocal enableextensions
(ping %1>nul
&& echo Success
) || echo Failure
endlocal & goto :EOF
Output
C:\_D\TEST>cmdfaq www.google.com
Success