gpt4 book ai didi

c# - 随机获取 Renci.SshNet.SftpClient.Connect 抛出 SshConnectionException

转载 作者:太空狗 更新时间:2023-10-29 19:43:29 25 4
gpt4 key购买 nike

我已经看到关于此错误的其他线程,但我随机遇到此错误。在 30 个连接中,有 12 个出现此错误。试图了解这是为什么,以及可能的解决方案是什么。

using (SftpClient client = new SftpClient(sftpHost, sftpPort, sftpUser, sftpPassword))
{
client.Connect();
}

抛出这个异常:

Renci.SshNet.Common.SshConnectionException: Server response does not contain SSH protocol identification

最佳答案

这听起来可能微不足道,但我已经尝试了 Renci.SshNet.Async 包并且它没有任何问题。以下可能是解决方案或替代解决方案出现问题的原因。

  1. Renci.SshNet.Async 可能会在特定环境或特定组合中出现错误或问题。这里有人提到同样的问题,没有解决方案https://github.com/sshnet/SSH.NET/issues/377
  2. 有人遇到过同样的问题并提到了解决方案,一个是重试nRenci.SshNet : "server response does not contain ssh protocol identification"
  3. 问题不在于您的客户,而或多或少在于您的房东。它可以是很多事情或问题。
  4. 最后我会尝试使用 WinSCP这也是 .net 公认的库 ( nuget )。我自己也尝试过,到目前为止从未遇到过任何问题。我建议您获取它并尝试将其作为当前解决方案的替代方案,看看是否有帮助。如果 WinSCP 工作,则第 1 点有效,如果您遇到与 WinSCP 相同的问题,则第 3 点有效。这样我们就可以得出结论并缩小问题范围。

此处示例由 WinSCP https://winscp.net/eng/docs/library_examples 提供.

我的测试示例是:

This is just playground example to see if things are running, when used in production, please modify it. Further more thanks to @MartinPrikry adding this note: If you have set SshHostKeyFingerprint correctly, then do not set GiveUpSecurityAndAcceptAnySshHostKey.

public static int WinSCPListDirectory()
{
try
{
var sessionOptions = new SessionOptions
{
Protocol = Protocol.Sftp,
HostName = "xxx.xxx.xxx.xxx",
UserName = "username",
Password = "password",
SshHostKeyFingerprint = "ssh-rsa 2048 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx",
GiveUpSecurityAndAcceptAnySshHostKey = true
};

using (var session = new Session())
{
session.Open(sessionOptions);

var directory = session.ListDirectory("/var/www");

foreach (RemoteFileInfo fileInfo in directory.Files)
{
Console.WriteLine(
"{0} with size {1}, permissions {2} and last modification at {3}",
fileInfo.Name, fileInfo.Length, fileInfo.FilePermissions,
fileInfo.LastWriteTime);
}
}

return 0;
}
catch (Exception e)
{
Console.WriteLine("Error: {0}", e);
return 1;
}
}

这个例子应该返回一个目录列表

相关链接:

关于c# - 随机获取 Renci.SshNet.SftpClient.Connect 抛出 SshConnectionException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54523798/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com