简体   繁体   English

使用zenity的bash脚本在终端中工作但不在php shell_exec中工作

[英]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.

相关问题 从终端运行shell脚本有效,但从PHP shell_exec运行时不起作用 - Running shell script from terminal works but doesn't work when running from PHP shell_exec 命令通过终端正常工作,但不能通过shell_exec php运行 - command works fine through terminal but not shell_exec php php shell_exec在浏览器中不起作用,但在终端中起作用 - php shell_exec doesn't work in browser but works in terminal PHP和FFmpeg:在终端上工作,但在shell_exec中时出现ffmpeg错误 - PHP and FFmpeg : works on terminal but ffmpeg error when in shell_exec 使用shell_exec进行的GPG加密不适用于浏览器,但适用于终端 - GPG encryption using shell_exec not working with the browser but works with the terminal AWS CLI命令可在Bash上运行,但不适用于PHP shell_exec() - AWS CLI command works on Bash, but not with PHP shell_exec() 在终端中运行而不在 Mac 中的 shell_exec php 脚本中运行的代码 - code running in terminal and not running in shell_exec php script in Mac 通过php shell_exec执行bash脚本的权限错误 - Permission error by execute bash script via php shell_exec Bash 在 PHP 中使用 shell_exec 调用的脚本永远不会完成 - Bash script called with shell_exec in PHP never finish 使用shell_exec(bash read)的PHP在tty上有效,但在输入管道输入并且没有换行符时无效 - PHP using shell_exec(bash read) works on tty but not when input is piped and has no newline
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM