[英]bash script using zenity works in terminal but not in php shell_exec
I'm not familar with zenity or shell-exec so I may have made a silly mistake... 我不熟悉zenity或shell-exec所以我可能犯了一个愚蠢的错误......
The following command line works in an Ubuntu terminal window; 以下命令行在Ubuntu终端窗口中工作;
PASSWD="$(zenity --password --title=Authentication)"; echo -e $PASSWD | sudo -S nmap -A 192.168.0.1-255;
It puts up a GUI dialogue box to ask for the password and then nmap runs correctly. 它会打开一个GUI对话框来询问密码,然后nmap正确运行。
If I put the following code in a php file on my local machine, served by Apache: 如果我将以下代码放在我的本地机器上的php文件中,由Apache提供服务:
$network = '192.168.0.1-255';
$cmd = 'PASSWD="$(zenity --password --title=Authentication)"; echo -e $PASSWD | sudo -S nmap -A ' . $network;
echo $cmd;
$output3 = rtrim(shell_exec($cmd));
echo ($output3);
then $cmd is output as 然后$ cmd输出为
PASSWD="$(zenity --password --title=Authentication)"; echo -e $PASSWD | sudo -S nmap -A 192.168.0.1-255
but nothing else happens. 但没有别的事情发生。
I looked at run zenity from php and tried giving this command first from a terminal: 我从php看了一下运行zenity并尝试从终端首先发出这个命令:
xhost local:www-data
but that did not help. 但这没有帮助。
Running, for example, "ls" from shell_exec works fine. 例如,从shell_exec运行“ls”可以正常运行。
I would welcome any suggestions. 我欢迎任何建议。
I would use this: 我会用这个:
pkexec nmap -A 192.168.0.1-255
pkexec replaces gksudo in newer distributions. pkexec在较新的发行版中替换了gksudo。
@danielsedoff I created a file "com.ubuntu.pkexec.nmap.policy" in folder /usr/share/polkit-1 with these contents: @danielsedoff我在/ usr / share / polkit-1文件夹中创建了一个文件“com.ubuntu.pkexec.nmap.policy”,其中包含以下内容:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE policyconfig PUBLIC
"-//freedesktop//DTD PolicyKit Policy Configuration 1.0//EN"
"http://www.freedesktop.org/standards/PolicyKit/1/policyconfig.dtd">
<policyconfig>
<action id="com.ubuntu.pkexec.nmap">
<defaults>
<allow_any>yes</allow_any>
<allow_inactive>yes</allow_inactive>
<allow_active>yes</allow_active>
</defaults>
<annotate key="org.freedesktop.policykit.exec.path">/usr/bin/nmap</annotate>
</action>
</policyconfig>
The command in my php file 我的php文件中的命令
pkexec --user root nmap -A 192.168.0.1-255
now works as I hoped without needing interactive authorisation from the user. 现在按照我的希望工作,无需用户的交互授权。 Thanks.
谢谢。
You can try this This will help you to find the shell_exec function errors: 你可以尝试这个这将帮助你找到shell_exec函数错误:
Add 2>&1
to you Command 在命令中添加
2>&1
Check the below changes: 检查以下更改:
$network = '192.168.0.1-255';
$cmd = 'PASSWD="$(zenity --password --title=Authentication)"; echo -e $PASSWD | sudo -S nmap -A ' . $network.' 2>&1 ';
echo $cmd;
$output3 = rtrim(shell_exec($cmd));
echo ($output3);
Tryout out the above code . 试用上面的代码。 i have added 2>&1 at the end of $cmd.
我在$ cmd结束时添加了2>&1。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.