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

Any idea if there is a simple script out there or .pm i could use to check to see if a server is online and direct it to a certain webpage if it is or another if it isnt? Im not too familiar with modules so any help is greatly appreciated!

Replies are listed 'Best First'.
Re: Redirection
by blaze (Friar) on Apr 14, 2003 at 21:50 UTC
    you could use LWP::Simple to check if its up or not
    #!/usr/bin/perl -w use strict; use LWP::Simple; my $website = 'http://www.yahoo.com'; if(head($website)){ #its up } else { #its not }
    hth,
    Robert
      This is a good technique but remember that some hosts do not return head requests at all and others don't return requests made by the generic agent name in LWP::Simple. It's not a long list of hosts but it happens. So, if you're having problems, also try a get() and if that's still not it, try LWP::UserAgent and define your own agent name.

      The previous example is much easier though.

        I have just spoken to my webhoster (im just using Telewest's Advanced Web Service) and they dont have the perl module installed and it doesnt look like they are going to get it installed...is there any way i can use the module code in the actual orginal script, ie merge the two into one .pl script? (i presume there is just with my attempts it came up with a 500 error)
      thanks that looks pretty easy to use !!! just got work out how to install the modules ?! or is it installed ?! hmm i think ill find that out for myself much more interesting!! thanks again
Re: Redirection
by crenz (Priest) on Apr 14, 2003 at 21:49 UTC

    The status check can be done using e.g. Net::Ping. How to do the redirection depends on what webserver you use and whether you want to redirect just one page or a whole bunch of pages.

    Update: Here's a quick&dirty script for the first case:

    use Net::Ping; my $host = "www.server.com"; my $alive_url = "http://www.server.com/somepage"; my $dead_url = "http://www.backupserver.org"; my $p = Net::Ping->new(); my $url = $p->ping($host) ? $alive_url : $dead_url; $p->close(); print <<EOT; Status: 302 Moved Temporarily Content-Type: text/html Location: $url <p>Go to <a href="$url">$url</a>.</p> EOT
      I wouldn't advise the use of ICMP echo requests to test the availablility of the HTTP protocol on a server. This will fail under some circumstances where the website is available, leading to unpredictable results.

      (although you did say quick and dirty ;-)

      --dug