Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: is there script to detect if remote machine is alive/reachable w/o using Net::Ping?

by grinder (Bishop)
on Jun 07, 2002 at 10:32 UTC ( #172478=note: print w/ replies, xml ) Need Help??


in reply to is there script to detect if remote machine is alive/reachable w/o using Net::Ping?

odd you should say that. I use almost exactly the same code and I have never been troubled with lengthy delays. Are you sure you don't have a problem with DNS name resolution (even though your example shows the use of numeric IP addresses)?

Here is the code I use. It is part of a CGI script, so there is a $q CGI object hanging around, and a few constant values for pretty colors, such as use constant PAINT_OK => '#008000' and so on.

sub test_socket { my $s = IO::Socket::INET->new( @_, Timeout=>2, Proto => 'tcp' ); if( $s ) { close $s; $q->font( {-color=>PAINT_OK}, 'ok' ); } else { $q->font( {-color=>PAINT_NOK}, $! ) } } my %tcp_probe = ( 'foo<br>Apache' => test_socket( PeerHost => '172.17.0.2', Pe +erPort => 80, ), 'foo<br>Lotus' => test_socket( PeerHost => '172.17.0.2', Pe +erPort => 1352, ), 'bar<br>Apache' => test_socket( PeerHost => '172.17.0.3', Pe +erPort => 80, ), 'bar<br>Lotus' => test_socket( PeerHost => '172.17.0.3', Pe +erPort => 1352, ), 'example<br>Apache' => test_socket( PeerHost => 'www.example.com', + PeerPort => 80, ), 'example<br>Domino' => test_socket( PeerHost => 'www.example.com', + PeerPort => 8000, ), 'example<br>Lotus' => test_socket( PeerHost => 'www.example.com', + PeerPort => 1352, ), 'Web<br>Proxy' => test_socket( PeerHost => '172.17.0.9', + PeerPort => 8080, ), 'baz<br>Sybase' => test_socket( PeerHost => '172.17.0.1', Peer +Port => 4100, ), 'baz<br>Samba' => test_socket( PeerHost => '172.17.0.1', Peer +Port => 139, ), ); print $q->h3('Mission Critical'), $q->table( $q->TR( $q->th( {-width=>60, -valign=>'bottom', -bgcolor=>'#fff0e0'}, [ sort keys %tcp_probe ] ) ) . $q->TR( $q->td( {-width=>60, -align=>'center'}, [ map { $tcp_probe{$_} } sort keys %tcp_probe ] ) ) );

The only other thing I could suggest would be to set an alarm of a second or two, an alarm handler and eval the code, in order to forcibly cancel the code. But if it's blocking at a low enough level, alarm won't necessarily work either, I've had that happen in the past.


print@_{sort keys %_},$/if%_=split//,'= & *a?b:e\f/h^h!j+n,o@o;r$s-t%t#u'


Comment on Re: is there script to detect if remote machine is alive/reachable w/o using Net::Ping?
Select or Download Code
Re: Re: is there script to detect if remote machine is alive/reachable w/o using Net::Ping?
by Anonymous Monk on Jun 07, 2002 at 20:31 UTC
    tnx guys, ive tried what GRINDER recommended and heres what happened to my code:
    use IO::Socket; sub doit { $ip1="210.22.106.31"; $host="$ip1"; $port="23"; $fnd=""; $pong=`ping -c 3 $host`; $fnd=($pong=~m/(..)%/gi); $xcd=$1; if ($xcd!=00) { $SIG{ALRM} = \&timed_out; eval { alarm (3); $remote = IO::Socket::INET -> new ( Proto => "tcp", PeerAddr => $host, PeerPort => $port, Timeout => 2 ) or die "." ; alarm (0); }; sub timed_out { die "."; } $| = 1; if ($remote) { close $remote; print "Socket ready...\n"; } else { print "Try again...\n"; } } else { print "Pinged timed out!\n"; }
    This doenst seem to work? It also hangs just as before? sigh... got any substitute for EVAL which works for Sockets? Thank you all for helping me. I really dont know what to do next? Lovelots and *mwah* *mwah* *mwah*, Cherry.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://172478]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (7)
As of 2014-11-23 06:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (128 votes), past polls