phpseclib - 退出回调?

phpseclib - exit callback?

我正在使用 SSH2 示例中的这段代码:

<?php
include('Net/SSH2.php');

$ssh = new Net_SSH2('www.domain.tld');
if (!$ssh->login('username', 'password')) {
    exit('Login Failed');
}

function packet_handler($str)
{
    echo $str;
}

$ssh->exec('ping 127.0.0.1', 'packet_handler');
?>

效果很好,但我想在 xx 秒后退出直播。

所以我修改如下:

<?php
include('Net/SSH2.php');
$st = time();
$dur = 10;

$ssh = new Net_SSH2('www.domain.tld');
if (!$ssh->login('username', 'password')) {
    exit('Login Failed');
}

function packet_handler($str)
{
    global $st, $dur, $ssh;
    echo $str;
    $now = ceil(time() - $st);
    if ($now >= $dur) { $ssh->disconnect(); unset($ssh); }
}

$ssh->exec('ping 127.0.0.1', 'packet_handler');
?>

这有效并退出数据包处理程序,但我收到以下通知:

Notice: Connection closed prematurely in SSH2.php on line 2676

Notice: Connection closed by server in SSH2.php on line 2959

谁能告诉我如何在没有通知消息的情况下关闭此连接?

您可以忽略这些通知,阻止它们从 php.ini 中显示 或者不用回调,使用 php sleep(x) 秒就可以了。

$ssh->enablePTY(); 
$ssh->exec("ping 127.0.0.1"); //run the ping
sleep(3); // sleep for 3 seconds
$ssh->write("\x03"); //send CTRL+C to terminate the ping command

print $ssh->read(); // dump the output

$ssh->disconnect(); unset($ssh); //disconnect