ssh2_fetch_stream

(PECL ssh2 >= 0.9.0)

ssh2_fetch_streamFetch an extended data stream

Description

resource ssh2_fetch_stream ( resource $channel , int $streamid )

Fetches an alternate substream associated with an SSH2 channel stream. The SSH2 protocol currently defines only one substream, STDERR, which has a substream ID of SSH2_STREAM_STDERR (defined as 1).

Parameters

channel

streamid

An SSH2 channel stream.

Return Values

Returns the requested stream resource.

Examples

Example #1 Opening a shell and retrieving the stderr stream associated with it

<?php
$connection 
ssh2_connect('shell.example.com'22);
ssh2_auth_password($connection'username''password');

$stdio_stream ssh2_shell($connection);
$stderr_stream ssh2_fetch_stream($stdio_streamSSH2_STREAM_STDERR);
?>

See Also

Коментарии

I got a friend using those functions and he was not able to use this ssh2_fetch_stream function. First of all I got the ssh2_shell sample by webmaster at spectreanime dot com, but this function does not work with his sample, i believe thats because he use fwrite instead of ssh2_shell or ssh2_exec to run the command.

This sample below is to run under a command line and is fully functional. note that i add the sleep as advised by webmaster at spectreanime dot com

<?php
echo "Connexion SSH ";
if (!(
$connection=@ssh2_connect("69.69.69.69"22))) {
   echo 
"[FAILED]\n";
   exit(
1);
}
echo 
"[OK]\nAuthentification ";

if (!@
ssh2_auth_password($connection,"root","YourPassword")) {
   echo 
"[FAILED]\n";
   exit(
1);
}
echo 
"[OK]\n";

$stdout_stream ssh2_exec($connection"/bin/lssss -la /tmp");
sleep(1);
$stderr_stream ssh2_fetch_stream($stdout_streamSSH2_STREAM_STDERR);

echo 
"Erros encontrados!\n------------\n";
while(
$line fgets($stderr_stream)) { flush(); echo $line."\n"; }
echo 
"------------\n";

while(
$line fgets($stdout_stream)) { flush(); echo $line."\n";}

fclose($stdout_stream);
?>
2006-12-18 09:41:33
http://php5.kiev.ua/manual/ru/function.ssh2-fetch-stream.html
Автор:
In addition to the last post from Ricardo Striquer:

Simple block the stream with stream_set_blocking(), and you dont have to sleep() the script...

<?php
stdout_stream 
ssh2_exec($connection"/bin/lssss -la /tmp");

$err_stream ssh2_fetch_stream($stdout_streamSSH2_STREAM_STDERR);

$dio_stream ssh2_fetch_stream($stdout_streamSSH2_STREAM_STDDIO);

stream_set_blocking($err_streamtrue);
stream_set_blocking($dio_streamtrue);

$result_err stream_get_contents($err_stream));
$result_dio stream_get_contents($dio_stream));
?>
2007-01-24 06:28:51
http://php5.kiev.ua/manual/ru/function.ssh2-fetch-stream.html
Автор:
I successfully used fgets in PHP4

examples

<?php
$stderr 
fgets(ssh2_fetch_stream($channelSSH2_STREAM_STDERR), 8192);

$str fgets(ssh2_fetch_stream($channelSSH2_STREAM_STDIO), 8192);
?>
2008-09-10 09:51:04
http://php5.kiev.ua/manual/ru/function.ssh2-fetch-stream.html
In addition to the last post (13 years ago) from Dennis K. I corrected two typos (constant SSH2_STREAM_STDIO was misspellt) and removed (too many) brackets - this code works:

<?php
      $stdout_stream 
ssh2_exec($connection"lsssss -la");

     
$sio_stream ssh2_fetch_stream($stdout_streamSSH2_STREAM_STDIO);
     
$err_stream ssh2_fetch_stream($stdout_streamSSH2_STREAM_STDERR);
     
     
stream_set_blocking($sio_streamtrue);
     
stream_set_blocking($err_streamtrue);
     
     
$result_dio stream_get_contents($sio_stream);
     
$result_err stream_get_contents($err_stream);

      echo 
'stderr: ' $result_err;
      echo 
'stdio : ' $result_dio;
?>
2020-06-25 18:46:59
http://php5.kiev.ua/manual/ru/function.ssh2-fetch-stream.html

    Поддержать сайт на родительском проекте КГБ