Need Help with Perl Scripting Issue.

The Perl script runs fine without any error from command line. The script executes an external command and prints the output on a web page. But somehow when I call it from URL it does not work. Any help will be appreciated. Here is the script:
--test.pl

#!/usr/bin/perl
#
use warnings;
use strict;
use Socket;

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

print <<ENDOFHTML;
<HTML>
<HEAD>
<TITLE>Test Page</B></CENTER></TITLE>
<BODY background="/icons/page-background.png">
<body text="#808080">
<IMG SRC="/icons/test.gif" width="142" height="60" align="left">
<h1 align="center" font size=1> Netops-DashBoard</center></h1>
<br>
<br>
ENDOFHTML

open(SMP, "/usr/local/bin/sshpass -p test ssh test.com /usr/contrib/bin/serverinfor -b |") || die "Failed: $!\n";

while ( <SMP> ) {
chomp;
my $line = $;
print "$
\n";
print "<br>\n";
} # while

print "</BODY>";
print "</HEAD>";
print "</HTML>";

-----The script does not work when we call from : http://www.xxx.xxx/test.pl

In the sense that the While Loop does not get executed.....

I bet your server's running under a different user, most likely www-data. So in that case your server would be trying to log in as user www-data @ test.com.

sudo su www-data
sshpass -p test ssh test.com /usr/contrib/bin/serverinfor -b

That is true , since httpd is ruuning as apache user and that is why we are using sshpass with the another user-id and password......I have tried giving a wrong password and the error log says invalid password. Hence the different user-id is not the issue.

Is the file marked executable?

Continuing from ikon... is it executable for 'others' (the third set of owner/group/others)? Especially if you are running this from the web - assume that to fall into the 'others'.

By not work, does it start-up?
If so, then I might be thinking of file locations/directories and their related settings.

Yes the file permissions are 755

Have tried with root user-id for the remote server and the result is the same. Hence its not the permission issue.

So what happends when you call the script? Anything? Anything written to the error log?

Do you have ScriptAlias setup for the directory the perl script is in? Generally I put perl/cgi scripts in cgi-bin directory.

Here is the command:

replace:open(SMP, "/usr/local/bin/sshpass -p test ssh test.com /usr/contrib/bin/serverinfor -b |") || die "Failed: $!\n";

By:open(SMP, "/usr/local/bin/sshpass -p test ssh test.com -l root /usr/contrib/bin/serverinfor -b |") || die "Failed: $!\n";

Does it start-up?
Do you see that initial "Print" mesaage?

No nothing is written into the error log and that is why am so confused. Here is the output of the access log:
10.10.10.10 - - [11/Dec/2008:19:26:37 +0000] "GET /icons/page-background.png HTTP/1.1" 304 - "http://10.10..10.1/cgi-bin/test2.pl" "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30; .NET CLR 3.0.04506.648)"

Yes I do the see inital print Message on the browser and then its Blank page

if you add -w does it give you more errors?

#!/usr/bin/perl -w 
.
.

No, since its not generating any error......I restarted httpd and here is the output of the error log, after I ran the URL.......

tail -f /etc/httpd/logs/error_log
[[Thu Dec 11 19:34:37 2008] [notice] caught SIGTERM, shutting down
[Thu Dec 11 19:34:37 2008] [notice] suEXEC mechanism enabled (wrapper: /usr/sbin/suexec)
[Thu Dec 11 19:34:38 2008] [notice] Digest: generating secret for digest authentication ...
[Thu Dec 11 19:34:38 2008] [notice] Digest: done
[Thu Dec 11 19:34:38 2008] [notice] mod_python: Creating 4 session mutexes based on 256 max processes and 0 max threads.
[Thu Dec 11 19:34:38 2008] [notice] Apache/2.2.3 (Red Hat) configured -- resuming normal operations

Okay on further investigation it looks like the following command is not being executed:

:open(SMP, "/usr/local/bin/sshpass -p test ssh test.com -l root /usr/contrib/bin/serverinfor -b |") || die "Failed: $!\n";

Hence that is why I do not see any output on the browser? Is there any other way of calling this function?

What is the error message received or logged ( using $! )? That should be a hint

Well the issue is that am not getting any error messages.......

You have not provided the correct log line. This line is for the image 'page-background.png' indicates the URL is being redirected (to the script??). The redirection is either not completing (and thus no long line is generated) or the log line appears later.

Try simply including an "exit(0);" after your initial "print Content-type" line. You should then see a line in the logs with status 200. Keep moving the "exit()" line and doing a refresh until the log line is no longer 200 (or does not appear).

Okay tried the suggestion and here are the logs entries.

10.10.10.10 - - [12/Dec/2008:20:17:13 +0000] "GET /favicon.ico HTTP/1.1" 404 287 "http://10.10.10.1/cgi-bin/test.pl" "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_5_5; en-us) AppleWebKit/525.18 (KHTML, like Gecko) Version/3.1.2 Safari/525.20.1"
10.10.10.10 - - [12/Dec/2008:20:17:50 +0000] "GET /cgi-bin/test.pl HTTP/1.1" 200 283 "-" "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_5_5; en-us) AppleWebKit/525.18 (KHTML, like Gecko) Version/3.1.2 Safari/525.20.1"
10.10.10.10 - - [12/Dec/2008:20:17:50 +0000] "GET /favicon.ico HTTP/1.1" 404 287 "http://10.10.10.1/cgi-bin/test.pl" "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_5_5; en-us) AppleWebKit/525.18 (KHTML, like Gecko) Version/3.1.2 Safari/525.20.1"
10.10.10.10- - [12/Dec/2008:20:17:50 +0000] "GET /icons/test.gif HTTP/1.1" 200 4110 "http://10.10.10.1/cgi-bin/test.pl" "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_5_5; en-us) AppleWebKit/525.18 (KHTML, like Gecko) Version/3.1.2 Safari/525.20.1"
10.10.10.10 - - [12/Dec/2008:20:17:50 +0000] "GET /icons/page-background.png HTTP/1.1" 200 17055 "http://10.10.10.1/cgi-bin/test.pl" "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_5_5; en-us) AppleWebKit/525.18 (KHTML, like Gecko) Version/3.1.2 Safari/525.20.1"

I tried different perl package to just open a ssh shell to one of the server and executed it via the url and any ideas how to fix this issue? Since if this works, then I can replace my above script with this approach:

#!/usr/bin/perl -w

use warnings;
use strict;
use Socket;
#use Net::SSH::Perl;
use Net::OpenSSH;

my $ssh = Net::OpenSSH->new('root:root@10.10.10.1');
$ssh->error and die "Can't ssh : " . $ssh->error;

my $output = $ssh->capture( "ls -la");
$ssh->error and
warn "operation didn't complete successfully: ". $ssh->error;
print $output;

The Error when executed from URL is:
[Fri Dec 12 20:26:52 2008] [error] [client 10.10.10.10] Can't ssh : unable to stablish master ssh connection: the authenticity of the target host can't be established, try loging manually first at /var/www/cgi-bin/test2.pl line 20.
[Fri Dec 12 20:26:52 2008] [error] [client 10.10.10.10] Premature end of script headers: test2.pl

NOTE: The command works fine when executed from command line.......