每行输出ssh2流

时间:2013-09-22 17:23:43

标签: php ssh libssh2

我是PHP的ssh2中的新手,需要一些帮助。

我试图逐行回显php ssh2输出流

if($ssh = ssh2_connect('127.0.0.1', 22)) {
    if(ssh2_auth_password($ssh, 'root', 'password')) {
        $stream = ssh2_exec($ssh, 'ifconfig');
        stream_set_blocking($stream, true);
        $data = '';
        while($buffer = fread($stream, 4096)) {
            $data .= $buffer;
        }
        fclose($stream);
        echo $data; // user
    }
}

但是输出没有逐行显示。我需要做什么来逐行获取输出?就像ssh一样?

1 个答案:

答案 0 :(得分:0)

使用fgets()。它将读取缓冲行:

while($line = fgets($stream)) {
    echo $line;
}