在php中使用sudo运行程序
Running program with sudo in php
我正在尝试通过PHP运行"lightled.py"(按钮RON)和"lightledoff.py"。这两个程序都包含GPIO库,并且需要root访问权限。按钮"ON"answers"OFF"直接打开和关闭led。按钮ON和OFF正常工作,但RON和ROFF不正常。如何运行需要root访问权限的python或c++程序?
<!doctype html>
<html>
<head>
<title>LED Test</title>
<meta charset="UTF-8"/>
</head>
<body>
<h1>Light Led</h1>
<form method=GET action="index.php">
<h3>Radio Led</h3>
<input name="button" type="submit" value="RON">
<input name="button" type="submit" value="ROFF">
<h3>On Board Led for Test</h3>
<input name="button" type="submit" value="ON">
<input name="button" type="submit" value="OFF">
</form>
<?php
if ($_GET["button"] == "RON") {
system("echo raspberry | sudo -S python ./lightled.py");
};
if ($_GET["button"] == "ROFF") {
system("echo raspberry | sudo -S python ./lightledoff.py");
};
if ($_GET["button"] == "ON") {
system("gpio -g mode 17 out");
system("gpio -g write 17 1");
};
if ($_GET["button"] == "OFF") {
system("gpio -g mode 17 out");
system("gpio -g write 17 0");
};
?>
</body>
</html>
您要做的是从没有权限的用户运行sudo命令,即使在以root身份运行PHP框架时,生成的用户(框架用户)也不会被sudo提升为root权限。
您必须将运行PHP脚本的框架添加到sudoers文件中,并且您应该能够通过sudo命令以root身份执行这些文件,就像您已经尝试过的那样。要做到这一点,您需要以root身份运行命令visudo,并向用户添加一个条目,如下所示:
# User Privilege Specification
root ALL=(ALL) ALL
PHPFramework ALL=(ALL) ALL
如果你不想给PHP框架所有的根权限(我建议你这样做,即使它只是本地运行的一个小RPi),你必须编辑sudo文件,更具体地说,要阅读主题谷歌"visudo-privaleges",这是我发现的第一个点击:https://www.garron.me/en/linux/visudo-command-sudoers-file-sudo-default-editor.html
通过sudo只允许运行python脚本的示例:
# User Privilege Specification
PHPFramework ALL=/path/to/file/lightled.py; /path/to/python_install
其中PHPFramework是部署方法的名称,对于apache,它将是"apache"(没有qoutes)。
''我最近发布了一个项目,该项目允许PHP获得真正的Bash shell并与之交互。在这里获取:https://github.com/merlinthemagic/MTS
下载后,您只需使用以下代码:
$shell = MTSFactories::getDevices()->getLocalHost()->getShell('bash', true);
//insert command
$strCmd = 'echo raspberry | sudo -S python ./lightled.py';
$return = $shell->exeCmd($strCmd);
//handle any return and issue next command into the shell
- 运行同一解决方案的另一个项目的项目
- CMake-按正确顺序将项目与C运行时对象文件链接
- 如何运行位于boost/libs/python/example/tutorial目录中的hello.cpp和Jamfil
- 代码在main()中运行,但在函数中出现错误
- 我在c++代码中生成了一个运行时#3异常
- 如何在linux终端中同时编译和运行c++代码
- 为什么在运行时没有向我们提供有关分段错误的更多信息?
- 如何在运行中期切换GTK CSS style_context
- 如何在MS Visual Studio 2019中运行QT UI
- 如何通过cpp程序运行shell脚本
- IPC使用多个管道和分支进程来运行Python程序
- 删除指向指针的指针是运行时错误吗
- 如何用参数值调用函数(仅在运行时已知)
- 为什么即使使用-cudart-static进行编译,库用户仍然需要链接到cuda运行时
- 是否可以在编译时初始化数组,以便在运行时不会花费时间?
- 如果程序在没有 sudo 的情况下运行,gtk_init会导致 ubuntu 16.04 崩溃
- 我运行sudo g++ main.cpp,但它没有运行程序
- 检查Qt c ++应用程序是否以sudo的形式运行
- 在php中使用sudo运行程序
- libusb 1.0程序以user身份运行时出现分段错误,而不是sudo身份