1

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?

Dan
  • 15,430
  • 1
  • 36
  • 67
Doron
  • 21
  • 4
  • 10

1 Answers1

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
)
Josh Atkins
  • 783
  • 4
  • 6
  • 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). – Josh Atkins 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. – Josh Atkins Jan 19 '12 at 11:34