简体   繁体   English

如何从需要以root身份在PHP页面上运行的Python脚本返回输出?

[英]How can I return the output from a Python script that needs to be run as root on a PHP page?

I'm messing around on my raspberry pi and I've just gotten started by installing nginx and PHP. 我在弄乱我的树莓派,我刚开始安装nginx和PHP。 I have a DHT11 temperature sensor hooked up to the GPIO on my Pi, and I'm using a version of this Adafruit script to get the values. 我有一个DHT11温度传感器连接到我的Pi上的GPIO,并且我正在使用此Adafruit脚本的一个版本来获取值。 Only difference is that it has the proper GPIO values. 唯一的区别是它具有正确的GPIO值。 On my Pi (this is all on my internal network, if it matters) I have index.php sitting in the default nginx directory, and this is what it outputs: 在我的Pi上(如果有关系,这全在我的内部网络上),我的index.php位于默认的nginx目录中,这是它的输出:

<?php

#echo phpinfo();
$cmd = escapeshellcmd('/home/pi/simpleTemp.py');
$out = shell_exec($cmd);
echo $out;

Right now, I get no output, but I'm very sure that's because the script needs to be run as root. 现在,我没有任何输出,但是我非常确定这是因为脚本需要以root身份运行。 I've added execute permissions to the file but I still get nothing. 我已经向文件添加了执行权限,但是仍然什么也没得到。 My PHP install works fine. 我的PHP安装正常。 Should I be using shell_exec or is there a better way? 我应该使用shell_exec还是有更好的方法?

To run it as root, you can make use of sudo . 要以root身份运行它,可以使用sudo

<pre>
<?php

$cmd = '/home/pi/simpleTemp.py';
$out = shell_exec("sudo -u root " . escapeshellcmd($cmd));

if (is_null($out))
{
    echo "An error occurred."
    return;
}

echo $out;

This will run the script as root under the defaut shell. 这将在defaut shell下以root身份运行脚本。 If you don't have a $PATH set properly, you may need to specify an absolute path to sudo as /usr/bin/sudo on most systems. 如果没有正确设置$PATH ,则在大多数系统上,可能需要将sudo的绝对路径指定为/usr/bin/sudo

As shell_exec() is a bit limited with regard to error handling, we can also just use exec() . 由于shell_exec()在错误处理方面有所限制,因此我们也可以只使用exec() In this example, we'll add some more error handling as well: 在此示例中,我们还将添加更多错误处理:

<pre>
<?php

if (ini_get('safe_mode'))
{
    echo "Safe mode is enabled; cannot execute!";
    return;
}

$cmd = '/home/pi/simpleTemp.py';
$output = array();
$returnCode = -1;

if (!is_executable($cmd))
{
    echo sprintf("Command '%s' is not executable!", $cmd);
    return;
}

exec(sprintf("/usr/bin/sudo -u root %s 2>&1", escapeshellcmd($cmd)), $output, $returnCode);
echo implode("\n", $output);

if ($returnCode != 0)
{
    echo sprintf("An error occurred (code: %d).", $returnCode);
    return;
}

POSIX programs exit with status code 0 when no errors occurred. 没有错误发生时,POSIX程序以状态代码0退出。

Also note the 2>&1 here, which will redirect the output from Standard Error ( 2 ) to Standard Output ( 1 ) so that error output is not lost. 还要注意这里的2>&1 ,它将输出从标准错误( 2 )重定向到标准输出( 1 ),这样就不会丢失错误输出。

Another problem we might run into is if the $PATH environment variable is not what we expect. 我们可能会遇到的另一个问题是$PATH环境变量是否不是我们期望的。 Your Python program might also depend on having a sane $PATH . 您的Python程序也可能依赖于合理的$PATH Use the examples below to get and set $PATH : 使用以下示例获取并设置$PATH

<?php
/* To get the current $PATH. */
echo sprintf("Current PATH: %s", getenv('PATH'));

/* To set the $PATH. */
putenv('PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin');

I recently published a project that allows PHP to obtain and interact with a real Bash shell. 我最近发布了一个项目,该项目允许PHP获取真正的Bash shell并与之交互。 Get it here: https://github.com/merlinthemagic/MTS 在这里获取它: https : //github.com/merlinthemagic/MTS

After downloading you would simply use the following code: 下载后,您只需使用以下代码:

//if the script does not require root permissions you can change the
//second argument on getShell() to false. That will return a bash shell
//with permissions from www-data or apache user.

$shell    = \MTS\Factories::getDevices()->getLocalHost()->getShell('bash', true);
$return1  = $shell->exeCmd('/home/pi/simpleTemp.py');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM