1

我正在尝试使用 libssh2-php 库函数从 PHP 连接到 ssh 服务器。这一切都很好,但我不知道如何阅读 PHP 中的登录前横幅。登录前横幅是 SSH 提示登录之前显示的文本,据我所知,我无法从连接对象获取流。

因此,鉴于此代码,有没有办法在登录之前进行读取?

<?php
if (!function_exists("ssh2_connect")) die("function ssh2_connect doesn't exist");
      // log in at server1.example.com on port 22 
if(!($con = ssh2_connect("server1.example.com", 22))){
  echo "fail: unable to establish connection\n";
} else {

 //####### Would really like to get a stream right here to read pre-login banner

// try to authenticate with username root, password secretpassword
if(!ssh2_auth_password($con, "root", "secretpassword")) {
    echo "fail: unable to authenticate\n";
} else {
    // allright, we're in!
    echo "okay: logged in...\n";

    // execute a command
    if (!($stream = ssh2_exec($con, "ls -al" ))) {
        echo "fail: unable to execute command\n";
    } else {
        // collect returning data from command
        stream_set_blocking($stream, true);
        $data = "";
        while ($buf = fread($stream,4096)) {
            $data .= $buf;
        }
        fclose($stream);
    }
 }
}
?>
4

2 回答 2

1

您可以使用phpseclib,一个纯 PHP SSH 实现来做到这一点。横幅应该可以通过$ssh->getLastError()(返回一个字符串)或$ssh->getErrors()(返回一个字符串数组)来获得。

引用它的来源:

// see http://tools.ietf.org/html/rfc4252#section-5.4; only called when the encryption has been activated and when we haven't already logged in
if (($this->bitmap & NET_SSH2_MASK_CONSTRUCTOR) && !($this->bitmap & NET_SSH2_MASK_LOGIN) && ord($payload[0]) == NET_SSH2_MSG_USERAUTH_BANNER) {
    $this->_string_shift($payload, 1);
    extract(unpack('Nlength', $this->_string_shift($payload, 4)));
    $this->errors[] = 'SSH_MSG_USERAUTH_BANNER: ' . utf8_decode($this->_string_shift($payload, $length));
    $payload = $this->_get_binary_packet();
}
于 2013-04-19T15:21:40.927 回答
0

更新(讨论后):

目前,除了自己实现之外,我认为没有办法在 php 中执行此操作。向 ssh2 扩展的开发人员发送功能请求可能是最好的开始方式。


原始(愚蠢)答案:登录后会出现登录横幅

于 2013-04-18T22:00:09.377 回答