[GLLUG] (OT) Win32 ping and errorlevel ?

Brad Fears fearsb@michigan.gov
Mon, 04 Feb 2002 09:22:00 -0500


Windows doesn't handle errorlevels as well as *nix does.  How about this instead?

@echo off
for /F "tokens=1-7 delims=:, " %%i in ('ping -n 5 -w 5000 10.0.0.8 ^|find ^"% loss^"') do (
   set xSent=%%l
   set xReceived=%%o
)

if %xSent% == %xReceived% goto next
echo Must be DOWN!
:: Insert your commands here
goto exit

:next
echo Must be UP!
:: Insert your commands here

:exit


--Brad Fears

>>> "Melson, Paul" <PMelson@sequoianet.com> 02/01/02 04:21PM >>>
Sorry for the Microsoftish question, but it seems like someone might have stumbled across this issue already.

Does anybody know about PING.EXE for WinNT/2K and its errorlevels?  I'm trying to script a Scheduled Task that would check to see if a network connection (VPN in this case, but that's irrelevant) is up before launching MOBSYNC.EXE.  Here is the offending code:

@echo off
ping -n 5 10.0.0.8				# This sends 5 packets to overcome any ARP or lag issues.
ping -n 1 -w 5000 10.0.0.8			# This sends 1 packet w/ 5sec wait time to assure the IP is up.
if not errorlevel 0 goto Fail			# If there is any errorlevel other than 0, we have a problem.


But we have a problem, anyway.  It seems to be skipping the "if" statement.  Regardless of whether or not it's able to ping the host, it tries to run the commands to connect to the host and sync up.  Is there something wrong w/ my syntax?  I chose to do it this way because I was unable to find any documentation on PING.EXE and specific errorlevels.  Or does PING.EXE simply not return errorlevels?  I would do this w/ a shell script in Cygwin, but Cygwin uses the same ping program, and it seems like such a simple task to use Ping.pm for.  Any help you can provide is greatly appreciated.

PS - the actual batch file doesn't have the #-marked comments.

Thanks,
PaulM