【问题标题】:SSH2 returning "resource(2) of type (stream)" (PHP)SSH2 返回“类型(流)的资源(2)”(PHP)
【发布时间】:2012-05-17 18:14:08
【问题描述】:

我正在尝试使用ssh2_exec 函数从带有 PHP 的 ssh2 库的远程服务器运行 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);

}

【问题讨论】:

    标签: php ssh libssh2


    【解决方案1】:

    阅读the documentation

    返回值

    成功返回流,失败返回 FALSE。

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

    例如

    $string = stream_get_contents($stream);
    
    $line = stream_get_line($stream);
    
    $fivebytes = fread($stream, 5);
    

    【讨论】:

    • 以前从未处理过“流”。我将如何从“流”中获取数据?
    • @JoshFoskett:您可以使用stream_get_contents() 读取流直到结束。
    • echo(stream_get_contents($stream));
    • 谢谢大家,我还得加上 stream_set_blocking($stream, true);
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-13
    相关资源
    最近更新 更多