reallydismayed
reallydismayed

Reputation: 101

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...

The following command line works in an Ubuntu terminal window;

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.

If I put the following code in a php file on my local machine, served by 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

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:

xhost local:www-data

but that did not help.

Running, for example, "ls" from shell_exec works fine.

I would welcome any suggestions.

Upvotes: 0

Views: 391

Answers (3)

shashikant kuswaha
shashikant kuswaha

Reputation: 573

You can try this This will help you to find the shell_exec function errors:

Add 2>&1 to you Command

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.

Upvotes: 0

reallydismayed
reallydismayed

Reputation: 101

@danielsedoff I created a file "com.ubuntu.pkexec.nmap.policy" in folder /usr/share/polkit-1 with these contents:

<?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

pkexec --user root nmap -A 192.168.0.1-255

now works as I hoped without needing interactive authorisation from the user. Thanks.

Upvotes: 0

Danny
Danny

Reputation: 588

I would use this:

pkexec nmap -A 192.168.0.1-255

pkexec replaces gksudo in newer distributions.

Upvotes: 0

Related Questions