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' | [reply] [d/l] |
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.
| [reply] |
|
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.
| [reply] |
|
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).
| [reply] |
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 | [reply] |
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. | [reply] |
|
| [reply] |
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
| [reply] |