bash: check for network

Andrew McNabb amcnabb at mcnabbs.org
Fri Apr 15 11:50:46 MDT 2005


On Fri, Apr 15, 2005 at 11:35:34AM -0600, Byron Clark wrote:
> 
> 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
> 

If you're using ping for scripting, I'd like to point out a problem I've
had in the past, along with a workaround.  At least some versions of
ping are expecting to have a working standard input.  I don't know what
they use it for, but they hang if there is no stdin available.  In some
scripting contexts, such as in boot scripts, cron jobs, etc., there may
not be a standard input available.  If you do this:

if ping -c 1 -w 1 -q example.com </dev/null &>/dev/null; then

instead of the above, you shouldn't have that problem.

Your version of Ping may or not be broken like that, but I thought I'd
point it out.  It took me a while to track down because it worked just
fine when I ran the script by hand, but when it was in other contexts it
would hang.

-- 
Andrew McNabb
http://www.mcnabbs.org/andrew/
PGP Fingerprint: 8A17 B57C 6879 1863 DE55  8012 AB4D 6098 8826 6868
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 186 bytes
Desc: not available
Url : http://plug.org/pipermail/plug/attachments/20050415/f8ff1010/attachment.bin 


More information about the PLUG mailing list