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
.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…