...

View Full Version : PHP to Wait on Shell Script Requiring User Input



TheShaner
04-29-2009, 05:36 PM
I have a shell script that requires user input. This script executes xwininfo, which is a utility for the X server that waits for the user to click on a window so that it can return info about it.

When I use PHP's exec, shell_exec, or system functions, they all execute the script as if xwininfo was never called. My guess is that when PHP executes a shell script, it can't transfer the focus to the script, and thus it never waits for user input and goes on like it doesn't exist.

My question is whether I can execute a shell script from a web page that gains the focus, receives user input, and outputs its info, while PHP waits for the script to finish before serving the page back to the browser? This just doesn't seem possible from my testing so far.

Any guidance would be great. Thanks.

-Shane

oesxyl
04-29-2009, 06:03 PM
try:

http://www.php.net/manual/en/function.passthru.php

best regards

TheShaner
04-29-2009, 08:24 PM
I haven't tried it yet, but the only difference between the passthru function and the exec function is just output. You get raw binary data with the passthru. I'm not looking for that. I'm looking for the PHP server to wait on serving the page while the shell script takes focus and does what it needs to do first.

-Shane

oesxyl
04-29-2009, 08:50 PM
I haven't tried it yet, but the only difference between the passthru function and the exec function is just output. You get raw binary data with the passthru. I'm not looking for that. I'm looking for the PHP server to wait on serving the page while the shell script takes focus and does what it needs to do first.

-Shane

This function should be used in place of exec() or system() when the output from the Unix command is binary data which needs to be passed directly back to the browser
that's all what you get, :) this is not Star Wars, and php is not the Force, :)

when you run xwininfo all mouse events will be directed to the x server untill somebody will click somewhere on that server( the one who run x not the one with the browser if there are different) and then, probably, the output of xwininfo will apear in browser.
I didn't try and I'm not sure about this scenario but this is how I think will work, :)

best regards

oesxyl
04-29-2009, 09:12 PM
I haven't tried it yet, but the only difference between the passthru function and the exec function is just output. You get raw binary data with the passthru. I'm not looking for that. I'm looking for the PHP server to wait on serving the page while the shell script takes focus and does what it needs to do first.

-Shane
back after a short test. I was wrong about how it work. It don't do nothing.
script:


<?php
passthru('/usr/bin/xwininfo',$result);
echo $result;
?>

will output 1 and in xwininfo man page I didn't find when xwininfo return a status like that.
sorry for negative answer but look to the bright side of that story, you don't waste time with this and you can focus to search another way to achive what you want, :)

best regards

TheShaner
04-29-2009, 09:34 PM
that's all what you get, :) this is not Star Wars, and php is not the Force, :)
Unnecessary comment. I'm well aware of most of the capabilities and limitations of PHP and other server-side languages. I'm not looking for PHP to do any magic; just execute a shell script that requires a mouse event in order to finish processing.



when you run xwininfo all mouse events will be directed to the x server untill somebody will click somewhere on that server( the one who run x not the one with the browser if there are different) and then, probably, the output of xwininfo will apear in browser.
I didn't try and I'm not sure about this scenario but this is how I think will work, :)
You're not understanding my dilemma. The problem is not receiving output from the script. I already have web pages that run shell scripts and output back to the browser. My problem is that when PHP executes xwininfo, it does not wait for a mouse event and/or cannot detect the mouse event. My belief is that there is an issue with a web page calling a shell script that requires user input or interactions, i.e. mouse events.

In my experience, when a shell script is executed from within PHP, the server will wait for the script to finish before the page is served up again. There are ways around having your server wait for a script to finish before serving it up (which I've also done before), but in this case, I would actually like that to happen. I want the server to wait for the script to finish executing. The script should not finish until you 'left click' a window.

In my test cases, however, when PHP executes the script, there is no opportunity to 'left click'. It's as if xwininfo never executes inside the script. I can't figure out whether this is because PHP has problems calling scripts that require user input/events, or if it has something to do with a user/permission problem of the apache user possibly. The apache server and PHP module all run on the same box that I'm attempting to use this script on. And yes, the script works when it's called from the command line.

So what I'm looking for is anyone with experience with having PHP call a shell script that requires user input and/or mouse events. It may or may not be possible, and so I'm hoping there is someone out there that has tried something similar before.

Thanks,

Shane

TheShaner
04-29-2009, 09:42 PM
<?php
passthru('/usr/bin/xwininfo',$result);
echo $result;
?>

When you ran that test, were you given the typical + icon and a chance to select a window before it echoed back your result? Did the server wait for you to click? Theoretically, I would think that your browser would show that it's loading as your server waits for you to click a window so that it can finish executing the xwininfo command and serve up your page.

My guess is it didn't do that for you and that is exactly the dilemma I'm having.

-Shane

oesxyl
04-29-2009, 10:07 PM
When you ran that test, were you given the typical + icon and a chance to select a window before it echoed back your result? Did the server wait for you to click? Theoretically, I would think that your browser would show that it's loading as your server waits for you to click a window so that it can finish executing the xwininfo command and serve up your page.

My guess is it didn't do that for you and that is exactly the dilemma I'm having.

-Shane
it's no dilemma here, when you run a script in browser it run in a different envoirment.
try this:


<?php
exec('/usr/bin/xwininfo',$result,$output,$result);
print_r($output);
echo $result;
?>

run in the browser, result will be:


Array ( ) 1

run same script from comand line with php -f script-name and will wait until you click on something and output the result. with a 0 status, that mean ok.
the problem is that runing in the browser it can't find x server.
Anyway have no sense what you want because x server run on one server, let's say A, and the browser can run on another server, let's say B separated by net.
how do you expect to click with your mouse on B and process the event on A?

I don't know if is what you want but for remote x apps there is alread a mechanism. Let's me know if this is what you intend to do.

best regards

TheShaner
04-29-2009, 10:21 PM
it's no dilemma here, when you run a script in browser it run in a different envoirment.
try this:


<?php
exec('/usr/bin/xwininfo',$result,$output,$result);
print_r($output);
echo $result;
?>

run in the browser, result will be:


Array ( ) 1


Exactly. That is the problem for me in which I'm trying to find a solution to. The xwininfo command failed and gave you that output. I can't have that.


run same script from comand line with php -f script-name and will wait until you click on something and output the result. with a 0 status, that mean ok.
I figured it probably would, but wasn't sure since I haven't tested that yet.


the problem is that runing in the browser it can't find x server.
Now this may be the case. I need to do some more testing. I need to find a way for a web browser to execute xwininfo.


Anyway have no sense what you want because x server run on one server, let's say A, and the browser can run on another server, let's say B separated by net.
how do you expect to click with your mouse on B and process the event on A?

I don't know if is what you want but for remote x apps there is alread a mechanism. Let's me know if this is what you intend to do.
This project is an internal project. It will not be on the internet. The web page will always be displayed on the same box as the apache server and X server. There will not be any remoting when utilizing this page and script.

-Shane

oesxyl
04-29-2009, 10:37 PM
I need to find a way for a web browser to execute xwininfo.

This project is an internal project. It will not be on the internet. The web page will always be displayed on the same box as the apache server and X server. There will not be any remoting when utilizing this page and script.
look over expect, maybe could help you:

http://www.php.net/manual/en/book.expect.php

unfortunately I don't have installed this extension and I can't do tests.

best regards

TheShaner
04-30-2009, 02:52 PM
look over expect, maybe could help you:

http://www.php.net/manual/en/book.expect.php

unfortunately I don't have installed this extension and I can't do tests.
I'll have to see if I can install that extension for testing.

Here is my test case called test.php:

<?php
ini_set('display_errors', 1);
ini_set('error_reporting', E_ALL);

$cmd = 'xwininfo | grep xwininfo | grep "Window id:" | sed s/[^\"]*// | sed s/\"//g';
$output = shell_exec($cmd);
echo 'Window Title: ' . trim($output) . "\n";
?>
If you run it command line with php -f test.php, it works perfectly.

However, if you bring it up in a web browser on the same machine, you never get a chance to click a window and of course get an empty window title.

-Shane



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum