【问题标题】:phpseclib - exit callback?phpseclib - 退出回调?
【发布时间】:2015-06-26 13:56:45
【问题描述】:

我正在使用 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');
?>

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

注意:在 SSH2.php 第 2676 行的连接过早关闭

注意:SSH2.php 2959 行中的服务器关闭连接

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

【问题讨论】:

    标签: php phpseclib


    【解决方案1】:

    您可以忽略通知,阻止它们从 php.ini 显示 或者在没有回调的情况下使用 php sleep(x) seconds 就可以了。

    $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
    

    【讨论】:

    • 谢谢 - 我还发现我可以运行 $ssh-&gt;exec("timeout 5 ping 127.0.0.1"); //run the ping,这似乎可行 :)
    猜你喜欢
    • 2017-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多