Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I am specifically looking on the return variables of running ping on my Ubuntu server, the variables I am looking for is: error status, output on stdout, output on stderr.

When pinging a host the following three scenarios can happen:

  1. The host answers on ALL packets (No error)
  2. The host answers on ZERO packets (Error)
  3. The host answers on SOME packets (Unspecified behavior)

I have read through man ping, a couple of times and can't seem to find what I should expect in that situation.

I have no way to test this, the only machines I have access to is a Linode virtual server running Ubuntu, and my Macbook running Snow Leopard (Probably not with the same PING program as my server)

What should I expect as return variables from the third scenario?

share|improve this question

migrated from serverfault.com Aug 14 '11 at 16:30

This question came from our site for professional system and network administrators.

2 Answers 2

up vote 1 down vote accepted

Without knowing what language you are using to write your code in, it is difficult to give you specifics, but here is a small primer on outputs.

error, stdout, stderr are not variables, but types of output that a unix binary can return.

stdout - This is the standard text that a program outputs to the screen (in many cases) or file (if redirected) or another program, it can be captured using

program > file

stderr - This is text that is outputted to the error stream, an second output that is not caputred by any redirection of the stdout stream. You can capture this using

program 2> file

error - This is the code returned from the program after it completes. Success is usually indicated by a 0. You can capture this using

#!/bin/bash
program
echo $?

You can find out more about streams at the Wikipedia page for Standard_Streams. There is good documentation of capturing streams in bash in the bash manual.

share|improve this answer
2  
By the way: The return value for successful pings with partial packet loss will be 0. –  Stefano Palazzo Aug 14 '11 at 16:37
    
@Stefano Palazzo: How do you know that? From a test, or from the man page? –  Kasper Grubbe Aug 14 '11 at 17:42
    
@StefanoPalazzo I get 1 with partial packet loss (with the default ping, from IPutils), with the command ping -w 5 -c 4 8.8.8.8 (and a network fault that drops some of the packets). –  Gilles Aug 14 '11 at 17:54
    
@Gilles @Kasper I've run ping -c 2 8.8.8.8 and disconnected the network halfway through. This is the result (the bit that was still in my clipboard anyway). Uhm, maybe it has to with which pings fail? (i.e. the first one vs. the last one?) –  Stefano Palazzo Aug 14 '11 at 19:17
1  
ping returns 0 if the host was contactable at least once, 1 if not and 2 for all other errors unless you set a timeout -w|-W and a count -c in which case it returns 1 if all packets sent didn't get a response within the timeout period. –  Iain Aug 14 '11 at 20:54

There are several ping utilities available on Linux, even on Ubuntu. The default one (the only one in Ubuntu main) is from iputils (iputils-ping). The possible return statuses (for either ping or the companion IPv6 utility ping6), as of iputils 20100418, are:

  • 0 if you pass the -V option.
  • 255 if ping can't drop privileges (this shouldn't happen).
  • 1 if an error occurs while parsing the subject name with ping6 -N.
  • Either 0 or 1 in case of normal exit:
    • 1 if no reply was ever received.
    • 1 if you set a deadline with the -w or -W option, and the number of replies received was smaller than the number of requests sent.
    • 0 otherwise, i.e. if there was a deadline and all expected replies were received, or if there was no deadline and at least one reply was received.
  • 2 in all other cases.

If you need to make finer distinctions, you'll have to parse the output. The utility uses stderr for fatal errors and some warnings, and stdout for various status reports and informational messages. These messages are meant for human consumption, so don't expect to have an easy time parsing them, expect to have to update your parser every time a version comes out or you want to work on a different platform, and remember to set LC_MESSAGES=C in case ping is ever translated (it currently isn't). There are very few uses cases that require parsing the output — usually all you need to know is 2 = DNS lookup failed, 1 = host not responding, 0 = success.

share|improve this answer

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.