仅当数据可用时才从流中读取Phpseclib ssh2,否则写入可用数据

时间:2013-11-19 21:37:51

标签: php phpseclib

我正在使用phpseclib,基本上我正在尝试做的是,每当从流中接收任何数据时,都会将exec流中的数据写入文件中(而不是等待数据显示并挂起平均时间就像我使用fgets一样)以便我可以将数据写入需要编写的流中,所有这些都在while循环中。

伪代码:

while stream isn't null:

    check for incoming data from the stream, if there is none, then continue to the next part of loop
        if there is data, then write it to file x.txt

    write any data into the socket that needs to be written.

我将如何实施此功能?我尝试了几种方法,但似乎都没有。

2 个答案:

答案 0 :(得分:0)

似乎$callback的{​​{1}}参数可能会执行您想要的操作。例如

Net_SSH2::exec()

失败了......也许你可以做这样的事情(未经测试):

function packet_handler($str)
{
    global $fp;
    fputs($fp, $str);
}

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

你可能也会need a PTY

答案 1 :(得分:0)

或做某事......

$tmp = $cn->exec($cmd, false);
$cn->setTimeout(10);
while (<some conditional>) {
  $tmp = $cn->_get_channel_packet(NET_SSH2_CHANNEL_EXEC);
  switch (true) {
    case === true:
      # Completed (or timed out if you are using setTimeout)
      if ($cn->is_timeout) {
        # Timed out
        break;  # Go through loop again?
      }
      break 2;
    case === false:
      # Disconnect or error
      break 2;
    default:
      # write to your file
      break;
  }

  # Other stuff to do before going back around
}

它会暂停等待10秒,然后再给予控制权。取决于你想要完成的事情。