LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Cgi (https://www.linuxquestions.org/questions/linux-newbie-8/cgi-649561/)

turuvekere 06-16-2008 01:52 AM

Cgi
 
Hi all,

I am executing a perl CGI script(stored in a Linux machine)through my windows browser(client side ).

I am not able to display terminal outputs executed through my perl CGI script.

.............................................
#!/usr/bin/perl
use CGI qw/:standard/;

print header,
start_html('First Task');

print `ls`;

print end_html;
.............................................

Here i want to display the output of the "ls " command on my windows browser.

theNbomr 06-16-2008 10:54 AM

Try adding to your CGI:
Code:

use CGI::Carp qw(fatalsToBrowser);
Then, print something that you know will get to the HTML page.
Code:

print "testing 1,2,3...";
Print the result of the `ls` command to standard error, and find it in the server error logs:
Code:

print STDERR `ls`;
After you've done all of that, you will probably find that ls is not being found in the web server's path, and you need to explicitly give the fully qualified path to the executable.
--- rod.

--- rod.

turuvekere 06-16-2008 11:50 PM

Thanks for your reply.

I have modified the code as you suggested.
But still the "ls" command output could not be displayed on the browser.


#!/usr/bin/perl
use CGI qw/:standard/;
use CGI::Carp qw(fatalsToBrowser);

print
header,
start_html('First Task');
print "testing 1 2 3 ";
print STDERR `ls`;

print end_html;

theNbomr 06-17-2008 09:51 AM

Did the ouptut from 'ls' show up in the web server's error log file? If not, then how do you know that 'ls' was actually invoked? Did you try using a fully qualified pathname for the 'ls' command? What happened there? You can verify this with something like this:
Code:

my $lsOut = `ls`;
print "ls Output:\n", $lsOut;

If you only see the literal text heading 'ls Output:' on your browser, it tells me that ls is not actually being invoked, because the server didn't have it in it's $PATH variable. Try changing 'ls' to '/bin/ls' or wherever ls is installed on your system.
Better still, since you are using perl, try using the opendir()/readdir() functions.
--- rod.

turuvekere 06-18-2008 03:10 AM

I tried as the above, but still the "ls" output could not be displayed (on client Windows browser).

My log message is : (/etc/httpd/logs/error_log)

[Wed Jun 18 12:48:26 2008] [error] [client 192.168.0.210]

theNbomr 06-18-2008 09:53 AM

What part did you try? If you only tried the part in the [code] block, then you've merely confirmed what I've been trying to tell you since my first post. What have you done to prove that 'ls' is actually being invoked?
Get your CGI script to print the value of the PATH environment variable:
Code:

print "PATH: ", $ENV{'PATH'},"\n";
If the 'ls' executable is not in the list of places in $PATH, then you need to use the fully qualified directory & filename of the ls executable in your script in order to invoke it. Have you done this?

Please answer all of these questions. In order to help you, you need to supply information.

--- rod.

turuvekere 06-19-2008 07:42 AM

Thanks for your help, i think the above problem will be solved if you please help me to solve the below task.

--------------------------------
#!/usr/bin/perl
use CGI qw/:standard/;

print
header,
start_html('First Task');
print " I am creating a file ";
print `touch xyz`;
print end_html;

-------------------------------------

Here, i want to create a file named "xyz" from the client(windows browser)in the server directory in which the above code runs.

theNbomr 06-19-2008 09:29 AM

What's to solve? You haven't said what doesn't work.
Look, I like to help people solve their problems. I ask thoughtfully composed questions to try to get the information necessary to first diagnose the problem, and then to provide a solution, if one exists. Please do yourself a favor and others the courtesy of replying with at least an attempt at answering the questions that are posed.
--- rod.

turuvekere 06-20-2008 12:39 AM

Sorry for the above...

When i used the [ print "PATH: ", $ENV{'PATH'},"\n"; ] in my CGI code. I got the below output

PATH: /sbin:/usr/sbin:/bin:/usr/bin:/usr/X11R6/bin

The "ls" programme is in {/bin}directory.

theNbomr 06-20-2008 10:43 AM

Okay, now we're getting somewhere. When you ran the code above, it was run as a CGI called by a web server, right? This is important, because the web server has its own user ID and attendant permissions. Running it standalone as a normal user or as root is not a valid test. If this is true, then we can be fairly sure that the ls command is actually being invoked, unless your server has restricted execution of these binaries by SElinux. I'm not sure how to selectively remove restrictions in Selinux; I normally run with is disabled. You may want to check that.
Your touch command should fail in a properly set up system, because you don't want your web server to have write permissions in the cgi-bin directory. You may be able to create files in other directories, if you give the directories appropriate permissions and ownerships. '/tmp' is often a good candidate, as it typically has world-read & world-write permissions. If you cannot write there, suspect Selinux.
If you really just want to acquire directory listings, and ls is not being used merely as a trivial test, then I suggest you look at the perl functions opendir() and readdir(). These can do everything that ls can do.
Code:

#! /usr/bin/perl -w
use strict;
use CGI qw/:standard/;
use CGI::Carp qw(fatalsToBrowser);

print header, start_html('First Task');

opendir( DIR, "." );
my @filesAndDirs = readdir(DIR);
foreach my $fileOrDir ( sort @filesAndDirs ){
        print $fileOrDir."<BR>\n";
}
print end_html;

If this works, then, I suspect Selinux is in your way.
--- rod.

turuvekere 06-22-2008 05:22 AM

I got the solution. Selinux was causing this problem. I stoped the selinux and my code is now working properly.
Thanks a lot for your help.


All times are GMT -5. The time now is 06:25 AM.