Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

external application call

Status
Not open for further replies.

jameshutchinson008

IS-IT--Management
Feb 26, 2002
43
0
0
GB
Hello all,

I am having problems reading the result of an external application. This problem only manifests itself when running from a web server via a browser; works fine from the cli.

Code:
#Redirect stderr to the browser
open(STDERR, ">&STDOUT");

use CGI ':standard';
$cgi = new CGI;
#use warnings;
print header();
#Example external application hosted on a windows web server
$cmd = "echo james";
$output = `$cmd 2>&1`;

#display the results of the external command
print "$output\n";

if I enable use warnings I am warned the $output variable has not been initialized.

Cheers,

James.
 
When you run a piece of code from a command prompt, that code runs as you and has the use of your 'environment'. The shell knows stuff like your PATH. When the same code is run by the web server in response to a browser request, it runs as the web server and very likely has different environment (eg. PATH). For security reasons, web servers are usually setup with very limited abilities when it comes to access system resources. So, it can frequently be the case that the web server process will not be able to do things you can do. For example, since the web server process has a limited PATH statement, it may not know where the 'echo' command lives (/bin/echo/) and consequently, may not be able to run it.

Here is an example that works on my Fedora Core 3 machine with Apache 2.0.

Code:
#!/usr/bin/perl -T
use strict;
use CGI;
$ENV{PATH} = '';
my $obj = new CGI;

my $cmd = '/bin/echo some_text';
my $result;
open(CMD,"$cmd  2>&1 |") or die "Failed to open pipe, $!\n";
while (<CMD>) { $result .= $_; }
close CMD;

print $obj->header, $obj->start_html, qq(result: $result), $obj->end_html;

Note that I gave the full path to the 'echo' command (/bin/echo).

'hope this helps

If you are new to Tek-Tips, please use descriptive titles, check the FAQs, and beware the evil typo.
 
Hello goBoating,

It seems that I only have this problem while attempting to run the script from the Windows box. I am now using the pipe method as you suggested which also works fine from my redhat 8.0/apache box. I have checked that echo has a pointer in path simply by having the server print the path to the browser.

I have also tried to clear the path and pass the command as an absolute path with no joy.

Could this be a configuration change needed in IIS?

Thanks for your help,

James.
 
Hhhmmm..... you ask if this problem could be a configuration issue with IIS. That makes me think you are actually running this code on a windows machine. If that is the case, then you're crossing a boundary where perl is not actually fully 'platform/OS' independent. Making system calls on a Windows machine is not always exactly the same as doing it on a *nix machine. For example, the 'echo' command is not exactly the same on the two OSes.

I'll play with it on my XP machine a little and let you know.... maybe a few hours before I can get to it....

'hope this helps

If you are new to Tek-Tips, please use descriptive titles, check the FAQs, and beware the evil typo.
 
Was a post of mine removed from this thread?
--Paul

cigless ...
 
Apologies goBoating I failed to mention that in my first post. I have a variety of platforms available but at the minute the main application that I am currently working on is running on a windows 2003 server platform. I used echo as a simple example so as to provide syntax clarity (I could have easily used ver), regardless of external app I try to call on the windows box I get the same result on the Linux platform it works!

PaulTEG.. No it looks like I may have posted the same thread twice, apologies.

Thanks for your input it has been most helpful.

James
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top