CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Web Server test

 



bobbyperl
Novice

Apr 18, 2006, 6:50 AM

Post #1 of 23 (4782 views)
Web Server test Can't Post

Hi im having a problem with my win 32k server it keeps crashing and i need to find away of knowin when it has without having to manualy ping the server .

Is there away in perl i can get it to check my web server and then send me back some results or bring me up a warning message to tell me it has failed and help or advice would be great am really stuck on this one and need to come up with something quick


davorg
Thaumaturge / Moderator

Apr 19, 2006, 1:00 AM

Post #2 of 23 (4775 views)
Re: [bobbyperl] Web Server test [In reply to] Can't Post


Code
#!/usr/bin/perl 

use strict;
use warnings;

use LWP::Simple;

my $host = 'http://your_host.com/";

unless (head($host)) {
print "Seems to be a problem with $host\n";
}


--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


bobbyperl
Novice

Apr 19, 2006, 1:02 AM

Post #3 of 23 (4774 views)
Re: [davorg] Web Server test [In reply to] Can't Post

Hi my firend could u explaine what this does


davorg
Thaumaturge / Moderator

Apr 19, 2006, 1:07 AM

Post #4 of 23 (4771 views)
Re: [bobbyperl] Web Server test [In reply to] Can't Post

What bit don't you understand? Did you try reading the documentation for LWP::Simple?

LWP is the WWW library for Perl. It enables you to write HTTP client programs in Perl. LWP::Simple is the simple procedural interface to LWP.

HEAD is a simple HTTP request that returns basic data from an HTTP request. LWP::Simple has a "head" function which implements this call. In scalar context it returns true or flase depending on whether the request was successful.

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


bobbyperl
Novice

Apr 19, 2006, 1:16 AM

Post #5 of 23 (4770 views)
Re: [davorg] Web Server test [In reply to] Can't Post

Well i dont really understand perl that well am just learning but copied the scipt into a text exditor and changed the host to www.yahoo.com and it just said therre was an eroor in line 1


bobbyperl
Novice

Apr 19, 2006, 1:21 AM

Post #6 of 23 (4768 views)
Re: [bobbyperl] Web Server test [In reply to] Can't Post

Got ti working tahnk you


davorg
Thaumaturge / Moderator

Apr 19, 2006, 1:24 AM

Post #7 of 23 (4768 views)
Re: [bobbyperl] Web Server test [In reply to] Can't Post

There's a typo on line 8. The quotes are unbalanced. They should both be single (or double) quotes.

And you'll need to adjust the shebang line to reflect your local Perl environment. If you're getting an error on line 1 then that's probably the problem. As I know nothing about your Perl installation, I can't help you there.

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


bobbyperl
Novice

Apr 19, 2006, 1:32 AM

Post #8 of 23 (4767 views)
Re: [davorg] Web Server test [In reply to] Can't Post

Hey man thanks ,m no probs ok i got this working and i understand whats going on .

Now could i get it to check multiple servers ?

by adding something like

my $host = "http://www.None Existing Domain.com/";

my $host2 = "http://balh.com

my $host3 = "http://host3.com

and down here have something like

unless (head($host)($host2)(host3)

print "Seems to be a problem with $host\n"; }

Or something along thoes lines .

Cheres for your help man


davorg
Thaumaturge / Moderator

Apr 19, 2006, 1:39 AM

Post #9 of 23 (4765 views)
Re: [bobbyperl] Web Server test [In reply to] Can't Post

Put the list of hosts into an array and then iterate across the array (using "foreach") calling "head" for each element in the array.

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


bobbyperl
Novice

Apr 19, 2006, 2:07 AM

Post #10 of 23 (4756 views)
Re: [davorg] Web Server test [In reply to] Can't Post

Hye my friend sorry i no this is wrong and its better for me to learn but is this any where near

what i should be do

#!/usr/bin/perl

$host1 = www.domain4.com

$host2 = www.domain3.com

$host3 = www.domain2.com

$host4 = www.domain1.com

use strict;
use warnings;
use LWP::Simple;
@testall = ("$host1", "$host2", "$host3", $host4);
unless (head(@testall)) {
print "Seems to be a problem with @testall\n"; }


d1zz13
User

Apr 19, 2006, 2:39 AM

Post #11 of 23 (4746 views)
Re: [bobbyperl] Web Server test [In reply to] Can't Post

As Dave mentioned, you need to test each of the hosts inside the array individually, using a foreach is ideal for this situation, try something like this:

Code
#!/usr/bin/perl 

use strict;
use warnings;
use LWP::Simple;

@hosts = ("www.domain4.com","www.domain3.com","www.domain2.com","www.domain1.com");

foreach $host(@hosts){
unless (head($host)){
print "There seems to be a problem with $host\n";
}
}

Also try and use code tags when posting to the forum, it makes it easier to read.

Regards
Rich

Regards
Rich

If it aint broke then don't try to fix it


bobbyperl
Novice

Apr 19, 2006, 2:45 AM

Post #12 of 23 (4745 views)
Re: [d1zz13] Web Server test [In reply to] Can't Post

Extremly thankful for ur help man ur a star u saved me hours of RDPing into server and calling up tech support .



If ur ever stuck with shit although im not useful i no a few things speciallly linux cheers .




Sorry one last thought would this work with ips ? instaed of domains . Cheers


(This post was edited by bobbyperl on Apr 19, 2006, 2:47 AM)


davorg
Thaumaturge / Moderator

Apr 19, 2006, 2:47 AM

Post #13 of 23 (4743 views)
Re: [bobbyperl] Web Server test [In reply to] Can't Post

"head" can only act on one URL at a time. You need to iterate across your array to get each element in turn. As I said before - you need to use "foreach".

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


bobbyperl
Novice

Apr 19, 2006, 3:00 AM

Post #14 of 23 (4738 views)
Re: [davorg] Web Server test [In reply to] Can't Post

Rich that code didnt work as dave said i think i need to change the

think to use foreach as dave saiud but i dont understand where this would go would this work

We have foreach in there at them moment so where would i change this ?

#!/usr/bin/perl

use strict;
use warnings;
use LWP::Simple;

@hosts = ("www.domain4.com","www.domain3.com","www.domain2.com","www.domain1.com");

foreach $host(@hosts){
print "There seems to be a problem with $host\n";
}
}


is this right .



As i said im so sorry new to this perl .


d1zz13
User

Apr 19, 2006, 3:05 AM

Post #15 of 23 (4734 views)
Re: [bobbyperl] Web Server test [In reply to] Can't Post


In Reply To
Sorry one last thought would this work with ip's ? instead of domains


A valid question, and one that's not mentioned in the documentation explicitly, however the term URL (Uniform Resource Locator) refers to IP addresses as well as domain names.

In my eyes, http://127.0.0.1 is as much a valid URL as http://localhost

Rich

Regards
Rich

If it aint broke then don't try to fix it


bobbyperl
Novice

Apr 19, 2006, 3:10 AM

Post #16 of 23 (4732 views)
Re: [d1zz13] Web Server test [In reply to] Can't Post

Ok thank you

Um still need some help with the foreach if any one could help .


d1zz13
User

Apr 19, 2006, 3:22 AM

Post #17 of 23 (4729 views)
Re: [bobbyperl] Web Server test [In reply to] Can't Post


In Reply To
Rich that code didnt work, as dave said i think i need to change to use foreach


The following works fine for me:

Code
use LWP::Simple;  

@hosts = ("http://www.google.co.uk","http://www.dfhasjkdfhjkasf.com");

print "Content-type: text/html\n\n";

foreach $host(@hosts){
if (head($host)){
print "$host ... OK<br>\n";
}
else {
print "$host ... Error<br>\n";
}
}


The above is an example of the code running on my web server and it produces the expected result. Note that I've had to add the mime type as this is running as a CGI script.

Regards
Rich

If it aint broke then don't try to fix it


bobbyperl
Novice

Apr 19, 2006, 3:32 AM

Post #18 of 23 (4725 views)
Re: [d1zz13] Web Server test [In reply to] Can't Post

hey man that code works sweet , Nice cheers and sorry for being dumb but it dosetn work with ips for some odd reason .



Cheers


davorg
Thaumaturge / Moderator

Apr 19, 2006, 3:36 AM

Post #19 of 23 (4723 views)
Re: [bobbyperl] Web Server test [In reply to] Can't Post

Ok. Look, I know that you're new to Perl, but you need to learn to track down problems by yourself and not just post "it doesn't work" messages here.

It would be a very good idea to get familiar with the perldoc web site which includes all of the (very extensive) Perl documentation.

Particularly useful to you currently would be the perldiag man page which includes detailed descriptions of all of the error messages you can get from a Perl program.

I can see four obvious problems with the current version of your program.

1/ The URLs in your @hosts array are not full URLs. They are missing the protocol definition.

2/ As you have "use strict" in your program, you need to declare all of your variables (probably using "my").

3/ You are no longer calling "head" anywhere. So you're not actually checking anything :)

4/ You have one too many closing braces.

We're happy to help you solve your problems, but we won't just write your programs for you. You need to do some work yourself.

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


d1zz13
User

Apr 19, 2006, 3:43 AM

Post #20 of 23 (4722 views)
Re: [bobbyperl] Web Server test [In reply to] Can't Post


In Reply To
hey man that code works sweet , Nice cheers and sorry for being dumb but it doesn't work with ip's for some odd reason


It does work with IP's, you need to include the protocol as per Dave's post above. Use 'http://127.0.0.1' as opposed to just '127.0.0.1'

Rich

Regards
Rich

If it aint broke then don't try to fix it


bobbyperl
Novice

Apr 19, 2006, 4:16 AM

Post #21 of 23 (4715 views)
Re: [d1zz13] Web Server test [In reply to] Can't Post

hey man this is so wierd that it works on some ips and not others like i cant do localhost but i can do like yahoo and things . I cant do a few of my server ips either but i can ping them any reaosn why perl does this.


d1zz13
User

Apr 19, 2006, 5:29 AM

Post #22 of 23 (4712 views)
Re: [bobbyperl] Web Server test [In reply to] Can't Post


In Reply To
I cant do a few of my server ips but i can ping them, any reaosn why perl does this.


No idea why it works for some and not others, but I'd be willing to bet money that it's not Perl that's the problem Wink

Actually thinking about it, http://localhost won't work unless you're running a web server.

Rich

(This post was edited by d1zz13 on Apr 19, 2006, 5:36 AM)


bobbyperl
Novice

Apr 19, 2006, 5:33 AM

Post #23 of 23 (4709 views)
Re: [d1zz13] Web Server test [In reply to] Can't Post

i figured it out i think because the ips are not actually web pages they are for other things it dosetn work

but for my ones that server web pages this has worked fine.



But odd how i cant do localhost

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives