Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

How do I tell if a remote site is up?

by Elliott (Pilgrim)
on Nov 11, 2003 at 17:27 UTC ( #306282=perlquestion: print w/replies, xml ) Need Help??

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

I use LWP::Simple to get responses from remote sites. If the site responds, all is well. If there is a DNS problem (e.g. the URL is wrong) all is also well - the get fails in a timely manner. But if the remote site is down and simply not responding, is there any way to tell?

A search on the monastery suggests it cannot be done with LWP - the script simply hangs awaiting a response - even if you adjust the timeout settings. Does anyone know any other way? (Or is the problem with Linux or Apache rather than LWP or Perl?)

Replies are listed 'Best First'.
Re: How do I tell if a remote site is up?
by Grygonos (Chaplain) on Nov 11, 2003 at 17:31 UTC

    try pinging the remote site before you call the  get method. It will tell you if the remote site is online.. not if any specific service is started.. but if the machine has an ip and is responding.

    Net::Ping can help you get it all together. Here is an idea to get you going. This doesn't work but its the general idea.

    #!/perl -w use strict; use Net::Ping; #Make object using default TCP my $p = Net::Ping->new(); #Construct an array of remote sites however you like #Read from a file or hardcoded etc. (file would be best) my @remote_sites = qw(; #Loop through the sites foreach(@remote_sites) { #pseudo-code here because I don't use LWP.. but #you understand what's going on right? attempt_LWP_Get if $p->ping($_); }

      If you don't get a reply from your ping, it doesn't mean the service is unavailable. Many routers and firewalls drop ping requests (and rightly so). If you do get a ping, you still don't know whether the remote site is alive. A router might respond to the ping.


      Thank you. That will definitely help but would not cover the problems I've had recently where one of my suppliers' sites has been up but their services have been failing intermittently.
        Are you sure its a software failure? and not possibly a problem with their hardware? Just something to consider. Dunno what services you are referring to.. but surely if its a semi-standard service there is a method to check for it on a local machine, via something like a mini-port-scan

Re: How do I tell if a remote site is up?
by sschneid (Deacon) on Nov 11, 2003 at 17:36 UTC
    Either ping it (using something like Net::Ping), or create a timeout wrapper around LWP:
    local $SIG{ALRM} = sub { die }; eval { alarm(10); # LWP request alarm(0); };
      Sounds great - now what am I doing wrong?

      #!/usr/bin/perl use LWP::Simple; print "Content-type: text/html\n\n<html><body bgcolor=white>\n"; local $SIG{ALRM} = sub { die "alarm\n" }; # NB: \n required eval { alarm 4; $content = get(" +d=36128"); alarm 0; }; if ($@) { die unless $@ eq "alarm\n"; # propagate unexpected errors # timed out print "timed out<BR>"; } else { # didn't print "succeeded<BR>"; } if($content) { print $content; } else { print "Durrr"; }

      It's still hanging.

      I haven't really understood how to use SIGALARM - I am just coding-by-numbers at the moment hoping that understanding will dawn.

        Ah.. I've tried it on a different server and it works perfectly. I wonder what the difference is.
Re: How do I tell if a remote site is up?
by barrd (Canon) on Nov 12, 2003 at 14:17 UTC
    Hi Elliott,
    You haven't actually stated what will happen (or what you want to have happen) if the remote site is actually 'down'.

    If you want notification by eMail (and lots of other diagnostic tools) then try Nagios formerly known as NetSaint. I recommended this before a few days ago and is a very good solution for this type of scenario.

    From the Nagios site:

    What is Nagios? An open source host, service and network monitoring program. Who uses it? Lots of people, including many big name companies and organizations.
    It's also written in perl... HTH
      I just want the script to be able to carry on without hanging and recognise that the remote site is down. In this context Nagios is a hammer to crack a nut but thanks for the info - I certainly have use for it in other contexts.
Re: How do I tell if a remote site is up?
by mce (Curate) on Nov 12, 2003 at 15:58 UTC
    use Net::Telnet f.e.
    use Net::Telnet; if ( my $t=new Net::Telnet(Port => 80, Host => "localhost", Timeout => + 5 )->open() ) { print "localhost is active"; $t->close(); };
    Or in shell,
    echo "."|telnet localhost 80
    This should do the trick
    Dr. Mark Ceulemans
    Senior Consultant
    BMC, Belgium

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://306282]
Approved by Grygonos
Front-paged by broquaint
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (4)
As of 2020-10-21 05:22 GMT
Find Nodes?
    Voting Booth?
    My favourite web site is:

    Results (212 votes). Check out past polls.