Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
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

In reply to Re: Please help me in goto statement by tcf03
in thread Please help me in goto statement by irah

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (2)
As of 2022-08-09 04:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?