我正在尝试执行一个带有php/html按钮的bash脚本来唤醒我的nas。
<form action="" method="POST">
<input type="submit" value="Wake NAS" name="zero" />
</form>
<?php
if (isset($_POST["zero"])){
#echo "Hello World!";
shell_exec("/var/www/html/wakenas.sh &");
}?>当按钮被按下时,打印"Hello World“。但是代码不会被执行。wakenas.sh看起来像这样,如果我在外壳上执行它,它就能正常工作
#!/bin/bash
etherwake -D "BC:5F:F4:09:E1:07"
echo "why!?!?!" > "/var/www/html/works.txt"
exit 1wakenas.sh拥有所有权利
也许你们知道为什么它不会被执行。
提前感谢
发布于 2016-05-20 19:27:20
执行脚本的简单而安全的方法是放入sudoers。假设您的Linux发行版是Debian base,运行web服务器的用户是www-data,那么您可以创建一个文件,例如/etc/sudoers.d/wakeup_ether。
Cmnd_Alias WAKE_UP_CMD = /var/www/html/wakenas.sh
www-data ALL=NOPASSWD: WAKE_UP_CMD修改脚本,在命令前加上sudo前缀。
shell_exec("sudo /var/www/html/wakenas.sh &");发布于 2015-09-04 05:59:43
从你的转储中:
etherwake: This program must be run as root.当您执行wakenas.sh时,您可能是以根用户身份执行它。这就是它工作的原因。
将sudo权限(没有密码)授予您的php服务器正在运行的用户。
并将wakenas.sh更改为:
#!/bin/bash
sudo etherwake -D "BC:5F:F4:09:E1:07"
echo "why!?!?!" > "/var/www/html/works.txt"
exit 1发布于 2016-05-20 19:11:52
我最近发布了一个项目,它允许PHP获得一个真正的Bash shell并与之交互(如果需要,作为根用户),它解决了exec()和shell_exec()的限制。在这里获取:https://github.com/merlinthemagic/MTS
下载后,您只需使用以下代码:
$shell = \MTS\Factories::getDevices()->getLocalHost()->getShell('bash', true);
$return1 = $shell->exeCmd("etherwake -D \"BC:5F:F4:09:E1:07\"");
//the return will be a string containing the return of the command
echo $return1;https://stackoverflow.com/questions/30641991
复制相似问题