LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-16-2008, 01:52 AM   #1
turuvekere
LQ Newbie
 
Registered: Jun 2008
Posts: 6

Rep: Reputation: 0
Angry 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.
 
Old 06-16-2008, 10:54 AM   #2
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
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.

Last edited by theNbomr; 06-16-2008 at 11:00 AM.
 
Old 06-16-2008, 11:50 PM   #3
turuvekere
LQ Newbie
 
Registered: Jun 2008
Posts: 6

Original Poster
Rep: Reputation: 0
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;
 
Old 06-17-2008, 09:51 AM   #4
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
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.
 
Old 06-18-2008, 03:10 AM   #5
turuvekere
LQ Newbie
 
Registered: Jun 2008
Posts: 6

Original Poster
Rep: Reputation: 0
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]
 
Old 06-18-2008, 09:53 AM   #6
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
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.
 
Old 06-19-2008, 07:42 AM   #7
turuvekere
LQ Newbie
 
Registered: Jun 2008
Posts: 6

Original Poster
Rep: Reputation: 0
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.
 
Old 06-19-2008, 09:29 AM   #8
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
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.
 
Old 06-20-2008, 12:39 AM   #9
turuvekere
LQ Newbie
 
Registered: Jun 2008
Posts: 6

Original Poster
Rep: Reputation: 0
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.
 
Old 06-20-2008, 10:43 AM   #10
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
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.
 
Old 06-22-2008, 05:22 AM   #11
turuvekere
LQ Newbie
 
Registered: Jun 2008
Posts: 6

Original Poster
Rep: Reputation: 0
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Perl CGI:Can't locate CGI.pm supermyself Programming 13 09-10-2007 06:22 AM
Need help for CGI elmerliu Programming 1 09-25-2004 07:49 PM
CGI to work out side of cgi-bin? crashedspine Linux - Newbie 13 09-02-2004 08:49 PM
apache v2.0.44 + cgi ; securing cgi-support in apache markus1982 Linux - General 0 01-20-2003 01:03 PM
http://www.burstnet.com/cgi-bin/ads/ad7954a.cgi/3980/RETURN-CODE rverlander LQ Suggestions & Feedback 1 06-07-2002 07:35 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:34 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration