bash: check for network

Justin Gedge jgedge at amis.com
Thu Apr 14 10:10:46 MDT 2005


on sun-- the default for ping gives you a text message of:

[hostname] is alive

and you have to do a ping -s if you want to see all the tries.

In linux-- if you want to just check for a connection use the -q [quiet 
option] and -c [count] -w [timeout]

ping -q -c -w 1 172.16.236.100

jgedge-slc7.urad.com>ping -q -c 1 -w 1 172.16.236.100
PING 172.16.236.100 (172.16.236.100) from 172.16.236.134 : 56(84) bytes 
of data.

--- 172.16.236.100 ping statistics ---
1 packets transmitted, 1 received, 0% loss, time 0ms
rtt min/avg/max/mdev = 0.181/0.181/0.181/0.000 ms

I guess the trick from here is to parse all this and return a value to a 
var for a conditional...

This is where my response may be of less value [since I know csh/tcsh 
better than bash].

in csh something like:

set alive = ( `ping -q -c 1 172.16.236.100 |& grep " 0% loss" ` )

echo $alive
1 packets transmitted, 1 received, 0% loss, time 0ms

make sure you include the leading space in the grep-- other wise [0% 
loss] and [100% loss] will both be matched.

with this the alive var will either be set to a long string [shown 
above] -- or it will be set to "".

from here it a string compare of some sort -- and since I'd don't know 
bash [and usually go through a few minutes of trial/error in my csh 
scripts at this point]-- I'll leave the rest as an exercise for the 
student :-)


Justin Gedge


Dan Wilson wrote:

>Hey all,
>
>I've got a cron script that connects to a remote server to accomplish a
>few tasks.  I run this on my laptop and sometimes don't have a network
>connection.  If there is no connection, I just end up getting a bunch of
>emails stuck in the queue and eventually sent to me that say it couldn't
>connect to the server.
>
>So within my script, I'd like to be able to check if I have a network
>connection prior to starting my tasks.
>
>I'm sure there is an easy way to do this (without too much awk, sed and
>grepping, etc).  Any ideas?
>
>-Dan
>
>.===================================.
>| 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