【发布时间】:2022-06-14 13:59:39
【问题描述】:
该组织有一个基于 .NET 4.5.2 的集成服务层。它有几个 WCF 服务正在运行。一项服务使用 SFTP 连接到同一台机器上的 SFTP 服务器以读取和移动一些文件。它使用简单的用户名和密码验证。在生产环境中一切正常。
我正在尝试在 Visual Studio 2019 (16.8.6) 中调试解决方案,但无法连接到 SFTP 服务器。它使用 Renci SSH.NET v2016.0.0。升级不是一种选择。我只是想在开发环境中调试一下。
开发环境(Windows Server 2012 R2)已经安装了 OpenSSH(OpenSSH_for_Windows_8.9p1,LibreSSL 3.4.3)。我以 管理员 的身份运行 Visual Studio。
我可以使用 CMD 和 FileZilla 中的特定用户和密码连接到 SFTP。我还创建了一个简单的控制台应用程序,它还能够连接、导航到相关文件夹并读取文件。
然后我创建了一个简单的 WCF 服务和一个服务主机并尝试连接,但是失败了。当它尝试连接时,它会给出错误“现有连接被远程主机强制关闭”。
控制台应用程序连接代码示例,此代码有效。
SftpClient _client = new SftpClient(host, u1, password1);
_client.Connect();
这是来自 WCF 服务的代码示例。
public string GetSFTPConnection(ConnectionDetails connectionDetails)
{
try
{
SftpClient _client = new SftpClient(connectionDetails.Host, connectionDetails.UserName, connectionDetails.Password);
_client.Connect();
if (_client.IsConnected)
{
return "SFTP Connected Successfully!";
}
return "SFTP Connection Failed!";
}
catch (Exception e)
{
return $"SFT Connection Error@ {e}";
}
}
错误:
InnerException = An existing connection was forcibly closed by the remote host
StackTrace = at Renci.SshNet.Session.WaitOnHandle(WaitHandle waitHandle, TimeSpan timeout)
at Renci.SshNet.Session.Connect()
at Renci.SshNet.BaseClient.Connect()
at SFTPServiceNew.Service1.GetSFTPConnection(ConnectionDetails connectionDetails) in C...
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Header />
<s:Body>
<GetSFTPConnectionResponse xmlns="http://tempuri.org/">
<GetSFTPConnectionResult>SFT Connection Error@ Renci.SshNet.Common.SshConnectionException: An existing connection was forcibly closed by the remote host ---> System.Net.Sockets.SocketException: An existing connection was forcibly closed by the remote host
at Renci.SshNet.Abstractions.SocketAbstraction.Read(Socket socket, Byte[] buffer, Int32 offset, Int32 size, TimeSpan timeout)
at Renci.SshNet.Session.SocketRead(Int32 length, Byte[] buffer)
at Renci.SshNet.Session.ReceiveMessage()
at Renci.SshNet.Session.MessageListener()
--- End of inner exception stack trace ---
at Renci.SshNet.Session.WaitOnHandle(WaitHandle waitHandle, TimeSpan timeout)
at Renci.SshNet.Session.Connect()
at Renci.SshNet.BaseClient.Connect()
at SFTPServiceNew.Service1.GetSFTPConnection(ConnectionDetails connectionDetails) in C:...</GetSFTPConnectionResult>
</GetSFTPConnectionResponse>
</s:Body>
</s:Envelope>
它还会根据主机返回不同的错误。
本地主机:
An existing connection was forcibly closed by the remote host
127.0.0.1:
Server response does not contain SSH protocol identification.
环境IP地址(10.xx.xx.xx):
Permission denied (password).
计算机名:
An existing connection was forcibly closed by the remote host
我感觉这可能与 WCF 服务及其主机在调试模式下的运行方式有关。以及 SSH 中的用户配置。但我很困惑如何弄清楚。我对 WCF 服务或 OpenSSH 没有太多经验。
我还尝试使用 blow 命令将我的用户帐户添加到 OpenSSH。但它失败了。
mkpasswd -l -u [my_user_account] >> etc\passwd
The system cannot find the path specified.
OpenSSH 文件夹中似乎也没有任何 etc\passwd 文件。
Sample WCF service application structure
Sample WCF service when debugging
如果有人能指出我正确的方向,非常感谢。
【问题讨论】:
-
你能打开一个普通的
Socket连接并从中读取SSH标识字符串吗? -
嗨,马丁,感谢您的建议。我尝试了一个带有套接字的示例,发现有另一个 SFTP 服务器正在运行。这台机器同时运行了 OpenSSH 和 Cerberus。我关闭了 OpenSSH 并能够连接到该服务。
标签: c# .net wcf openssh ssh.net