使用 SSH.NET 连接到 OpenSSH 7.4p1 失败并显示“服务器响应在位置包含空字符”但在 WinSCP 中有效

Posted

技术标签:

【中文标题】使用 SSH.NET 连接到 OpenSSH 7.4p1 失败并显示“服务器响应在位置包含空字符”但在 WinSCP 中有效【英文标题】:Connecting with SSH.NET to OpenSSH 7.4p1 fails with "The server response contains a null character at position" but works in WinSCP 【发布时间】:2021-04-22 22:14:23 【问题描述】:

我正在尝试使用 SSH.NET (2020.0.0) 连接到 SFTP 服务器。

我的代码看起来很简单:

try

    var x  = new ConnectionInfo(FtpIpAddress, 22, FtpUser,
        new PasswordAuthenticationMethod(FtpUser, FtpPw));

    using (var sftpClient = new SftpClient(x))
    
       sftpClient.Connect();
       sftpClient.Disconnect();
    

catch (Exception e)

    Console.WriteLine(e.Message);

但是当我运行代码时,出现以下错误:

The server response contains a null character at position 0x00000028:  
00000000  53 53 48 2D 32 2E 30 2D 4F 70 65 6E 53 53 48 5F  SSH-2.0-OpenSSH_  
00000010  37 2E 34 70 31 20 44 65 62 69 61 6E 2D 31 30 2B  7.4p1 Debian-10+  
00000020  64 65 62 39 75 32 0A 00                          deb9u2..  
A server must not send a null character before the Protocol Version Exchange is complete.

我不知道如何解决它,当手动使用 WinSCP 时它工作正常,当使用 WinSCP.NET 时,它与代码一起工作正常。但是我不能在 Linux 发行版上使用 WinSCP.NET。

所以有人知道出了什么问题吗?

【问题讨论】:

【参考方案1】:

SSH.NET (2020.0.0) 要求 SSH 服务器版本字符串以 CRLF (0D0A) 序列结尾。

您的服务器似乎只发送 LF (0A)。

WinSCP 更宽容——它对 LF 很满意。

检查 OpenSSH 源代码,似乎 OpenSSH 7.4p1(仅该特定版本)仅发送 LF。旧版本没有这个问题。它已在 OpenSSH 7.5 中修复。似乎他们甚至不认为这是一个错误,因为change 不包含在7.5 release notes 中。

正如您自己发现的那样,SSH.NET 2016.1.0 也只对 LF 感到满意。

SSH.NET 2020.0.1 专门针对this issue 发布。


可能导致相同错误消息的完全不同的问题:"The server response contains a null character" when connecting to FTP site on port 990 using SSH.NET

【讨论】:

以上是关于使用 SSH.NET 连接到 OpenSSH 7.4p1 失败并显示“服务器响应在位置包含空字符”但在 WinSCP 中有效的主要内容,如果未能解决你的问题,请参考以下文章

使用 Net::OpenSSH 和 Solaris SSH 客户端的“错误配置选项:ServerAliveInterval”

SSH.NET - 消息类型 80 无效

使用 SSH.NET 库通过 SSH 连接到 MySQL

使用 SSH 公钥/私钥连接到 sFTP 服务器。错误 Invalid private key file. 使用 SSH.NET

无法通过运行 OpenSSH 的 SSH 连接到 Windows 10

在 Perl 的 ssh 连接中捕获错误主机名的错误消息(使用 Net::OpenSSH)