Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

Efficiently check internet connection?

by Cody Pendant (Prior)
on May 23, 2009 at 21:54 UTC ( #765855=perlquestion: print w/replies, xml ) Need Help??

Cody Pendant has asked for the wisdom of the Perl Monks concerning the following question:

I want to run a regular script from a machine whose internet connection isn't always reliable.

What do monks think is a good way to check that the machine is online?

If it was me at the keyboard I'd just ping google or something.

Nobody says perl looks like line-noise any more
kids today don't know what line-noise IS ...

Replies are listed 'Best First'.
Re: Efficiently check internet connection?
by Perlbotics (Bishop) on May 23, 2009 at 22:56 UTC

    Why bother Google when your program can timeout while connecting to the real destination? This has also the advantage that the protocol used for testing is the same as later on for communication - which can be a good idea in the presence of firewalls.
    More specific advice might be possible if you tell us more about the protocol/application.

    A Super Search for connection timeout delivers some more suggestions.

    Update: To check for presence of a server that is not confused if someone is ringing off without saying a word...

    sub check_port { my $host = shift; # hostname or hostname:port my $timeoutseconds = shift || 1; my $proto = "tcp"; my $handle = IO::Socket::INET->new('PeerAddr'=>$host,'Timeout'=>$tim +eoutseconds, 'Proto'=>$proto); if (defined $handle && $handle) { $handle->close(); return 1; # port in use } return 0; # not reachable or timeout }

Re: Efficiently check internet connection?
by ig (Vicar) on May 23, 2009 at 23:14 UTC

    I wouldn't test that the machine is online. I would just do what I want to do and handle the error that occurs when disconnected.

    The only case I can think of where there might be an advantage to testing is if there is a lot of work to prepare for whatever transaction is to happen over the network. Even in this case, I think a better solution than testing would be to do the work and use the results immediately if the network connection succeeds or save them to be used later if the network connection fails. In this way, the work of preparation is done only once and there are no superfluous tests to give false negatives or false positive.

Re: Efficiently check internet connection?
by McDarren (Abbot) on May 24, 2009 at 06:06 UTC
    If you are interested in tracking this historically, check out Smokeping.

    Excellent tool, highly configurable. It will also send email alerts, if required. And, it's written in Perl :)

    Hope this helps,

Re: Efficiently check internet connection?
by shmem (Chancellor) on May 23, 2009 at 22:21 UTC
    If it was me at the keyboard I'd just ping google or something.

    Do that from within the script ;-)

Re: Efficiently check internet connection?
by sathiya.sw (Monk) on May 26, 2009 at 11:55 UTC
    check the Net::Ping which is meant for "check a remote host for reachability".

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://765855]
Approved by ww
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (6)
As of 2021-02-28 13:08 GMT
Find Nodes?
    Voting Booth?

    No recent polls found