如何从PHP中的shell脚本选择用户输入? [英] How to select user input from a shell script in PHP?

查看:77
本文介绍了如何从PHP中的shell脚本选择用户输入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经从PHP站点运行此shell脚本。

I've this shell script running from a PHP site.

在shell脚本(审计shell脚本)
我有3个选项:

In the shell script (Audit shell script), I have 3 options:

1)过程脚本
2)显示结果
3)退出

1) Process script 2) Display results 3) Exit

试过$ C $以下CS似乎并没有工作,PHP网站上显示空白。

Tried the codes below and doesn't seem to work, the PHP site displayed blanks.

<?php



session_start();


exec('/Desktop/test.sh');
exec('1');
$output = exec('2');
echo "<pre>$output</pre>";

?>

任何帮助将大大AP preciated。

Any help will be greatly appreciated.

推荐答案

请尝试使用proc_open代替EXEC;它可以让你处理输入/输出更多的控制。是这样的:

Try using proc_open instead of exec; it gives you more control of process input/output. Something like:

<?php

$descriptorspec = array(
   0 => array("pipe", "r"), // stdin is a pipe that the child will read from
   1 => array("pipe", "w"), // stdout is a pipe that the child will write to
   2 => array("file", "/dev/null", "a") // stderr is a file to write to
);

$cwd = '/Desktop';
$env = array();

$process = proc_open('/Desktop/test.sh', $descriptorspec, $pipes, $cwd, $env);

if (is_resource($process)) {
    // $pipes now looks like this:
    // 0 => writeable handle connected to child stdin
    // 1 => readable handle connected to child stdout
    // Any error output will be sent to /dev/null (ie, discarded)

    fwrite($pipes[0], "1\n");
    fwrite($pipes[0], "2\n");
    fclose($pipes[0]);

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

    // It is important that you close any pipes before calling
    // proc_close in order to avoid a deadlock
    $return_value = proc_close($process);

    echo $output;
}

?>

请注意:我举从 PHP这个code手册的proc_open页面

这篇关于如何从PHP中的shell脚本选择用户输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆