Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

Re: Please help me in goto statement

by tcf03 (Deacon)
on Jun 05, 2009 at 17:25 UTC ( [id://768855]=note: print w/replies, xml ) Need Help??

in reply to Please help me in goto statement

You could do something like
#!/usr/bin/perl use strict; use warnings; use Net::Ping; my $down; my $host = $ARGV[0]; my $host2 = $ARGV[1]; my $rc = ( PING($host) == 0 ) ? "$host is up" : ( PING($host2) == 0 ) ? "$host2 is up" : "Neither $host or $host2 are reachable"; print "$rc\n"; sub PING { my $host = shift; my $p = Net::Ping->new("icmp"); return ( $p->ping($host) ) ? 0 : 1; }
Just want to note though that you could hit ports on each machine and it may be a bit more reliable than icmp.... To do that I use something like the following and you don't need to be root to run it ( assuming you are using a UNIX-ish OS...
From perldoc Net::Ping If the "icmp" protocol is specified, the ping() method sends an icmp echo message to the remote host, which is what the UNIX ping program does. If the echoed message is received from the remote host and the echoed information is correct, the remote host is considered reachable. Specifying the "icmp" protocol requires that the program be run as root or that the program be setuid to root.
#!/usr/bin/perl use strict; use IO::Socket::INET; use Getopt::Long; my @hosts = ""; my $timeout = "2"; #my $port = ""; my @ports = ""; my $opts = GetOptions( "host=s" => \@hosts, "port=s" => \@ports, "timeout=s" => \$timeout ); @hosts = split /,/, join( ',', @hosts ); die "usage: sp -h <host|IP> [ -h <host|IP> ] -p <port> [ -p <port> ]\n +" unless $hosts[1]; shift @ports; for my $host (@hosts) { next if $host eq ''; for my $port (@ports) { my $status = ( connection( $host, $port ) == 0 ) ? "Up" : +"Down"; my $line = sprintf(" %-20s %-5s %-1s", $host, $port, $stat +us); print $line . "\n"; } } ############## sub connection ############## { my $host = shift; my $tcp_port = shift; return ( IO::Socket::INET->new( Timeout => $timeout, PeerAddr => $host, PeerPort => $tcp_port, Proto => 'tcp' ) ) ? 0 : 1; } perl -h -h -p 80 80 Up 80 Up
"That which we persist in doing becomes easier, not that the task itself has become easier, but that our ability to perform it has improved."
  --Ralph Waldo Emerson

Log In?

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

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (5)
As of 2024-05-21 21:53 GMT
Find Nodes?
    Voting Booth?

    No recent polls found