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

RE: Net::Ping not working

by Shendal (Hermit)
on Sep 12, 2000 at 18:32 UTC ( #32110=note: print w/replies, xml ) Need Help??

in reply to Net::Ping not working

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( +); 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"; }


Replies are listed 'Best First'.
RE: RE: Net::Ping not working
by merlyn (Sage) on Sep 12, 2000 at 18:35 UTC
    Ewww. Wayyy too much typing:
    use LWP::Simple; for my $host(qw( { 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, 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.


      e-mail jcwren
        OK, if it's just a connect to 80, let's do this (again with less typing):
        use IO::Socket; for my $host(qw( { IO::Socket::INET->new("$host:80") or warn "cannot connect to web of +$host\n"; }

        -- Randal L. Schwartz, Perl hacker

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (1)
As of 2021-10-26 22:11 GMT
Find Nodes?
    Voting Booth?
    My first memorable Perl project was:

    Results (91 votes). Check out past polls.