1

purpose: use php to input commands directly into the minecraft server console

Im trying to use a php script (run from browser) to exec() a shell script. when i run the php from a terminal it works! But in the browser, nothing happens.

exec('sudo -u root sh /home/minecraft/whitelist-reload.sh', $out, $ret_val);

When running from terminal, i get a "array 0" but the browser gives me a "array 1"

what is the issue? and once i run the shell, shouldn't everything after that work as if you were on a terminal?(does it matter what is inside of shell script?)

the shell has all rx permissions and is in the sudoers file as

www-data ALL = NOPASSWD: /home/minecraft/whitelist-reload.sh
4

2 回答 2

2

问题是,您在终端上以可能拥有sudo权限的用户身份运行脚本,而 apache/webserver 用户没有,因此$ret_val(实际上只是一个状态代码)设置为 1(表示错误) .

尝试var_dump($out);在这两种情况下查看 exec 调用的结果。要从浏览器中执行此类操作,您可能需要查看proc_open和家庭,或者有一个 chmod'ed 为 777 的脚本,因此 apache 用户也可以运行它。然后让该脚本调用实际的 shell 脚本并返回它的输出。然而,这是非常危险的,并且应该只用于在您自己的机器上测试环境。永远不要在生产环境中这样做!

我在这里也发布了几个问题,这可能会提供有用的信息:

通过 ssh 进行交互

打开第二个外壳,加载配置文件变量并调用另一个脚本

于 2012-08-03T11:28:53.497 回答
0

原来......在将 www-data 输入到 sudoers 文件后,我需要做的就是在它后面加上“-u root”

于 2012-08-04T08:42:09.753 回答