如何从php脚本执行shell命令(How to execute a shell command from a php script)

编程入门 行业动态 更新时间:2024-10-27 01:36:25
如何从php脚本执行shell命令(How to execute a shell command from a php script)

我想创建一个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 script

My 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.

更多推荐

本文发布于:2023-07-26 05:09:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1271342.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:脚本   命令   shell   php   command

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!