2012-05-17 26 views
1

我试图从远程服务器运行一个SSH命令,使用ssh2函数,使用ssh2函数库。SSH2返回“类型(流)的资源(2)”(PHP)

非但没有什么我想从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); 

} 

回答

4

阅读the documentation

返回值

返回成功流或失败时返回FALSE。

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

E.g.

$string = stream_get_contents($stream); 

$line = stream_get_line($stream); 

$fivebytes = fread($stream, 5); 
+0

以前从来没有处理过“流”。我如何才能从“流”中获取数据? –

+0

@JoshFoskett:您可以使用['stream_get_contents()'](http://php.net/stream_get_contents)读取流直到结束。 –

+0

echo(stream_get_contents($ stream)); – Interrobang