【问题标题】:PHP can run a bash file from the php shell but not when a button is clicked on webbrowser(ubuntu-firefox))PHP 可以从 php shell 运行 bash 文件,但不能在单击 webbrowser(ubuntu-firefox)上的按钮时运行)
【发布时间】:2015-11-17 22:03:35
【问题描述】:

我有一个 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 会询问我的密码。

【问题讨论】:

  • 我认为是因为目录后面有空格
  • 尝试将 2>&1 添加到您的命令中
  • 你确定你有权限使用 shell_exec() 因为大多数服务器默认不允许这样做
  • @elibyy 谢谢这是一个错字。
  • 打印帖子数据..你确定你收到了帖子数据吗...我的事情你必须给你的表格一个名字

标签: php bash unix


【解决方案1】:

在网络发布请求下,您将无法运行以交互方式请求您的密码的脚本(至少在没有大量额外基础设施的情况下无法运行,例如 expect 脚本)。无论如何,您希望如何输入您的回复?

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

【讨论】:

    【解决方案2】:

    您可以使用类似的方法来输入您的密码,然后将其传递给 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
    

    【讨论】:

      猜你喜欢
      • 2012-01-23
      • 1970-01-01
      • 2019-04-01
      • 1970-01-01
      • 2015-12-25
      • 2017-08-09
      • 2021-06-26
      • 2021-05-07
      • 1970-01-01
      相关资源
      最近更新 更多