Ping return code

  • Thread starter Thread starter Demis Gonçalves
  • Start date Start date
D

Demis Gonçalves

Hi for all, on unix when i use the ping command and a machine returns with
time out the SO returns to me with the code 1, telling me that i hadn´t no
answer. On Windows if a computer returns with time out the windows returns
to me a return code 0. How do i do to got the correct return code of
Windows. I need a return code other 0 when a computer times out.

Thanks in advance.

--
===========================
Demis Gonçalves
Sr. Support Analyst
NetControl Network Management
São Paulo - Brazil
Mobile: 55 11 9904-9684
Office : 55 11 3227-2872
e-mail: (e-mail address removed)

"Tivoli, love it or leave it!"
===========================
 
Hi for all, on unix when i use the ping command and a machine returns with
time out the SO returns to me with the code 1, telling me that i hadn´t no
answer. On Windows if a computer returns with time out the windows returns
to me a return code 0. How do i do to got the correct return code of
Windows. I need a return code other 0 when a computer times out.

Thanks in advance.

set OK=Y
for /f "Tokens=*" %%a in ('ping xxx.xxx.xxx.xxx^|FIND "Request timed out."') do (
set OK=N
)
If "%OK%" EQU "N" goto timeout


Jerold Schulman
Windows: General MVP
JSI, Inc.
http://www.jsiinc.com
 
The following results are from Windows XP Pro. -

[Paste]
C:\>ping -n 1 172.31.0.99

Pinging 172.31.0.99 with 32 bytes of data:

Request timed out.

Ping statistics for 172.31.0.99:
Packets: Sent = 1, Received = 0, Lost = 1 (100% loss),

C:\>echo %ERRORLEVEL%
1
[/Paste]

If you're running Windows 2000 (which I'm guessing you are), try
something like this -

ping 172.30.2.2 -n 1 | find "Reply from"

HTH
 
Demis said:
Hi for all, on unix when i use the ping command and a machine returns with
time out the SO returns to me with the code 1, telling me that i hadn´t no
answer. On Windows if a computer returns with time out the windows returns
to me a return code 0. How do i do to got the correct return code of
Windows. I need a return code other 0 when a computer times out.
Hi

This is a "bug" in Win2k's ping.exe that is fixed in WinXP.

The best solution I have found to detect if a computer is online or not
is to search for the text "TTL=" in the output from ping.exe. I have
found it to work all situations/languages I have come up with.

So this should work fine for both WinXP and Win2k:

'--------------------8<----------------------
set host=128.33.12.144
set connected=N
for /f "Tokens=*" %%c in ('ping -n 1 %host% ^| FIND "TTL="') do (
set connected=Y
)
if "%connected%" EQU "Y" echo %host% is online
'--------------------8<----------------------
 
Hi for all, on unix when i use the ping command and a machine returns with
time out the SO returns to me with the code 1, telling me that i hadn´t no
answer. On Windows if a computer returns with time out the windows returns
to me a return code 0. How do i do to got the correct return code of
Windows. I need a return code other 0 when a computer times out.

Try the FREE Mount/\Command called .Alive

It is based on Ping.exe and works CONSISTENTLY
across NT/2K/XP/K3.

*******

A frequent SysAdmin need is to check the accessibility of a remote
system by using the native Ping utility. A new M^^C called .Alive
provides a CONSISTENT self-documenting method to check one machine,
a list of machines or an entire range of IP addresses.

The variable which contains the input is called, of course, %Alive%.

The scripting code is stored in variable %.Alive%.

Since the input is expanded at runtime, you can set the input value
and run the command within the same line of code! Delayed Variable
Expansion is not necessary, allowing full compatibility across
NT/2K/XP/K3. The self-documenting code looks like this:

FOR /L %%A IN (1,1,254) DO @(
(SET "Alive=10.7.7.%%A")
(%.Alive%)
)

..Alive takes the internal Ping command up on Mount Knowledge!
The code above will check the entire address range from
10.7.7.1 through 10.7.7.254, reporting on each address in turn.

But it get even better than that!

Success is reported to STDOUT, failure is reported to STDERR.
So, to see only the addresses that are active, suppress STDERR
using the %.Kity% {Keep-It-To-Yourself] command.

FOR /L %%A IN (1,1,254) DO @(
(SET "Alive=10.7.7.%%A")
(%.Alive% %.Kity%)
)

To see only the machines that are inaccessible,
suppress STDOUT using the %.Quiet% command.

FOR /L %%A IN (1,1,254) DO @(
(SET "Alive=10.7.7.%%A")
(%.Alive% %.Quiet%)
)

Additional examples are on the pre-release ".Alive Page" at
(http://TheSystemGuard.com/MtCmds/CrystalClear/Alive.htm).

There is also a link there to download the pre-release code
and test it on your machine.

To look up the syntax of any other "Mounted Commands"
used in the examples, see the Master Catalog at
(http://TheSystemGuard.com/MasterCatalog.asp).


-tsg

/-----------------+---------------+----------------------\
| COMPATIBILITY | CLARITY | SPEED |
| Write code ONCE | Make it clear | THEN...Make it fast! |
\-----------------+---------------+----------------------/
400+ command-line resources using ONLY native NT commands!
(http://TheSystemGuard.com/default.asp#MasterCommandList)
 
You could try the ALIVE freeware available at:
http://wettberg.home.texas.net/freeware.htm

|"Demis Gonçalves" wrote
|
|> Hi for all, on unix when i use the ping command and a machine returns with
|> time out the SO returns to me with the code 1, telling me that i hadn´t no
|> answer. On Windows if a computer returns with time out the windows returns
|> to me a return code 0. How do i do to got the correct return code of
|> Windows. I need a return code other 0 when a computer times out.
|>
 
Back
Top