Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I am running this within windows xp batch file:

ping -n 3 10.1.1.2 >nul: 2>nul:
if %errorlevel%==0 (
    echo ping reply arrived        
) else (
    echo no ping reply.
)

sometime, it seems like i get no ping reply although on a parallel cmd line window running

ping  10.1.1.2 -t

Am I checking in a wrong way?

share|improve this question

1 Answer 1

Perhaps reconsider why you are testing connectivity (I assume that is what you are doing?) this way. Ping is not a reliable method of determining a hosts connectivity:

  • Ping is ICMP and can have different results on different network devices and is therefore not always guaranteed to react the same way
  • It could be being rate limited and therefore occasionally return no results
  • Dropped packets, latency, or general packet loss can cause a ping to not return; it can even be dropped by QoS
  • Ping describes nothing about the routing environment or any problems other than end to end/host

Perhaps reconsider what you are trying to do? If you give us more information I'm sure we can offer some more reliable suggestions.

EDIT: The following batch script works for me.

@echo off
ping -n 3 192.168.0.4 >nul: 2>nul:
if %ERRORLEVEL% EQU 0 (
    echo ok
) else (
    echo not ok
)
share|improve this answer
    
I have two systems connected via network switch in a closed environment. I want to check if system B is reachable over network from system A. I also want to check if network connectivity exists(eth cable in place etc.) –  Doron Jan 19 '12 at 11:16
    
Could you please say if "ping -n 3 10.1.1.2 >nul: 2>nul:" and then checking the errorlevel is correct? –  Doron Jan 19 '12 at 11:17
    
Hmm, ping does seem like the way to go then. In my experience it is impossible to have 100% success return rate on pings, no matter the environment (you should see this reflected in your -t, as well). In that case your best bet is going to be checking for consecutive ping failures or the availability of a service on the the host (http, ssh, telnet, etc). –  nullity Jan 19 '12 at 11:19
    
is this syntax correct ">nul: 2>nul:"? How could I check for an available service on the host, for example? –  Doron Jan 19 '12 at 11:22
    
Yep, that syntax is correct. I updated my original answer with a scrpit that works for me, the only different is using EQU instead of == (my batch is pretty weak, I don't know offhand if there is a difference). The casing of ErrorLevel should also be irrelevant. –  nullity Jan 19 '12 at 11:34

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.