Batch ERRORLEVEL ping response
Solution 1
I 'm not exactly sure what the interaction between FIND
and setting the error level is, but you can do this quite easily:
@echo off
for /f %%i in ('ping racer ^| find /c "(0%% loss)"') do SET MATCHES=%%i
echo %MATCHES%
This prints 0
if the ping failed, 1
if it succeeded. I made it look for just "0% loss" (not specifically 4 pings) so that the number of pings can be customized.
The percent sign has been doubled so that it's not mistaken for a variable that should be substituted.
The FOR
trick serves simply to set the output of a command as the value of an environment variable.
Solution 2
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
.
Solution 3
If you were to
echo "Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),"
you would see the %
is stripped. You need to escape it as %
has a special meaning within a batch file:
"Packets: Sent = 4, Received = 4, Lost = 0 (0%% loss),"
However its simpler to use TTL as the indication of success;
.. | find "TTL"
Solution 4
Testing for 0% loss may give a false positive, in this scenario: Let's say you normally have a network drive on some_IP-address, and you want to find out whether or not it's on.
If that drive is off, and you ping some_IP-address, the IP address from which you ping, will respond:
Answer from your_own_IP-address: target host not reachable
... 0% loss
You might be better off using if exist
or if not exist
on that network location.
Solution 5
Another variation without using any variable
ping racer -n 1 -w 100>nul || goto :pingerror
...
:pingerror
echo Host down
goto eof
:eof
exit /b
LastStar007
Updated on July 09, 2022Comments
-
LastStar007 almost 2 years
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 always displays 'failure' when run as a batch. Here is the code:
@echo off cls ping racer | find "Packets: Sent = 4, Received = 4, Lost = 0 (0% loss)," if not errorlevel 1 set error=success if errorlevel 1 set error=failure cls echo Result: %error% pause
'racer' is the name of my computer. I'm having my computer ping itself so I can eliminate the variable of a poor connection. As I said before, the batch always results in failure. Oddly enough, the program works fine if I copy the code into the command prompt. Does anyone know why the program works fine in the command prompt but doesn't work as a batch? Thanks
-
LastStar007 about 12 yearsI saw something about using "TTL" on another post, but wanted to avoid it because someone said it can return a false positive.
-
LastStar007 about 12 yearsThanks for your help! [This] (stackoverflow.com/questions/3050898/…) link was where I started this project, and its example used FIND.
-
l0ft13 about 10 yearsproblem with this is the errorlevel is only set to 1 if the host is unknown - if you ping an ip address which doesn't respond it still returns 0
-
Stephan almost 9 yearsOP intended to implement a sort of connection quality test - therefore a ping with several answers and checking for
0% loss
.net view
won't do that. -
jdw over 8 yearsAgree. Here's the situation described further:
C:\>ping -n 1 172.25.0.1 Pinging 172.25.0.1 with 32 bytes of data: Reply from 172.25.1.1: Destination host unreachable. Ping statistics for 172.25.0.1: Packets: Sent = 1, Received = 1, Lost = 0 (0% loss), C:\>echo %errorlevel% 0
-
littleguga over 7 years
ms
is also translated in other languages(e.g. Russian). -
David Glickman about 7 yearsPlease provide more information when you give an answer. See How to Answer
-
jacktrader almost 7 yearsVery neat method, I've seen a lot of batch but have never seen that. However, I'd modify it to be more like 'ping 198.168.57.98 -n 1 > nul 2>&1 && echo Success || echo failed'. Kudos
-
moteus over 6 yearsIt may not works. e.g. I got %0 loss when get response like
Host unreachable
-
Stephan about 5 yearsNote: this is language dependent (my ping says
Anwort von ...
) Also it only works with IP-Addresses, not with hostnames (as requested by the question). -
Stephan about 5 yearsdoes not (try to) answer the question.
-
BuvinJ over 3 yearsHmm. "ms" worked on French Windows. It would be great if anyone could look into, and post, the specifics of this, notably on which languages "ms" would fail on.