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

I'm running on Win98 with a dial-up connection to the ISP. Is there a simple way to detect from within perl if there is currently an active dial-up connection?

Replies are listed 'Best First'.
(jcwren) Re: Detecting dial-up connections
by jcwren (Prior) on Jun 09, 2000 at 23:58 UTC
    Sure. You can execute the command 'ipconfig' and parse the output with the script. Another more kludgy way is if you do NOT have dial-on-demand set, you can ping a known address periodically and check the result.

    But allow me to ask an exploratory question: Are you trying to determine if the link is down before you go off to a site, or are you attempting something else? Because it's Windows, there might be a better way to do it. If you can clarify what you're trying to do, we might be able to come with a more 'creative' solution.
      I'm trying to determine if the link is up and running before I try and retrieve something. The idea is that the script spends most of its time asleep and wakes up every x minutes. If it can see a connection it goes and gets something (actually some weather data from a local airport) otherwise it sleeps again.
        have you tried somthing like
        $minuits_to_wait = '5'; $last = my $last2 = 0; while (1) { if ((time - $last) > 60 * $minuits_to_wait ) { $ret = sometypeof-ping-routine(); if ($ret eq '1') { get_somthing(); } elsif ($ret eq '0') { print "Not connected\n"; } } sleep(60 * $minuits_to_wait); }
Re: Detecting dial-up connections
by Corion (Patriarch) on Jun 10, 2000 at 00:02 UTC

    If you have auto-dial set to off, you can simply ping the gateway machine, using Net::Ping. If the ping comes back, you are connected. The Cable Modem Check program by Ozymandias does more or less what you want then - but it uses the ping command which is available under Win32 as well but has slightly different output. The program also tries to reconnect the cable modem, which you don't want to do either. But it should be easy to modify the script to suit your needs.

    The problem starts if auto-dial is enabled. Then that ping will connect you, which is what you wanted to avoid in the first place. I haven't found a good way yet to detect a modem connection. You could try to open COM2 or whatever port your modem is connected to, and if an error occurs, you "guess" that you are connected, you can then try the above ping.

    Update: KM posted the method to go by if you want to open the serial port(s). And jcwren posted the ipconfig trick, which will show you a NDISWAN adapter for the remote connection.

Re: Detecting dial-up connections
by KM (Priest) on Jun 09, 2000 at 23:57 UTC
    I am not sure if it will do this or not (I haven't personally used it) but you may want to look at Win32::SerialPort which has Win32API::CommPort. I suppose you could also open a pipe or something to COM2 (which you can use as a filehandle just like STDOUT or STDERR) to see what it returned.

    Cheers,
    KM

Re: Detecting dial-up connections
by Anonymous Monk on Jun 10, 2000 at 02:15 UTC
    <Previously unmentioned> There is some registry value which is true if the computer is currently connected to the Internet. I would actually recommend searching around a few VB sites (in the 'Tips and Tricks' section). Once you find out the registry ID, key or whatever it's called, there is some module in the Win32:: hierarchy that lets you look at its value.

    So, it could be as simple as:

    if ($whatever_key) { print "I'm online!\n"; }
    </Previously unmentioned>
(jcwren) RE: Detecting dial-up connections
by jcwren (Prior) on Jun 10, 2000 at 04:08 UTC
    Here's a little more information:

    Goto here and search for Q242558 in the search box. This is an article using 'C' to determine if you're connected to the net or not. Good foundation information here.

    Win32::Internet.pm (click on search CPAN when you get to the window) is basically an interface into the above, but seems to lack the 'InternetGetConnectedState' call. I haven't written any API interface modules, but as much as this module does, it should be easy to hack it in (and it you do, make sure to propagate the changes back to the author. Can't imagine why he left it out).

    This module Win32::RASE (click on search CPAN when you get to the window) isn't what you're looking for, but is pretty cool for managing your connection lists. It might have some other applicability for you in the future (Or not, if you switch to Linux!)

    Hope that helps a little.

    --Chris

      Win32::RASE makes checking the internet connection status possible, if one digs a bit deeper. I've thought about doing a "real" program for this, but now I see that Perl already has the necessary bindings for connection checking :).

      To use Win32::RASE to check the internet connection, I'd do something like the (untested) following :

      use strict; use Win32::RASE; my (%connections, $entry, $hrasconn); %connections = RasEnumConnections(); foreach $entry (keys %connections) { my $status; $hrasconn = $connections{$entry}; $status = RasGetConnectStatus( $hrasconn ); if ($status == RASCS_DeviceConnected) { print "Computer is online via $entry\n"; }; };
Re: Detecting dial-up connections
by lhoward (Vicar) on Jun 10, 2000 at 00:00 UTC
    If you dial-up connection isn't configured to "dial-on-demand" (i.e. establish a connection automatically whenever there is traffic to the internet) you could ping a few known internet hosts and if any of them respond you can safely assume that you are conneted to the internet. This approach will work independent of OS.

    I image there is some funky registry entry or something you can check, maybe look for a routing table entry out the PPP interface. But I almost never touch Windows, so I can't vouch for a more internal method being available.

      I use dial on demand so I don't want to initiate a connection if there isn't one already. I was trying to avoid (for the obvious reasons) any complicated registry manipulation.
Re: Detecting dial-up connections
by Odud (Pilgrim) on Jun 10, 2000 at 01:05 UTC
    Thanks to everyone for some useful and thought provoking suggestions. I'll try these out and see which fit best.