[X-Unix] How to determine which network component is failing

Stroller macmonster at myrealbox.com
Tue Jul 25 21:20:25 PDT 2006


On 26 Jul 2006, at 00:33, Charles Howse wrote:
> ...
> if [ ! "`ping -c 3 google.com | grep packets`" == "$offline" ] ; then
> 	date >> $logfile
> 	echo host1 online, exiting >> $logfile
> 	exit 0
> elif
> 	[ ! "`ping -c 3 microsoft.com | grep packets`" == "$offline" ] ; then
> 	date >> $logfile
> 	echo host2 online, exiting >> $logfile
> 	exit 0
> elif
> 	[ ! "`ping -c 3 yahoo.com | grep packets`" == "$offline" ] ; then
> 	date >> $logfile
> 	echo host3 online, exiting >> $logfile
> 	exit 0
> else
> 	what_failed
> fi
>
> exit 0

This grepping for "offline" seems a little contrived, if you'll  
excuse me mentioning it.

You set an exit staus with your script, so why not trust others to do  
so, too?

    $ ping -c 3 google.com
    PING google.com (72.14.207.99): 56 data bytes
    64 bytes from 72.14.207.99: icmp_seq=0 ttl=243 time=90.402 ms
    64 bytes from 72.14.207.99: icmp_seq=1 ttl=243 time=169.823 ms
    64 bytes from 72.14.207.99: icmp_seq=2 ttl=243 time=90.617 ms

    --- google.com ping statistics ---
    3 packets transmitted, 3 packets received, 0% packet loss
    round-trip min/avg/max/stddev = 90.402/116.947/169.823/37.389 ms
    $ echo $?
    0
    $ ping -c 3 hgurhurhrhufhrh.com
    ping: cannot resolve hgurhurhrhufhrh.com: Unknown host
    $ echo $?
    68
    $

`man ping` says:

    RETURN VALUES
        The ping utility returns an exit status of zero if at least  
one response
        was heard from the specified host; a status of two if the  
transmission
        was successful but no responses were received; or another  
value (from
        <sysexits.h>) if an error occurred.


I'm not sure how valuable it is to make 3 ping requests, but should  
you wish to do so then I think I might `grep -o '[[:digit:]]\{1,3\}%  
packet loss' and provide this as output to the user.

Stroller



More information about the X-Unix mailing list