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

Hiya,

My friend Dave and I have been working on the following script, which has been designed to ping a server and feedback whether or not it is online.

Only trouble is that I can't get it working on my server. I dont know if this is because I have set the path to perl wrong or what, nor do I know how I am supposed to find out what the path is, because my ftp doesn't support the WHEREIS command. :(

If any of you can see a problem in my code below, please let me know at llydapdicter@yahoo.co.uk

Thanx in advance.

#!/usr/bin/perl #file by davidjp@cableinet.co.uk, llydapdicter@yahoo.co.uk use CGI qw(:standard); print header(), start_html("Server Ping Testing"), h1("Ping Results"); @host_array = ("irc.webchat.org","64.177.244.76"); use Net::Ping; $p = Net::Ping->new(); print "$host is alive.\n" if $p->ping($host); $p->close(); $p = Net::Ping->new("icmp"); foreach $host (@host_array) { print "$host is "; print "NOT " unless $p->ping($host, 2); print "reachable.\n"; print "<br>"; sleep(1); } $p->close(); $p = Net::Ping->new("tcp", 2); while ($stop_time > time()) { print "$host not reachable ", scalar(localtime()), "\n" unless $p->ping($host); sleep(300); } print end_html();

2001-03-19 Edit by Corion : Changed title

Replies are listed 'Best First'.
Re: Can't get my script working. Please help! :)
by dvergin (Monsignor) on Mar 19, 2001 at 02:13 UTC
    There are things in the flow of your script that appear to be remnants of several attempts to approach your task in slightly different ways. For example, $host is used early on before it is given a value. Toward the bottom, $stop_time is used without being given a value.

    You need some feedback from your script to help you sort things out. If you have telnet access to the server, run the script from the command line. The results may be instructive.

    If you do not have telnet access, you need to get the server to cough up messages to your web browser. Try:

    use CGI qw(:standard); # you already have this use CGI::Carp; use CGI::Carp qw(fatalsToBrowser); local $SIG{__WARN__} = \&Carp::cluck;
    Later, when things are better, you will likely want to remove these lines or comment them out.

    Also, use strict and include -w in your hash-bang line. They do wonders for helping you see where you may have slipped up.

    Having said all this, I have gone to the docs as you clearly have done, and done some (frustrated) playing around. Turns out that the protocol really does make a difference. 'udp' dies ungracefully in different ways on two different OS's. 'tcp' reports nobody home. 'icmp' reports the servers are responding. Here's a script I ran from the command prompt on a Linux machine.

    use Net::Ping; my @protocols = qw(icmp tcp); my @host_array = ("www.google.com", "irc.webchat.org", "64.177.244.76"); foreach my $proto (@protocols) { my $p = Net::Ping->new($proto); foreach my $host (@host_array) { print "With $proto, $host does "; print "NOT " unless $p->ping($host); print "respond\n"; } $p->close(); }
    Result:
    With icmp, www.google.com does respond With icmp, irc.webchat.org does respond With icmp, 64.177.244.76 does respond With tcp, www.google.com does NOT respond With tcp, irc.webchat.org does NOT respond With tcp, 64.177.244.76 does NOT respond
    Now here's the gotcha. I did this from the command prompt because you need root privilege to use the icmp protocol on a Linux machine. No good if you want to run this from a web page. And the tcp protocol option crashes on my Win98 machine.

    Yuck! No easy answers but I hope this helps you sort things out.

Re: Can't get my script working. Please help! :)
by Elgon (Curate) on Mar 19, 2001 at 11:26 UTC
    Just a small point: Some servers are set up not to respond to pings. Try pinging www.microsoft.com for example, you shouldn't get a result.

    A slightly surer way might be to send an HTTP head request and base your result on that.

    Elgon