php command use root

piaoling  2014-10-22 16:59:35
function mycommand($command,$pwd=''){
 
    $descriptorspec = array(
            0 => array("pipe", "r"),  // 标准输入,子进程从此管道中读取数据
            1 => array("pipe", "w"),  // 标准输出,子进程向此管道中写入数据
            2 => array("file", "/tmp/error-output.txt", "a") // 标准错误,写入到一个文件
    );


    $currentCommand = 'su --login root --command "'.$command.'"';

    $process = proc_open($currentCommand, $descriptorspec, $pipes);

    if (is_resource($process)) {
        // $pipes 现在看起来是这样的:
        // 0 => 可以向子进程标准输入写入的句柄
        // 1 => 可以从子进程标准输出读取的句柄
        // 错误输出将被追加到文件 /tmp/error-output.txt

        fwrite($pipes[0], $pwd);
        fclose($pipes[0]);

        $data =  stream_get_contents($pipes[1]);
        fclose($pipes[1]);


        // 切记:在调用 proc_close 之前关闭所有的管道以避免死锁。
        $return_value = proc_close($process);

        //echo "command returned $return_value ";
        return $data;
    }
}
类别 :  默认(739)  |  浏览(4367)  |  评论(0)
发表评论(评论将通过邮件发给作者):

Email: