Weird system call behavior

M

Me

I'm looking for some insight to the following code. I don't understand why
when I uncomment the #$command="/usr/local/apache/cgi-bin/port-reset.exp
$portnumber"; and comment the ls command it does not execute successfully.
When the code is as below everything works fine, but switching the commented
lines cause it to fail. I send the results of $command to a file and it
looks fine, I can even copy and paste the output, as any user, and it
executes fine, it just won't execute successfully when I call it from a web
page. Any assistance would be appreciated.



## Code begins
print "Content-type: text/html\n\n";
print "<HTML><BODY bgcolor=#483d8b><FONT COLOR=\"yellow\">";
print "<CENTER>";

$portnumber = $in{'PORT'}; #Value passed in from the
/usr/local/apache/htdocs/port-reset/index.html page

#$command="/usr/local/apache/cgi-bin/port-reset.exp $portnumber"; ## This
command will only execute from a command line and not from within this
script
$command="/usr/bin/ls -al > /tmp/file2"; # This works fine, so I comment
this line, uncomment the above and the script fails. Why?

$result = system("$command");
if ($result) {
# $? is the exit code.
print "<h1>Unable to reset port $portnumber. Contact support.\n";
}
else {
#It exits 0 if successfully
print "<h1>Port $portnumber has been reset.";
}

##############################################################
# This logs all page sent through the web
##############################################################
$Time = localtime;
open(OUTFILE, ">>/usr/local/apache/logs/port-reset.logs");
print OUTFILE
("\"$Time\",\"$ENV{'REMOTE_ADDR'}\",\"$portnumber\",\"$command\"\n\"");
exit;
## Code ends
 
L

-linux_lad

Me said:
I'm looking for some insight to the following code. I don't understand why
when I uncomment the #$command="/usr/local/apache/cgi-bin/port-reset.exp
$portnumber"; and comment the ls command it does not execute successfully.
When the code is as below everything works fine, but switching the commented
lines cause it to fail. I send the results of $command to a file and it
looks fine, I can even copy and paste the output, as any user, and it
executes fine, it just won't execute successfully when I call it from a web
page. Any assistance would be appreciated.



## Code begins
print "Content-type: text/html\n\n";
print "<HTML><BODY bgcolor=#483d8b><FONT COLOR=\"yellow\">";
print "<CENTER>";

$portnumber = $in{'PORT'}; #Value passed in from the
/usr/local/apache/htdocs/port-reset/index.html page

#$command="/usr/local/apache/cgi-bin/port-reset.exp $portnumber"; ## This
command will only execute from a command line and not from within this
script
$command="/usr/bin/ls -al > /tmp/file2"; # This works fine, so I comment
this line, uncomment the above and the script fails. Why?

$result = system("$command");
if ($result) {
# $? is the exit code.
print "<h1>Unable to reset port $portnumber. Contact support.\n";
}
else {
#It exits 0 if successfully
print "<h1>Port $portnumber has been reset.";
}

##############################################################
# This logs all page sent through the web
##############################################################
$Time = localtime;
open(OUTFILE, ">>/usr/local/apache/logs/port-reset.logs");
print OUTFILE
("\"$Time\",\"$ENV{'REMOTE_ADDR'}\",\"$portnumber\",\"$command\"\n\"");
exit;
## Code ends

Use sudo to give apache permission to execute.
 
M

Me

I can execute the command at the command line as nobody, so I don't think
using sudo would help. I'll give it a try anyways.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top