我想创建一个php脚本来执行一个shell命令并返回它的输出。 服务器需要私钥。 当我第一次决定测试这个时,我创建了这个:
<?php $command = "ls"; $output = shell_exec($command); echo "<pre>$output</pre>"; ?>这工作得很好。 但是当我将$command改为我真正想要运行的命令时:
$command = "/etc/init.d/mycontrollerd status /etc/mycontrollerconfig";它给了我这个输出:
You need root privileges to run this script我的猜测是我需要使用sudo 。 当然,这需要将pem文件放在服务器上的某个地方。 假设我这样做, $command应该是什么? 我应该使用shell_exec() , exec() , system()还是别的?
I would like to create a php script to execute a shell command and return its output. The server requires a private key. When I first decided to test this out I created this:
<?php $command = "ls"; $output = shell_exec($command); echo "<pre>$output</pre>"; ?>That worked just fine. But when I changed $command to the command I really wanted to run:
$command = "/etc/init.d/mycontrollerd status /etc/mycontrollerconfig";it gave me this output:
You need root privileges to run this scriptMy guess is I need to use sudo. Of course that will require putting the pem file somewhere on the server. Assuming I do that, what exactly should $command be? Should I use shell_exec(), exec(), system() or something else?
最满意答案
使用哪个php函数启动脚本并不重要 - 缺少用户帐户的授权。
可以使用sudo(预先配置web服务器用户通过visudo运行没有密码的确切命令,并用sudo命令前缀),或者设置一个setuid脚本来执行自己的命令。
It does not matter which php function you use to start the script - what lacks is the authorization of your user account.
Either use sudo (preconfigure the web server user to run the exact command without password via visudo, and prefix the command with sudo) or set up a setuid script that executes the command on itself.
更多推荐
发布评论