PHP 运行 bash (.sh) 脚本。 dpkg --info 有效,但 dpkg -i 无效

发布于 2024-11-03 07:59:14 字数 594 浏览 9 评论 0原文

示例 PHP 脚本如下所示:

#!/usr/bin/php
    $file = '/private/var/www/app/install.sh';
    if(is_file($file)){
        $output = shell_exec('bash /private/var/www/app/install.sh');
        fwrite(STDOUT, $output."\n");
    }
    exit(0);

install.sh 示例

#!/bin/bash
clear
echo "Executing Install Script..."
dpkg --info /private/var/www/app/app.deb
dpkg -i /private/var/www/app/app.deb
echo "Script Finished"
exit 0

这将打印出生成的 dpkg --info 数据,但不会运行 dpkg -i。它不会打印出任何错误或任何内容...

此外,这是通过网络浏览器执行的。当脚本从终端运行时,它工作正常。但在网络上,仅返回 info 命令。

Sample PHP Script looks like this:

#!/usr/bin/php
    $file = '/private/var/www/app/install.sh';
    if(is_file($file)){
        $output = shell_exec('bash /private/var/www/app/install.sh');
        fwrite(STDOUT, $output."\n");
    }
    exit(0);

install.sh Example

#!/bin/bash
clear
echo "Executing Install Script..."
dpkg --info /private/var/www/app/app.deb
dpkg -i /private/var/www/app/app.deb
echo "Script Finished"
exit 0

This will print out the resulting dpkg --info data, but it will not run dpkg -i. It doesnt print out any errors, or anything at all...

Also, this is being executed via a web web browser. When the script is run from the terminal, it works fine. But on the web, only the info command is returned.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

記柔刀 2024-11-10 07:59:14

您在这里尝试执行的操作可能存在一些安全问题,我不会对此发表评论。但是,我猜测 dpkg -i 需要以 root 身份运行,而您的网络服务器(执行 install.sh 脚本)没有以 root 身份运行。另一方面,dpkg --info 命令不需要 root 权限即可运行,因此在通过网络服务器的用户执行时您将看到其输出。如果您确实需要以 root 身份运行此脚本,您可能需要查看特定的 /etc/sudoers 配置。也许从这里开始:https://help.ubuntu.com/community/Sudoers

看看这里:http://ubuntuforums.org/showthread.php?t=1132821

There are probably some security concerns with what you are trying to do here, and I'm not going to comment on those. However, I'd guess that dpkg -i needs to run as root, and your webserver (which executes the install.sh script) is not running as root. On the other hand, the dpkg --info command doesn't need root privileges to run, and so you will see its output when executed via the webserver's user. If you really need to run this script as root, you might want to look at a specific /etc/sudoers config. Perhaps start here: https://help.ubuntu.com/community/Sudoers

and take a look here: http://ubuntuforums.org/showthread.php?t=1132821

终难愈 2024-11-10 07:59:14

dpkg --info 不需要 root 权限,而 dpkg -i 则需要。由于您在浏览器上运行脚本,这意味着您的脚本在 php/apache 的用户上运行。我相信,在大多数安装中,该用户没有 root 权限。

为了克服这个问题,您可以创建某种处理器,该处理器将执行以下步骤:

创建队列

  • queue_id
  • 时间戳
  • deb_file
  • is_processed

浏览器脚本

  • 单击安装后,将一个条目插入队列表。

处理器脚本(假设queue_processor.php

  • 运行时,检查队列表中是否有 is_processed = 0 的条目(
  • 如果有):
    • 更新 is_processed = 1
    • 处理该文件
    • 处理完成后(安装deb文件),设置is_processed = 2

,设置 is_processed = 2 最后一步是,在 crontab 上设置 queue_processor.php 并拥有它的所有者作为根用户:

*   *   *   *   *   root   /path/to/php/binary/php /path/to/your/queue_processor.php

dpkg --info doesn't require root permission while dpkg -i does. Since you run your script on browser, this mean that your script run on php / apache's user. I believe, on most installation, that user doesn't have root permission.

To overcome this, you could create some sort of processor that will have the following step:

create queue table:

  • queue_id
  • timestamp
  • deb_file
  • is_processed

browser script:

  • upon clicking on install, insert an entry to queue table.

processor script (let's say queue_processor.php):

  • upon run, check if there are entries on queue table whose is_processed = 0
  • if there are any:
    • update is_processed = 1
    • process that file
    • after finished processing (installing deb file), set is_processed = 2

The final step is, to set queue_processor.php on crontab and have it owner as root:

*   *   *   *   *   root   /path/to/php/binary/php /path/to/your/queue_processor.php
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文