bash: check for network

Justin Gedge jgedge at amis.com
Fri Apr 15 11:44:24 MDT 2005


Byron--
You're right... here it is-- strait out of the man page:

       If ping does not receive any reply packets at all it will exit 
with  code  1.  If  a  packet
       count and deadline are both specified, and fewer than count 
packets are received by the time
       the deadline has arrived, it will also exit with code 1.  On 
other error it exits with  code
       2.  Otherwise  it exits with code 0. This makes it possible to 
use the exit code to see if a
       host is alive or not.

thanks for the info-- don't know about the rest of plug-- but the 
example code is always fun to see.

Justin Gedge



Byron Clark wrote:

>On Fri, Apr 15, 2005 at 11:17:12AM -0600, Justin Gedge wrote:
>  
>
>>This is where you get back to the whole mess of using grep.  You don't 
>>want to check wether ping ran or not-- you want to check what the 
>>results of ping were.  Pipe the results of ping through grep-- and 
>>filter on " 0%" or  " 100%".  Also-- putting the -w for timout is good 
>>too-- otherwise ping may sit there for quite a while.  I figure-- on a 
>>good network [locally] if your ping is over 1s there are other problems 
>>at large.
>>    
>>
>
>Using grep to see if ping succeeded or not is "the hard way."  ping has
>an exit code of 0 if there are no errors and there is a response; the
>exit code is 1 or 2 otherwise.  I know someone already showed the easy
>way, but just to reiterate:
>
>if ping -c 1 -w 1 -q example.com &>/dev/null; then
>    echo "example.com is up"
>else
>    echo "example.com is down"
>fi
>
>  
>
>------------------------------------------------------------------------
>
>.===================================.
>| This has been a P.L.U.G. mailing. |
>|      Don't Fear the Penguin.      |
>|  IRC: #utah at irc.freenode.net   |
>`==================================='
>




More information about the PLUG mailing list