http://www.perlmonks.org?node_id=768855


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 sp.pl -h google.com -h yahoo.com -p 80 google.com 80 Up yahoo.com 80 Up
Ted
--
"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