php - SSH2返回 “resource(2) of type (stream)”(PHP)

标签 php ssh libssh2

我正在尝试使用ssh2函数从具有PHP的ssh2_exec库的远程服务器上运行SSH命令。

我没有从SSH得到我想要的东西,而是得到了:

resource(2) of type (stream)

有时候23(如果很重要)。

这是我尝试使用的代码:
<?php

$connection = ssh2_connect('ssh.example.com', 22);

ssh2_auth_password($connection, 'root', 'password');

if($output = ssh2_exec($connection, 'uptime')) {

    var_dump($output);

}

工作解决方案:
<?php

$connection = ssh2_connect('ssh.example.com', 22);

ssh2_auth_password($connection, 'root', 'password');

if($output = ssh2_exec($connection, 'uptime')) {

    stream_set_blocking($output, true);

    echo stream_get_contents($output);

}

最佳答案

阅读the documentation:

Return Values

Returns a stream on success or FALSE on failure.



stream是类似文件的对象。您可以使用stream functionsfread之类的文件句柄函数从中获取数据。

例如。
$string = stream_get_contents($stream);

$line = stream_get_line($stream);

$fivebytes = fread($stream, 5);

关于php - SSH2返回 “resource(2) of type (stream)”(PHP),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10641242/

相关文章:

multithreading - Perl - 如何制作特定于各个线程的库

c - 如果接受 key 请求,libssh2 公钥身份验证仅需要密码

php - 使用字符串路径从数组中删除元素

php - 在模式对话框中隐藏服务器端数据

python - 没有 "nohup"python 命令结果

ssh - 如何从配置文件启用 SSH 详细模式

php - 如何访问在另一个 PHPUnit 测试中初始化的对象

php - php 结果错误

bash - 从此处开始的 SSH session Doc 未正确退出

C++ Libssh -sever/client file transfer over the lan 例子