Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Network or no network???... That is the question!

by akm2 (Scribe)
on Jun 30, 2002 at 16:44 UTC ( [id://178390]=perlquestion: print w/replies, xml ) Need Help??

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

I have a unique situation where I need to run a Perl script in a Windows XP Professional CMD console window. The catch is, I need to determine if the network is connected each time I run the scipt. If yes, I need to to an extra step. If no, I need to skip that step. How do I instruct Perl to check network connection status?

Andrew Kenton Mitchell
Andrew@AndrewKMitchell.com

  • Comment on Network or no network???... That is the question!

Replies are listed 'Best First'.
Re: Network or no network???... That is the question!
by grinder (Bishop) on Jun 30, 2002 at 17:56 UTC
    Who cares about the network? All you want to know is whether you can reach the machine you're interested in, on the port you want. What better way to test than to see if you can communicate with it?

    my $s = IO::Socket::INET->new( PeerHost => '10.0.0.1', PeerPort => 80, # or whatever port you want Timeout => 15, Proto => 'tcp', ); if( $s ) { close $s; # it's ok } else { die $!; # it's not }


    print@_{sort keys %_},$/if%_=split//,'= & *a?b:e\f/h^h!j+n,o@o;r$s-t%t#u'
Re: Network or no network???... That is the question!
by dws (Chancellor) on Jun 30, 2002 at 17:07 UTC
    The catch is, I need to determine if the network is connected each time I run the scipt.

    "Network is connected" can over a lot of ground, and diagnosing the various ways that a network can be "down" can be difficult. If you constraint your requirements to mean "can I connect to the outside world," the problem gets simpler. Merely try to open a socket to a known IP address/port. Say, to a known DNS server. If you can't reach the DNS server, the DNS server itself could be down, but more likely the problem is with cables/routers/whatever.

      As to checking for an IP address, IIRC, on *nix it is often the 'ifconfig' command (check your man or info pages for the needed options), and on Windows (NT, 2K, and XP), the 'ipconfig' command (I can't recall if there is an equivalent for 9x).

      As to opening a connection to a known IP/port, you could also try pinging the default gateway as well, perhaps using the Net::Ping module.

        ifconfig/ipconfig will tell you what IP address you were given the last time you acquired a DHCP lease, but they won't tell you that a rat has gnawed through the cat5 leading out of the back of your box unless you invoke the option to renew your lease(s).

Re: Network or no network???... That is the question!
by little (Curate) on Jun 30, 2002 at 22:21 UTC
    Just to add one more I'd suggest using netstat with the -a switch, which is present on both *ix and windows systems, and so you can see your (possible and active) network connections also.

    Have a nice day
    All decision is left to your taste
Re: Network or no network???... That is the question!
by Courage (Parson) on Jun 30, 2002 at 16:51 UTC
    I can suggest may be not best way, but it works for many cases. Just check output of "ipconfig" command.

    Of course, it's only for IP network. I don't know whether exists Win32::* way of doing similar task -- this beeds extra investigations.

    Courage, the Cowardly Dog.

      I'll investigate. Don't think Win32 can do this as well as *nix but I'll check. :) Thank,

      Andrew Kenton Mitchell
      Andrew@AndrewKMitchell.com

Re: Network or no network???... That is the question!
by theorbtwo (Prior) on Jun 30, 2002 at 22:02 UTC

    Careful, BTW; most of these will cause some version or another of windows to automaticly connect or ask the user, if so configured. Parsing the output of ipconfig may be the best option... though it's trickier then it looks. For example, on this machine, if the only available conenction has the address 192.168.0.1, then there is no connection. On the one just to it's side, if the only connection has the address 192.168.0.2, there there /might/ be a connection to the outside world.


    We are using here a powerful strategy of synthesis: wishful thinking. -- The Wizard Book

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (6)
As of 2024-04-16 20:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found