Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

RE: Net::Ping not working

by Shendal (Hermit)
on Sep 12, 2000 at 18:32 UTC ( [id://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( 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

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(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
        OK, if it's just a connect to 80, let's do this (again with less typing):
        use IO::Socket; for my $host(qw(www.slashdot.org www.deja.com www.perlmonks.org)) { IO::Socket::INET->new("$host:80") or warn "cannot connect to web of +$host\n"; }

        -- Randal L. Schwartz, Perl hacker

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (6)
As of 2024-03-28 14:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found