PHP可以从PHP shell运行bash文件,但在web浏览器(ubuntu-firefox)上单击按钮时不能运行


PHP can run a bash file from the php shell but not when a button is clicked on webbrowser(ubuntu-firefox))

我有一个bash文件,当我使用php -a(交互式shell)从终端运行它时,可以执行它,输入:

shell_exec('sh /home/oceanview/run_test.sh');

我已经创建了一个按钮,当这个按钮被点击时,bash文件应该运行

<form action="Control.php" method="post">
<input value="Continous Acquisition " name="Continous" type="submit">
</form>
<?php
if (isset($_POST['Continous'])) {
shell_exec('sh /home/oceanview/run_test.sh');
 }
 ?>

但是当我按下按钮时什么也没发生。我已经尝试了chmod +x和chmod 0777,日志中没有显示错误。

我必须添加,当我在终端中运行shell_exec命令时,交互式shell会询问我的密码。

在web post请求下,您将无法运行交互式请求您的密码的脚本(至少不是没有很多额外的基础设施,如expect脚本)。话说回来,你怎么才能打出你的回复呢?

最有可能发生的事情是进程在检测到它没有连接到交互式终端时退出。您将无法看到它产生的任何输出,因为您没有捕获或使用shell_exec()的结果。

您可以使用类似的代码来输入密码,然后将其传递给shell命令

<form action="Control.php" method="post">
<label>Password: <input type="password" name="pwd"></label>
<input value="Continous Acquisition " name="Continous" type="submit">
</form>
<?php
if (isset($_POST['Continous'])) {
  $command = sprintf(
    'echo %s | sh /home/oceanview/run_test.sh',
    escapeshellarg($_POST['pwd'])
  );
  echo shell_exec($command);
}
?>

您可以在命令行中使用

进行测试
echo 'mypasswordhere' | sh /home/oceanview/run_test.sh