bash: check for network

Paul Seamons paul at seamons.com
Fri Apr 15 12:43:25 MDT 2005


This may not be as popular with the non-perl types out there but here is 
something short and even readable (copy and paste these onto a command line 
to try them out):

perl -e 'use Net::Ping; print Net::Ping->new->ping("byu.edu") ? "Yup\n" : 
"Nope\n"'

And for hosts that aren't listening on the echo port but do have a web server 
running:

perl -e 'use Net::Ping; $p=Net::Ping->new; $p->{port_num}=80; print 
$p->ping("yahoo.com") ? "Yup\n" : "Nope\n"'

Of course you can configure it even more if you want (timeouts, tcp vs udp, 
and so on).  For more info see 
http://search.cpan.org/~bbb/Net-Ping-2.31/lib/Net/Ping.pm .

Enjoy

Paul

On Thursday 14 April 2005 09:39 am, 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