bash: check for network

Byron Clark byron at theclarkfamily.name
Fri Apr 15 11:35:34 MDT 2005


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

-- 
--------------------------------+-----------------------------------
Byron Clark                     |       http://www.byronandannie.net
byron at theclarkfamily.name       |      http://bits.byronandannie.net 
--------------------------------+-----------------------------------
GnuPG Fingerprint: 0365 6979 6C3E BC0C 56C0 FB7F 12B3 75DD 042B EA68

-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
Url : http://plug.org/pipermail/plug/attachments/20050415/00db46f1/attachment.bin 


More information about the PLUG mailing list