Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

How can I watch for an active internet connection

by boat73 (Scribe)
on Dec 30, 2004 at 19:01 UTC ( [id://418347]=perlquestion: print w/replies, xml ) Need Help??

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

I would like a script to run that will watch for an active internet connection and take action when it is detected. Does anyone have any suggestions on this subject? I figure I could just run a ping but thought with all the wisdom out there someone might have a better idea. Thanks in advance!!!
  • Comment on How can I watch for an active internet connection

Replies are listed 'Best First'.
Re: How can I watch for an active internet connection
by PreferredUserName (Pilgrim) on Dec 30, 2004 at 19:07 UTC
    perl -MIO -le 'sleep 10 until IO::Socket::INET->new("www.google.com:80 +"); print "Action!"'
Re: How can I watch for an active internet connection
by elwarren (Priest) on Dec 31, 2004 at 00:15 UTC
    ActiveState Perl has Win32-IPConfig and Win32-IPHelper available via PPM. They will be able to tell you if your interface is up, whether you have a DHCP lease, whether there is a gateway on that interface (so you can guess that that's your internet connection.)

    You'll still need to verify that you can connect through the network connection you find, but this will be a good start. I would suggest either pinging your dns server or making an http header request to a web server that you think is reliable, or possibly the web server you want to work against.

    I say ping your dns server, because if you can't resolve names to addresses, you might not get very far anyway. Unless you're running a caching nameserver locally on your network or in your router.

    Many networks are blocking ICMP packets from ping these days, as well as traceroute packets, so that's why I suggest the http request. Do the net a favor and just do the HEAD request instead of grabbing the entire page.

    This should be enough to get you started. Good luck.
      Thanks for pointing me in the right direction.
Re: How can I watch for an active internet connection
by holli (Abbot) on Dec 30, 2004 at 19:20 UTC
    that is platform dependent.
    for windows you will need to check a registry key (not sure which, but i can find out if you want).
      Sorry, should have said I am using windowz. That would be great, thanks. So the reg entry actualy tells you if the connection is live, interesting.
        after rereading the thing i found that the registry-key is only true for dialup-connections.
        afaik, there is no way to detect wether the computer is connected via LAN.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://418347]
Approved by Old_Gray_Bear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (2)
As of 2024-04-19 19:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found