我在尝试建立与AWS Managed SFTP服务器的连接时遇到问题。使用现有的凭据,我可以使用以下sftp
命令从Windows命令行连接到服务器。这是我的.NET代码:
using (var client = new SshClient(new ConnectionInfo(baseHost, user,
new AuthenticationMethod[]{
new PrivateKeyAuthenticationMethod(user,new PrivateKeyFile[]{
new PrivateKeyFile(keyLocation, pkpassword)
}),
}
)))
{
client.Connect(); // Timeout here
}
上面的代码到达该client.Connect()
行,然后在30秒后超时,但有一个Renci.SshNet.Common.SshOperationTimeoutException
例外。当我查看Wireshark的情况时,我发现sftp
命令行实用程序使用的协议是SSH,而SSH.NET使用TCP,并且数据包大小完全不同。
有人知道我在这里可能会想念的吗?
我在sftp
与上述代码相同的计算机上运行命令行实用程序。下面的第一个Wireshark图像来自上面的C#代码。第二个来自sFTP实用程序:
当我尝试在原始模式下使用PuTTY连接到服务器的端口22时,没有任何响应。
谢谢,吉姆
建立连接后,双方必须发送一个识别字符串。
Both SSH.NET client and Amazon Managed SFTP server fail this requirement. Both first wait for the other side to send the identification string before sending its own. A deadlock is inevitable (interrupted only by a timeout). That also explains why Wireshark does not identify the session as SSH, as there's no data exchanged at all. Hence, there's nothing by which the protocol can be identified.
If you can modify SSH.NET source code, moving this line in Session.Connect
:
SocketAbstraction.Send(_socket, Encoding.UTF8.GetBytes(string.Format(CultureInfo.InvariantCulture, "{0}\x0D\x0A", ClientVersion)));
... above this block:
Match versionMatch;
// Get server version from the server,
// ignore text lines which are sent before if any
while (true)
{
...
}
... should fix the problem.
Also consider reporting the bug to Amazon.
I have reported the bug to SSH.NET including the needed change.
If you cannot change SSH.NET code, you will need to use another SFTP library.
例如,我的WinSCP .NET程序集与Amazon Managed SFTP服务器兼容。
这等效于您的代码:
// Set up session options
SessionOptions sessionOptions = new SessionOptions
{
Protocol = Protocol.Sftp,
HostName = baseHost,
UserName = user,
SshHostKeyFingerprint = ...,
SshPrivateKeyPath = keyLocation,
PrivateKeyPassphrase = pkpassword,
};
using (Session session = new Session())
{
// Connect
session.Open(sessionOptions);
// Your code
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句