我正在尝试使用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); 


评论关闭
IT序号网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!