[nycbug-talk] To ping or not to ping, that is the question...

Ray Lai nycbug at cyth.net
Sat Jul 8 08:02:02 EDT 2006


On Sat, Jul 08, 2006 at 07:41:36AM -0400, Isaac Levy wrote:
> Hi Ray,
> 
> On Jul 8, 2006, at 7:31 AM, Ray Lai wrote:
> 
> >> Inside the box:
> >> - to my knowledge, (and digging through the man pages), ping cannot
> >> bind itself to a particular network interface, which is a problem on
> >> boxes with multiple interfaces, with IP's all on the same subnet.
> >> (ping just finds the first IP route, and pings out on that interface.
> >
> > NetBSD and OpenBSD's ping allows this with the -I flag.  FreeBSD's
> > allows it with the -S flag.
> 
> Well I stand corrected, happily :)
> 
> >
> >> Outside the box:
> >> - to my knowledge, (and digging through the man pages), ping does not
> >> have any options which let the utility return some sort of boolean
> >> value for weather or not a packet was returned.
> >
> > It returns 0 if a packet was received and other values otherwise,
> > depending on the operating system.  You'll probably want to use the
> > -c flag.
> >
> > -Ray-
> 
> Ok- so now here's my new question, how can I get ping to return less  
> verbose stuff, a simple integer returned would be way nicer to  
> programatically deal with (vs. having to parse the text)- which in  
> the context of my app, is all just verbose garbage for me to deal  
> with...
>
> Additionally, I'd like to be able to set some kind of response  
> timeout, (under 1 second)?

Just do:

ray at x[~] COUNT=1; WAIT=1; ping -c "$COUNT" -w "$WAIT" google.com >/dev/null && echo success || echo fail
success
ray at x[~] COUNT=1; WAIT=1; ping -c "$COUNT" -w "$WAIT" 10.0.1.9 >/dev/null && echo success || echo fail
fail

Unfortunately the -w flag only accepts integers, so the lowest
timeout you can set is one second.

-Ray-



More information about the talk mailing list