Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
520 views
in Technique[技术] by (71.8m points)

php or apache? exec, popen, system and proc_open commands do not execute any command not even ls

My problem is with executing any command from my php homepage

Half solved, I got my pythonscript running but futher on the pythonscript does not have permission to execute its modules

but i′m starting to think that the problem is with apache and not php? i tried to run a pythonscript that is actually what i′m going to run in the end, then it seems to run but stops when the script tries to access a txt file looking like this

[stdout] =>
[stderr] => Traceback (most recent call last): 
 File "pythontest.py", line 4, in ? f = open("(path)/temp.txt", "w") 
 IOError: [Errno 13] Permission denied: '(path)/temp.txt'

I have tried all different kind of execution methods that i could find exec, system, popen, proc_open almost nothing runs, i can get a string from system("pwd") but system("ls") returns a number 127 or 126. I also tried to run a compiled c program (code below) exec will return an object "Array" but system will only return 127 or 126 as ls does.

Also I tried to get the values out of the "Array" object? from exec but it returns nothing, if I php print_R Array the page won′t load.

the php output is (code below)-> a: , out1: 126, t[[0]: , strlen out: 5, out: Array, t: no matter how many arguments i actually put in!

the full path to ls gives 126 and just "ls" gives the number 127

i have tested to chmod all files to full permission, from the page.php to test program but it still gives the same output

php safe mode is off

echo '<pre>';
print_r(execute('ls'))
echo '</pre>';

returns

array
(
    [stdout] => 
    [stderr] => sh: /var/www/html/grndb/upscgenesearch/test1: Permission denied

    [return] => 126
)

why does it give me Permission denied when the test1 program has full permission?

<?php
$a = system("/bin/ls",$out1);
echo "a: "; echo $a;
echo ", out1: "; echo $out1;
$t = exec('pwd/test1 aa hh 11',$out);

foreach ($t as &$value) {
    echo ($value);
}

echo ", t[[0]: "; echo $t[0]; 
echo ", strlen out: "; echo strlen($out);
echo ", out: "; echo $out; 
echo ", t: "; echo $t;
?>

C source code

int main(int argc, char *argv[])
{
int i;

for(i = 0; i < argc; i++)
printf("arg %d: %s
", i, argv[i]);
return 0;
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Here's another functions to add to the list :)

/**
 * Executes a program and waits for it to finish, taking pipes into account.
 * @param string $cmd Command line to execute, including any arguments.
 * @param string $input Data for standard input.
 * @return array Array of "stdout", "stderr" and "return".
 * @copyright 2011 K2F Framework / Covac Software
 */
function execute($cmd,$stdin=null){
    $proc=proc_open($cmd,array(0=>array('pipe','r'),1=>array('pipe','w'),2=>array('pipe','w')),$pipes);
    fwrite($pipes[0],$stdin);                      fclose($pipes[0]);
    $stdout=stream_get_contents($pipes[1]);        fclose($pipes[1]);
    $stderr=stream_get_contents($pipes[2]);        fclose($pipes[2]);
    $return=proc_close($proc);
    return array( 'stdout'=>$stdout, 'stderr'=>$stderr, 'return'=>$return );
}

Example of use for your case:

echo '<pre>';
print_r(execute('ls'));
echo '</pre>';

The function above is offers more features than using the PHP ones directly. It could help you at debugging, though it's more intended for production code.

In general, this is what you should be aware off:

  • You're not under safe mode
  • Your executable does exist
  • Your executable is runnable - know that FTP clients often use ASCII/Text mode, changing CRLFs in uploaded files thus corrupting them
  • Be sure to chmod your executable at least 755

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...