How to execute an interactive command from PHP?

I need to execute the kdiff3 command on my desktop machine (localhost) from a PHP script (using a browser, not a command line). I gave permission to use the www data of a user running scripts to execute kdiff3 using visudo. In fact, if I log in as www data, I can execute it without any problems (sudo kdiff3 ..., it is configured to not ask for a password at all).

The problem is that I am trying to execute this command with a PHP script. I tried this:

$output = shell_exec("sudo kdiff3 -m $file.def.old $file $file.def -o $file"); 

and nothing happens (NULL output). If I try a non-interactive command like ls, it works:

 $output = shell_exec("ls"); 

What's happening? Why is it impossible to execute an interactive command?

+4
source share
5 answers

kdiff3 is an interactive GUI program, so it uses KDE and Qt and requires an X11 server .

And inside the web server (i.e. in PHP for Apache or Lighttpd ) you do not have an X11 server.

Thus, it is not possible to use kdiff3 inside a PHP script (if the web server is not running on your Linux computer on the desktop, and then you need to configure the environment accordingly, especially DISPLAY and, possibly, the XAUTHORITY environment variable). However, you can run a command-line program like diff3 ... (using popen tricks).

+2
source

Try passthru($cmd) ;

This will allow you to use custom I / O on the terminal screen.

+8
source

The problem is that sudo waiting for user input, as you said, it is "interactive" - ​​it asked you to enter a password.

You can provide it with proc_open() to execute the command and get individual I / O streams so you can parse the output and enter the password if necessary.

I came across scripts that use this approach echo "suPassword\n" | sudo cmd echo "suPassword\n" | sudo cmd to do this, but I personally found that it hit a bit and missed.

+1
source

Try using exec:

$ Output = exec ($ command, $ dom, $ return)

0
source

Each interactive command expects a working stdin - this is not set using shell_exec();

Use popen() and friends or redirect input from a file. You may still be out of luck if the file in question checks ISATTY

0
source

Source: https://habr.com/ru/post/1393339/


All Articles