ERRORLEVEL ping batch response

I am trying to use a batch file to confirm network connectivity using ping. I want to do a batch run and then print if ping was successful or not. The problem is that it always displays a "crash" when run as a package. 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 have a ping computer itself, so I can eliminate the bad connection variable. As I said, the party always leads to failure. Oddly enough, the program works fine if I copy the code on the command line. Does anyone know why the program works fine on the command line but doesn't work as a package? thanks

+10
windows batch-file command-prompt ping
source share
14 answers

I'm not quite sure what the interaction is between FIND and setting the error level, 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% 

0 sent if ping failed, 1 if it succeeded. I made him search only for “0% loss” (not only 4 pings) so that the number of pings could be adjusted.

The percent sign has been doubled, so it is not mistaken for a variable that needs to be replaced.

The FOR triple simply serves to set the command output as the value of an environment variable.

+2
source share

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 if a string is printed, for example 69 ms or 314ms , using ping .

(Translated versions of Windows can print 42 ms (with a space), so we check this.)

Cause:

Other suggestions, such as matching time= or TTL , are not so reliable, because when checking IPv6 addresses, TTL is not displayed (at least on my Windows 7 machine), and translated versions of Windows may show the translated version of the time= string. In addition, not only the translation time= , but sometimes it can be time< , not time= , as in the case of time<1ms .

+21
source share

If you were

 echo "Packets: Sent = 4, Received = 4, Lost = 0 (0% loss)," 

you will see that % free. You need to avoid this, since % has a special meaning in the batch file:

 "Packets: Sent = 4, Received = 4, Lost = 0 (0%% loss)," 

However, it is easier to use TTL as an indicator of success;

 .. | find "TTL" 
+4
source share

Testing for a 0% loss can give a false result in this scenario: Let's say you usually have a network drive at some_IP address and you want to know if it is turned on.

If this drive is turned 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 in this network location.

+4
source share

The simplest solution for this that I can think of:

 set error=failure ping racer -n 1 -w 100>nul 2>&1 && set error=success 

Of course, -w needs to be adjusted if on a slow line (100 ms might be too short on Dialup ;-))

considers

+1
source share

Another change without using any variable

 ping racer -n 1 -w 100>nul || goto :pingerror ... :pingerror echo Host down goto eof :eof exit /b 
+1
source share
 ping 198.168.57.98 && echo Success || echo failed 
+1
source share

Based on a comment by Alex K., this works for me on Windows 7:

 @echo off setlocal enableextensions enabledelayedexpansion for /f %%i in (PCS.TXT) do ( SET bHOSTUP=0 ping -n 2 %%i |find "TTL=" > NUL && SET bHOSTUP=1 IF !bHOSTUP! equ 1 ( CALL :HOSTUP %%i ) else ( CALL :HOSTDOWN %%i ) ) GOTO EOF :HOSTUP echo Host UP %1 GOTO EOF :HOSTDOWN echo Host DOWN %1 GOTO EOF :EOF exit /B 
0
source share

First of all

 >@echo off >for /f %%i in ('ping racer ^| find /c "(0%% loss)"') do SET MATCHES=%%i >echo %MATCHES% 

Does not work. If he does not fail, he will find 0%, because he has 0%. If it fails, it does not work, because it will have a 100% loss, which means it found a 0% loss in 10 10 (loss 0%)

Was it determined by a 100% loss:

 >for /f %%i in ('ping -n 1 -l 1 %pc% ^| find /c "(100%% loss)"') do SET check=%%i 

The error level may be a little messed up, but it works like a charm:

 >if '%check%'=='1' goto fail >if '%check%'=='0' echo %pc% is online.&goto starting 

1 means he failed 0 means he succeeded

My script uses links. Going to failure will be: a failure in my script that tells me that% pc% (which I will enter to the user at the beginning) is offline and will go to another start.

 >:fail >color 0c >title %pc% is offline >echo %pc% is offline >PING -n 6 127.0.0.1>nul >goto choice 

Hope this helps.

0
source share

ping has an error output value. Success is 0, failure is 1. Just do the following:

 C:\>ping 4.2.2.2 Pinging 4.2.2.2 with 32 bytes of data: Reply from 4.2.2.2: bytes=32 time=28ms TTL=57 Reply from 4.2.2.2: bytes=32 time=29ms TTL=57 Reply from 4.2.2.2: bytes=32 time=30ms TTL=57 Reply from 4.2.2.2: bytes=32 time=29ms TTL=57 Ping statistics for 4.2.2.2: Packets: Sent = 4, Received = 4, Lost = 0 (0% loss), Approximate round trip times in milli-seconds: Minimum = 28ms, Maximum = 30ms, Average = 29ms C:\>echo %errorlevel% 0 C:\>ping foo.bar Ping request could not find host foo.bar. Please check the name and try again. C:\>echo %errorlevel% 1 

As you can see, there is no need for this script overflow.

0
source share
 set ErrorLevel=1 (ping example.com -n 1 && set ErrorLevel=0)>nul 

ErrorLevel 0 if example.com is online, 1 otherwise.

NOTE. Tested only on Win8!

0
source share

Yes ping does not return the correct error level. To check the network connection and the computer, I used "net view computername" and then checked% errorlevel% - simple and easy

0
source share

I needed to reset the Wi-Fi connection because it has problems. That was my quick decision.

 @echo off Rem Microsoft Windows 10 ping test to gateway. Rem Run batch file from an administrative command prompt. cls :starting Rem Send one ping to the gateway. Write the results to a file. ping 192.168.1.1 -n 1 > pingtest.txt Rem Search for unreachable in the file. c:\windows\system32\findstr.exe "unreachable" pingtest.txt Rem errorlevel 0 reset the adapter if 1 then wait 10 minutes and test again if %errorlevel%==1 goto waiting Rem unreachable was found reset the adapter. Rem write the date and time the reset was done. echo Reset date: %date% time: %time% >> resettimes.txt Rem issue netsh interface show interface to find your adapter name to reset Rem my adapter is "wi-fi" netsh interface set interface "wi-fi" disable timeout /t 5 netsh interface set interface "wi-fi" enable :waiting echo "It is online waiting 10 minutes" timeout /t 600 goto starting 
0
source share

I liked the concept of FIND in the ping results, but why not just FIND the answer from the sending address?

In the example below, I enter the IP address as a variable, check this IP address, then look for this variable in the response line using the FIND command. If the response line contains anything other than the correct IP address, it reports an error.

If you want, you can just read the ERRORLEVEL value from FIND. This will give you reliable value for the job.

 @echo off Set /P IPAdd=Enter Address: cls ping %IPAdd% | find "Reply from %IPAdd%:" if not errorlevel 1 set error=success if errorlevel 1 set error=failure cls echo Result: %error% pause 
0
source share

All Articles