[英]Executing a bash script via php
Good day all, I m trying to implement a web interface that will operate my wireless network. 一切顺利,我正在尝试实现可操作我的无线网络的Web界面。
One of the operations is to configure my card into monitor mode. 其中一项操作是将我的卡配置为监视模式。 pretty simple, if you run this command: 非常简单,如果您运行以下命令:
bash prepareCard.sh wlan0
and the script prepareCard.sh is as follows: 脚本prepareCard.sh如下:
#! /bin/bash
IFACE=$1
ifconfig $IFACE down
iwconfig $IFACE mode monitor
ifconfig $IFACE up
Now I want to execute this script via a php script: 现在,我想通过php脚本执行此脚本:
$cmd = shell_exec("bash prepareCard.sh wlan0");
when I check if the card has been set to monitor mode, nothing! 当我检查卡是否已设置为监控模式时,什么都没有! it's still in management mode!! 它仍然处于管理模式! Can you please tell me where did I go wrong? 您能告诉我我哪里出问题了吗?
One good way to debug BASH scripts is to set debug mode on (either by passing an -x
flag to bash in your shell_exec
call, by running set -x
, or in the shebang line #!/bin/bash -x
). 调试BASH脚本的一种好方法是将调试模式设置为on(通过在shell_exec
调用中将-x
标志传递给bash,运行set -x
或在shebang行中#!/bin/bash -x
)。 This shows you what's going on during execution of the bash script. 这向您显示了执行bash脚本期间发生的情况。 In your case, I suggest the first case, since you don't know if the script is even being loaded in the first place. 在您的情况下,我建议第一种情况,因为您不知道脚本是否首先被加载。
$cmd = shell_exec("bash -x prepareCard.sh wlan0");
After that, you should have more in your $cmd variable. 之后,您应该在$ cmd变量中包含更多内容。
Assuming the webserver user that is running the script does not have sufficient permissions, you can try this way to fix it: 假设正在运行脚本的Web服务器用户没有足够的权限,则可以尝试以下方式对其进行修复:
Use command visudo
to edit /etc/sudoers
and add this line: 使用命令visudo
编辑/etc/sudoers
并添加以下行:
ALL ALL=(root) NOPASSWD: /absolute/path/prepareCard.sh
Make sure to set permissions 700
to the script, so no one else can edit it. 确保为脚本设置了权限700
,因此没有其他人可以对其进行编辑。 Then execute your script with sudo
like this: 然后使用sudo
执行脚本,如下所示:
$cmd = shell_exec("sudo /absolute/path/prepareCard.sh wlan0");
That should execute the script as root without a need to enter a password. 那应该以root用户身份执行脚本,而无需输入密码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.