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

I'm using V 2.02 of Net::Ping, but for some reason nothing will work. I understand that 'icmp' pings require UID 0 (root) (someone told me that Net::Ping sends raw packets, hence the need for root). But, I can't get TCP or UDP pings to work either.

I thought it might be some kind of permissions issue, but the test script runs with my UID (i've watched it in the process list), and i've been able to ping the boxes (both with backticks and from ping in the shell - same thing, i know - so it's not a question of permissions.

or is it? here's the code (straight outta the documentation):

use Net::Ping; my @host_array = qw( www.slashdot.org www.deja.com www.perlmonks.org +); my $proto = 'tcp'; my $def_timeout = '40'; my $bytes = '52'; my $p = Net::Ping->new($proto, $def_timeout , $bytes); foreach my $host (@host_array) { print "$host is "; print "NOT " unless $p->ping($host, 2); print "reachable.\n"; } $p->close();
and when i run it:
bclarkso-sun:/users/bclarkso/snips --> perl -w ping.pl www.slashdot.org is NOT reachable. www.deja.com is NOT reachable. www.perlmonks.org is NOT reachable.

Replies are listed 'Best First'.
Re: Net::Ping not working
by tye (Sage) on Sep 12, 2000 at 02:35 UTC

    TCP and UDP pings only work to boxes that are set up to offer the "echo" services. This is becoming an increasingly rare configuration.

            - tye (but my friends call me "Tye")
      is there some substitute, different way? or am in stuck w/ backticks and/or system calls?

        Well, you could install a decent operating system. Windows NT will let you do ICMP pings without having to be root. (: [ Sorry, I get sick of this kind of stupid and useless response when things work the other way. ]

        You can use a set-UID Perl script.

        You would probably have good luck "pinging" things that you know the destination provides. For example, "ping" port 80 of a webserver by asking for headers using LWP::Simple. "Ping" the telnet or SMTP or FTP port of servers that provide those services.

        If you don't have a firewall in the way, you can probably also do TCP "pings" to ports that don't provide a service. A "connection refused" error means that the site is up, heard you, and doesn't offer what you were asking for. Firewalls tend to be configured to remove this helpful stuff in favor of silence. :(

                - tye (but my friends call me "Tye")
RE: Net::Ping not working
by Shendal (Hermit) on Sep 12, 2000 at 18:32 UTC
    Assuming from your host_array that you're looking at websites, I also gather that you're interested if the web services are available. Since, as has been previously described, ping services are not always available, and ping doesn't really answer the question "Is the website accessible?", I'd recommend attempting a connection to port 80 on each box, which would at least tell you that something (hopefully a webserver) is listening.

    Here's the code:
    #!/usr/bin/perl -w use strict; # always use Socket; my @host_array = qw( www.slashdot.org www.deja.com www.perlmonks.org +); my $proto = 'tcp'; my $port = 80; foreach my $host (@host_array) { # open the socket socket(SOCK, PF_INET, SOCK_STREAM, getprotobyname($proto)) or die "Unable to create socket!\n"; # connect to remote site my $isUp = connect(SOCK, sockaddr_in($port, inet_aton($host))); # print what we found out print "$host is "; print "NOT " unless $isUp; print "reachable.\n"; }

    Cheers,
    Shendal
      Ewww. Wayyy too much typing:
      use LWP::Simple; for my $host(qw(www.slashdot.org www.deja.com www.perlmonks.org)) { get "http://$host/" or warn "cannot get top page of $host\n"; }

      -- Randal L. Schwartz, Perl hacker

        Depends on how fast your modem is, and how big the home page is. If he starts hitting SlashDot on a 2400 baud modem, and is checking 4 times an hour, that's way too much useless traffic, and simply contributes to useless net-gestion.

        And, if any one cares, it skews the hit counters on the pages. Let's do that to www.stonehenge.com, and suddenly you may find that it *appears* that thousands more people are interested. Also fills up web logs uselessly.

        The socket code may be a few more lines to type, but it cuts down on net traffic (one, vs possible dozens of connections, for each image on a page), doesn't inflate hit counters, and is more net-responsible.

        Frankly, I'm a little surprised you let that slip by.

        --Chris

        e-mail jcwren
Re: Net::Ping not working
by BastardOperator (Monk) on Sep 12, 2000 at 04:44 UTC
    You could always just start doing what you're trying to do and set up and alarm handler.
    $SIG{ALRM} = sub { die "timeout" }; eval { alarm(300); # do stuff here alarm(0); }; if($@) { if($@ =~ /timeout/) { # # Call our alrm_hdlr subroutine, we were hung up # alrm_hdlr(); } else { alarm(0); die; } } # continue on, manipulate data, etc... sub alrm_hdlr { # Handle the fact that we got hung up }