I\'m trying to use a batch file to confirm a network connection using ping. I want to do batch run and then print if the ping was successful or not. The problem is that it a
A more reliable ping
error checking method:
@echo off
set "host=192.168.1.1"
ping -n 1 "%host%" | findstr /r /c:"[0-9] *ms"
if %errorlevel% == 0 (
echo Success.
) else (
echo FAILURE.
)
This works by checking whether a string such as 69 ms
or 314ms
is printed by ping
.
(Translated versions of Windows may print 42 ms
(with the space), hence we check for that.)
Reason:
Other proposals, such as matching time=
or TTL
are not as reliable, because pinging IPv6 addresses doesn't show TTL
(at least not on my Windows 7 machine) and translated versions of Windows may show a translated version of the string time=
. Also, not only may time=
be translated, but sometimes it may be time<
rather than time=
, as in the case of time<1ms
.