PHP 8.4.1 Released!

ssh2_fetch_stream

(PECL ssh2 >= 0.9.0)

ssh2_fetch_streamParcours un flux étendu de données

Description

ssh2_fetch_stream(resource $channel, int $streamid): resource

Parcourt un sous-flux alternatif associé à un flux de canal SSH2. Le protocole SSH2 définie actuellement un seul sous-flux, STDERR, qui a un identifiant de sous-flux de SSH2_STREAM_STDERR (défini à 1).

Liste de paramètres

channel

streamid

Un canal de flux SSH2.

Valeurs de retour

Retourne la ressource, représentant le flux demandé.

Exemples

Exemple #1 Ouverture d'un shell et récupération du flux stderr qui lui est associé

<?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_stream, SSH2_STREAM_STDERR);
?>

Voir aussi

add a note

User Contributed Notes 4 notes

up
7
ingo at baab dot de
4 years ago
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_stream, SSH2_STREAM_STDIO);
$err_stream = ssh2_fetch_stream($stdout_stream, SSH2_STREAM_STDERR);

stream_set_blocking($sio_stream, true);
stream_set_blocking($err_stream, true);

$result_dio = stream_get_contents($sio_stream);
$result_err = stream_get_contents($err_stream);

echo
'stderr: ' . $result_err;
echo
'stdio : ' . $result_dio;
?>
up
-1
Ricardo Striquer (ricardophp yohoocombr)
17 years ago
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_stream, SSH2_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);
?>
up
-1
hexer
16 years ago
I successfully used fgets in PHP4

examples

<?php
$stderr
= fgets(ssh2_fetch_stream($channel, SSH2_STREAM_STDERR), 8192);

$str = fgets(ssh2_fetch_stream($channel, SSH2_STREAM_STDIO), 8192);
?>
up
-2
Dennis K.
17 years ago
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_stream, SSH2_STREAM_STDERR);

$dio_stream = ssh2_fetch_stream($stdout_stream, SSH2_STREAM_STDDIO);

stream_set_blocking($err_stream, true);
stream_set_blocking($dio_stream, true);

$result_err = stream_get_contents($err_stream));
$result_dio = stream_get_contents($dio_stream));
?>
To Top