471,319 Members | 1,585 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,319 software developers and data experts.

ssh2 & php & graphical apps

I'm trying to use the ssh2 functions to run a graphical app. I have no
problem opening ssh2_shell and running something like "/bin/ls /tmp".
However when I try to do something like:
fwrite($stdio,"/usr/X11R6/bin/xclock -display some-other-machine:0\n");

It doesn't work. I've tried wrapping the xclock command in a shell
script and a C program and still no luck. In both instances I can ssh
into the remote machine and run the commands just fine. Is this
something that just doesn't work? Or does someone have a tip for how
to get that to display back? Should I be looking at ssh2_exec?

-Brian Vincent

Full code below using some sample PHP code:
<?php
echo "Connexion SSH ";
if (!($resource=@ssh2_connect("10.72.xx.xx"))) {
echo "[FAILED]<br />";
exit(1);
}
echo "[OK]<br />";

// Authentification by login/passwd
echo "Authentification ";
if (!@ssh2_auth_password($resource,"myusername","mypa ssword")) {
echo "[FAILED]<br />";
exit(1);
}
echo "[OK]<br />";

// We need a shell
echo "Shell stdio ";
if (!($stdio = @ssh2_shell($resource,"xterm"))) {
echo "[FAILED]<br />";
exit(1);
}
echo "[OK]<br />";

// Execution of any command
// Be careful to add an '\n' at the end of the command
$command = "/usr/X11R6/bin/xclock -display
some-other-machine:0\n";
fwrite($stdio,$command);

// IMPORTANT
// For some obscur reasons, just after ur command, u need to
make a sleep to be sure, that the command has reached the server and is
running
sleep(1);

// Then u can fetch the stream to see what happens on stdio
while($line = fgets($stdio)) {
flush();
echo $line."<br />";
}
// It's always cleaner to close all stream
fclose($stdio);
?>

Jul 17 '05 #1
7 3120
br***********@gmail.com wrote:
I'm trying to use the ssh2 functions to run a graphical app. I have no
problem opening ssh2_shell and running something like "/bin/ls /tmp".
However when I try to do something like:
fwrite($stdio,"/usr/X11R6/bin/xclock -display some-other-machine:0\n");

It doesn't work.


How are you running your script?

Jul 17 '05 #2
I'm not sure I know what you mean. I'm embedding that PHP script in
index.php and then opening the page in a remote web browser on
some-other-machine. I've actually heard rumors that this will work,
but I can't seem to figure it out.

-Brian

Jul 17 '05 #3
br***********@gmail.com wrote:
I'm not sure I know what you mean. I'm embedding that PHP script in
index.php and then opening the page in a remote web browser on
some-other-machine. I've actually heard rumors that this will work,
but I can't seem to figure it out.


The question should have been:
Who/what is executing the script?

My guess is that you don't know how X11 authenticates and authorizes the
usage of the display (==server) by an application (==client).

If you run the script by accessing an URL on a httpd, the httpd must
have access to the server that will display a client. So where is the
display and where is the httpd running?

Jul 17 '05 #4
> The question should have been:
Who/what is executing the script? My guess is that you don't know how X11 authenticates and authorizes the
usage of the display (==server) by an application (==client).


Ah. I see.

This is part of the reason I'm using ssh.

So the process should be:

1. me (on machine #1) hits the web server
2. the PHP script (on machine #2) initiates an ssh connection using
ssh2_shell either to itself or to machine #3 (I don't think it matters)

3. the ssh shell authenticates as a specific user and attempts to run
a graphical app back to machine #1.

Now, on machine #1 I've just done an "xhost +" to completely disable
authentication checking (for testing only).

I know X is fine since I can ssh from machine #1 and run the same
command line manually on machine #2 to fire up xclock. I suspected
maybe something wasn't being set up correctly in the environment, but
comparing the 'env' output from a normal ssh session and one initiated
by ssh2_shell() doesn't show many differences. No problems running
/usr/bin/env through ssh2_shell and capturing the output of the stream,
so I know non-graphical apps work.

Now, maybe if I tell you the Big Picture idea you can think of another
way to do it. I'm basically trying to create an application server. A
user will need to authenticate and once they do so it will
automatically fire up a graphical app residing on the remote server and
display it back on the client. The apps need to utilize custom
settings based on the user, so using something like ssh2_shell to
execute it seems to a logical way to do it.

Thanks for thinking about this so far. I'm somewhat stumped.

-Brian

Jul 17 '05 #5
br***********@gmail.com wrote:
Now, maybe if I tell you the Big Picture idea you can think of another
way to do it. I'm basically trying to create an application server. A
user will need to authenticate and once they do so it will
automatically fire up a graphical app residing on the remote server and
display it back on the client. The apps need to utilize custom
settings based on the user, so using something like ssh2_shell to
execute it seems to a logical way to do it.

Thanks for thinking about this so far. I'm somewhat stumped.


Me too, after an "xhost +" (which is offcourse BAD (ssh-ing into an
account that has any valid X display should be enough for your
solution)) any input should be allowed.

The only small difference I can think of might be that if you run the
commands yourself you are actually getting the results by tunneling the
X protocol so the effect will be that remote xclients effectively
connect to "localhost". Your httpd doesn't have a valid display so any
ssh session started from it will lack the X forwarding, if you try to
display to a remote machine it should talk to it thru the usual TCP
networking protocol. In many cases the Xserver will be started by
[xkg]dm, some installations (eg Debian) will disable tcp support in the
Xserver so that remote hosts can't connect with a simple
"-display remote:0.0". Scan for '-nolisten tcp' to see if this might be
the cause.

If this isn't it, I can't think of anything else :)

Jul 17 '05 #6
> "-display remote:0.0". Scan for '-nolisten tcp' to see if this might be
the cause.


Already removed. Previous to that I couldn't run the X app over a
normal ssh connection and afterwards I could.

-Brian

Jul 17 '05 #7
Alright.. well, in case someone else ever runs into this I should post
something that works. I still have a lot to figure out. For example,
I can get xterm to run but not xclock. I also used ssh2_exec() rather
than ssh2_shell(). That's fine for me though, ssh2_exec() will work.
Anyway, I still have a long way to go to turning this into production
quality, but the basic concept seems to be there. Also, I'm running
all this locally right now. The web server, the ssh calls, and the
display are all on one box. Eventually I think I need to move it onto
three boxes. Anyway, this works (assuming you have a bunch other
little things configured correctly and xhost + set, which is a bad
idea.)

By the way, with the case of xclock failing, I could at least get the
process running on my box with ssh2_exec. I have no idea why it
wouldn't display, but it sat there in the process list.

It is somewhat neat to reload a web page and watch xterm pop up.

<?php
echo "Connexion SSH ";
if (!($resource=@ssh2_connect("10.72.64.xx"))) {
echo "[FAILED]<br />";
exit(1);
}
echo "[OK]<br />";

// Authentication by login/passwd
echo "Authentication ";
if (!@ssh2_auth_password($resource,"myusername","mypa ssword")) {
echo "[FAILED]<br />";
exit(1);
}
echo "[OK]<br />";

// Exec a program
echo "Shell exec ";
if (!($stdio = @ssh2_exec($resource,'/usr/X11R6/bin/xterm
-display 10.72.64.xx:0'))) {
echo "[FAILED]<br />";
exit(1);
}
echo "[OK]<br />";

// It's always cleaner to close all stream
fclose($stdio);
?>

-Brian

Jul 17 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by gonchi | last post: by
2 posts views Thread by Venkat B | last post: by
9 posts views Thread by Bryan Olson | last post: by
reply views Thread by Bernhard Günther | last post: by
4 posts views Thread by Adam Knight | last post: by
reply views Thread by FishGills | last post: by
reply views Thread by YellowFin Announcements | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.