pecl-ssh2 and remote command execution
| From: | Radek Hladik | Date: | Mon, 12 Nov 2007 14:05:40 +0000 |
| Subject: | pecl-ssh2 and remote command execution | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-264367@lists.php.net to get a copy of this message | ||
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.
I also would like to see what return value command returned but it can be done via adding second command like ' echo $?'
Radek