Delicious Bookmark this on Delicious Share on Facebook SlashdotSlashdot It! Digg! Digg



PHP : Function Reference : Secure Shell2 Functions : ssh2_fetch_stream

ssh2_fetch_stream

Fetch an extended data stream (PECL ssh2:0.10-0.9)
resource ssh2_fetch_stream ( resource channel, int streamid )

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

Code Examples / Notes » ssh2_fetch_stream

dennis k.

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));
?>


ricardo striquer ricardophp yohoocombr

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);
?>


Change Language


Follow Navioo On Twitter
ssh2_auth_hostbased_file
ssh2_auth_none
ssh2_auth_password
ssh2_auth_pubkey_file
ssh2_connect
ssh2_exec
ssh2_fetch_stream
ssh2_fingerprint
ssh2_methods_negotiated
ssh2_publickey_add
ssh2_publickey_init
ssh2_publickey_list
ssh2_publickey_remove
ssh2_scp_recv
ssh2_scp_send
ssh2_sftp_lstat
ssh2_sftp_mkdir
ssh2_sftp_readlink
ssh2_sftp_realpath
ssh2_sftp_rename
ssh2_sftp_rmdir
ssh2_sftp_stat
ssh2_sftp_symlink
ssh2_sftp_unlink
ssh2_sftp
ssh2_shell
ssh2_tunnel
eXTReMe Tracker