简体   繁体   English

无法使用 VB.NET 和 SSH.NET 连接到 SSH/SFTP 服务器 – 但 WinSCP 和 FileZilla 可以连接

[英]Cannot connect to SSH/SFTP server with VB.NET and SSH.NET – but WinSCP and FileZilla can connect

For years the code below has worked.多年来,下面的代码一直有效。 Then on April 11th it stopped.然后在 4 月 11 日它停止了。 I noticed a warning in Visual Studio that the SSH.NET package was out of date, so I updated the package... still doesn't work.我注意到 Visual Studio 中的一个警告说 SSH.NET 包已过期,因此我更新了该包...仍然不起作用。

Protected Sub FTPUpload(ByVal fileToBeUploaded As String, uploadedFilename As String)
    Try
        Using sftp As New SftpClient(sFtpServer, sFtpCreditUser, sFtpCreditPW)
            sftp.Connect()
            Dim upFile As Stream = File.OpenRead(fileToBeUploaded)
            sftp.UploadFile(upFile, uploadedFilename)
            upFile.Close()
            sftp.Disconnect()
        End Using
    Catch ex As Exception
        MsgBox(ex.Message & Chr(13) & Chr(10) & uploadedFilename)
    End Try
End Sub 

Exception thrown:抛出异常:

Renci.SshNet.Common.SshConnectionException in Renci.SshNet.dll Renci.SshNet.dll 中的 Renci.SshNet.Common.SshConnectionException
Renci.SshNet.Common.SshConnectionException: An established connection was aborted by the software in your host machine. Renci.SshNet.Common.SshConnectionException:已建立的连接被主机中的软件中止。
at Renci.SshNet.Session.WaitHandle(WaitHandle waitHandle)在 Renci.SshNet.Session.WaitHandle(WaitHandle waitHandle)
at Renci.SshNet.Session.Connect()在 Renci.SshNet.Session.Connect()
at Renci.SshNet.BaseClient.Connect()在 Renci.SshNet.BaseClient.Connect()
at InvManager.Main.FTPUpload(String fileToBeUploaded, String uploadedFilename) in C:\\Data\\vb.net\\InvManager2.3\\InvManager\\Main.vb:line 562在 C:\\Data\\vb.net\\InvManager2.3\\InvManager\\Main.vb:line 562 中的 InvManager.Main.FTPUpload(String fileToBeUploaded, String UploadFilename)

Have verified that credentials are still valid on server side.已验证凭据在服务器端仍然有效。

FileZilla log showing SFTP connection from same Windows box running subject VB code to server: FileZilla 日志显示从运行主题 VB 代码的同一个 Windows 框到服务器的 SFTP 连接:

Status: Connecting to server.com...  
Response: fzSftp started, protocol_version=8  
Command: open "user@server.com" 22  
Command: Pass: ******  
Status: Connected to server.com  
Status: Retrieving directory listing...  
Command: pwd  
Response: Current directory is: "/home/server"  
Command: ls  
Status: Listing directory /home/server  
Status: Directory listing of "/home/server" successful  
Status: Disconnected from server  

Would appreciate some help in getting this working again.希望能得到一些帮助,让这个工作再次发挥作用。

Never got the original code working again... Still not sure what changed with ssh.net that broke the code.从来没有让原始代码再次工作......仍然不确定破坏代码的 ssh.net 发生了什么变化。 But per @MartinPrikryl I re-wrote the code to use the WinSCP library instead of SSH.Net and the following code has us up and working again.但是根据@MartinPrikryl,我重新编写了代码以使用 WinSCP 库而不是 SSH.Net,下面的代码让我们重新开始工作。

Protected Sub WinScpSFTPupload(ByVal fileToBeUploaded As String, uploadedFilename As String)
        Try
            Dim sessionOptions As New SessionOptions
            With sessionOptions
                .Protocol = Protocol.Sftp
                .HostName = sFtpServer
                .UserName = sFtpCreditUser
                .Password = sFtpCreditPW
                .GiveUpSecurityAndAcceptAnySshHostKey = True
            End With
            Using session As New Session
                session.Open(sessionOptions)
                session.PutFiles(fileToBeUploaded, uploadedFilename).Check()
            End Using
        Catch ex As Exception
            MsgBox(ex.Message & Chr(13) & Chr(10) & uploadedFilename)
        End Try
    End Sub ' Sub to WinSCP SFTP File

It's difficult to debug SSH.NET library as it does hardly any logging .很难调试 SSH.NET 库,因为它几乎没有任何日志记录 Try to find out, if there was any change in SSH server configuration.尝试找出 SSH 服务器配置是否有任何更改。 It's quite probable that the server changed the set set of allowed ciphers and other algos, what turned it incompatible with SSH.NET服务器很可能更改了一组允许的密码和其他算法,导致它与 SSH.NET 不兼容

You can also try to build SSH.NET out of its source code and add some logging/debug it, to see why it closes the connection.您还可以尝试从其源代码中构建 SSH.NET 并添加一些日志记录/调试,以了解它关闭连接的原因。


Other than that, you should be able to solve it by switching to WinSCP .NET assembly , as WinSCP works for you.除此之外,您应该能够通过切换到WinSCP .NET 程序集来解决它,因为 WinSCP 适合您。 If your code is as simple as in your question, it would be trivial to switch.如果您的代码像您的问题一样简单,那么切换将是微不足道的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM