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

I just got done reading How can I watch for an active internet connection which is similar to what I need but the responses are too "basic".

I want to detect whether or not my internet connection is active by more reliable means than pinging a list of known web sites and looking for a response. That's a little too basic for what I need. I want it to check for error responses as well (Running Windows 98 through LAN environment through a D-Link router for DSL). I want it to give errors like Windows offers "cable disconnected" to see if anyone disconnected my computer from the LAN while I am away and errors of that type. Are these errors detectable in Perl? Sometimes my LAN just dies (no one knows just why) so that'd be nice to have a custom error for incase all wires are connected and it still doesn't work.

I know it's a long shot but is something of this sort possible? To not just verify whether or not you're connected to the internet or not but check for errors given from the operating system?

I was thinking it might be possible to run this script on my web site via a chron job but would it be possible to detect my specific system on the network if we all share the same IP?



"Age is nothing more than an inaccurate number bestowed upon us at birth as just another means for others to judge and classify us"

sulfericacid

Replies are listed 'Best First'.
Re: checking internet connection
by Mr. Muskrat (Canon) on Feb 03, 2005 at 03:16 UTC

    If you were running Windows NT, 2000, XP or 2003, you could use Win32::EventLog.

    use Win32::EventLog; $Win32::EventLog::GetMessageText = 1; my $eventlog = Win32::EventLog->new('System', $ENV{ComputerName}) or d +ie "Can't open System EventLog\n"; my %event; while ($eventlog->Read(EVENTLOG_BACKWARDS_READ| EVENTLOG_SEQUENTIAL_RE +AD, 0, \%event)) { if ( $event{EventType} == EVENTLOG_INFORMATION_TYPE && $event{Source +} eq 'Tcpip' ) { print $event{RecordNumber}, ' ', scalar localtime($event{TimeGener +ated}), ' ', $event{Message}, "\n"; last; # this example only shows the last } }

    I unplugged my network cable and ran it. X:\Code>eventlog.pl 4368 Wed Feb 2 21:11:23 2005 The system detected that network adapter \DEVICE\T CPIP_{FFFFFFFF-FFFF-FFFF-FFFF-FFFFFFFFFFFF} was disconnected from the network, and the adapter's network configuration has been released. If the network adapter was not disconnected, this may indicate that it has malfunctioned. Please contact your vendor for updated drivers.

    I plugged my network cable back in and ran it again. X:\Code>eventlog.pl 4369 Wed Feb 2 21:11:38 2005 The system detected that network adapter \DEVICE\T CPIP_{FFFFFFFF-FFFF-FFFF-FFFF-FFFFFFFFFFFF} was connected to the network, and has initiated normal operation over the network adapter.

    See also: Roth Consulting's EventLog.pl script.

Re: checking internet connection
by machinecraig (Monk) on Feb 02, 2005 at 20:12 UTC
    You should be able to scrape the contents of 'ipconfig /all', which should get a "media disconnected" message if your NIC gets unplugged.

    The comments in the other thread you mentioned are good though - especially about using an HTTP request instead of ping or traceroute.

      you'll need to check the actual windows version being run to determine which command line utility to run (if you're looking to go that route and care about compatibility)

Re: checking internet connection
by Grygonos (Chaplain) on Feb 02, 2005 at 20:03 UTC

    Windows can generate those type of errors because it directly interfaces with the hardware. From a strictly software perspective you're not going to be able to generate those kinds of errors without using a low-level interface to your NIC.