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

Ping a list of servers

by Kickstart (Pilgrim)
on Oct 18, 2002 at 22:39 UTC ( [id://206452]=perlquestion: print w/replies, xml ) Need Help??

Kickstart has asked for the wisdom of the Perl Monks concerning the following question:

Hi all PMs...I have the following start to a script to ping a bunch of servers on my internal network. All of the names are currently pingable on the command line, but the script always returns the fact that they don't return ping requests. What am I doing wrong?

Thanks!

#!/usr/bin/perl -w use strict; use Net::Ping; # Var declaration my ($host, $x, $res); my $output = ''; # Servers that should be pingable at all times my @ext_servernames = qw( exchange milton border sequel faxserver troll vindex waifer ); sub ping_hosts { foreach $host (@ext_servernames) { my $p = Net::Ping->new; my $res = $p->ping($host); $output .= "Unknown host $host\n" unless defined $res; if (!$res) { $output .= "$host doesn't respond to ping requ +ests!\n"; } else { $output .= "$host is fine.\n"; } } } ping_hosts; # DEBUG print $res; print $output;

Replies are listed 'Best First'.
Re: Ping a list of servers
by grep (Monsignor) on Oct 18, 2002 at 22:58 UTC

    Couple of things to look at:

  • You did not specify ICMP (UDP is default, but ping uses ICMP)   '$p = Net::Ping->new("icmp");'
  • To use ICMP you must be 'root' or have appropriate privledge to send ICMP packets
  • from the Net::Ping docs

    You may choose one of four different protocols to use for the ping. The "udp" protocol is the default. Note that a live remote host may still fail to be pingable by one or more of these protocols. For example, www.microsoft.com is generally alive but not pingable...

    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.



    grep
    Mynd you, mønk bites Kan be pretti nasti...
      Thank you...that did the job (I guess I can't take examples from perlmonks.org as perfect!)!

      Kickstart

Re: Ping a list of servers
by mojotoad (Monsignor) on Oct 18, 2002 at 23:00 UTC
    Command line ping runs as suid root -- this is required when using the icmp protocol, normal ping.

    Net::Ping, by default, utilizes udp to attempt a connection to the echo port on the remote host. You may also attempt tcp connections to the echo service port.

    When hosts are reported as "not returning ping requests" for your script, it means they aren't running the echo service.

    In order to use icmp you will need to run your script as suid root -- otherwise you'll have to wrangle with return codes from the command line ping using system calls or backticks.

    By the way, if there's a large number of hosts and a significant portion tend to be down, you might consider using something like merlyn's Parallel::ForkManager to ping hosts in parallel batches.

    Matt

    Update: after reading the docs, switched udp and tcp in my statements regarding the defaults.

Re: Ping a list of servers
by drat (Hermit) on Oct 19, 2002 at 13:12 UTC
    In the spirit of TMTOWTDI, you might want to check out Net::Ping::External on CPAN. I've looked at it, but haven't tried it yet (as a replacement to `/usr/sbin/ping` approach) so YMMV. It attempts to provide a standard interface to each platform's ping command.

    -drat

    "That is the best engineering, not which makes the most splendid, or even the most perfect, work, but that which makes a work that answers the purpose well, at the least cost"
    --Ashbel Welch, President ASCE 1882
Re: Ping a list of servers
by traveler (Parson) on Oct 19, 2002 at 17:26 UTC
    Just FYI, there is no need for the Net::Ping->new to be in the loop. It can be outside the foreach.

    --traveler

Re: Ping a list of servers
by grantm (Parson) on Oct 19, 2002 at 18:51 UTC

    You might also like to take a look at the fping utility. It's designed to do what you're after and you can easily call it from Perl with backticks.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://206452]
Approved by Aristotle
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (5)
As of 2024-04-25 14:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found