Chris napsal(a):
Radek Hladik wrote:
Hi,
I need to run remote command (by ssh), send it some data and
receive its output and return value. I could run command like
ssh user@server 'command' via proc_open or something like that. But
I've discovered ssh2 pecl-extension and I liked the idea of using ssh
directly from PHP. However I was not able to figure out, how to close
input stream and how to obtain return value. I.e. let's have this code:
$stream = ssh2_exec($connection,"head -n 4");
stream_set_blocking($stream, true);
fputs ($stream,"line1\nline2\n");
while($o=fgets($stream)){ echo $o.'<br>';}
fclose ($stream);
The problem is that this one never ends as 'head -n 4' waits for two
more lines as it does not know, that I've finished sending data. If I
close the stream after sending two lines, then I can not get output
(of course :-) ). I've tried sending chr(3), chr(4), chr(0) to
indicate end of input but nothing works.
head will sit there waiting forever until you send another 2 lines - try
it in your ssh session.
Send 4 lines:
line 1\n
line 2\n
\n
\n
and you'll get what you want.
Thanks for your reply.
I know that head will wait till it receives 4 lines but it should also
end when its input stream gets closed. That's why I've choosed head for
my example. If I use proc_open on local head then I can selectively
close its input and still get its output.
In real use I will use commands which need input to be closed like sed,
cat, etc.... For simplicity imagine this example: (I know this is
stupid, but it's just an example :-) )
$stream = ssh2_exec($connection,"tac");
How can I signal to tac that input is over? In shell I would hit ctrl+d
(chr(4))
Radek
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php