简体   繁体   中英

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. 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.

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.Common.SshConnectionException: An established connection was aborted by the software in your host machine.
at Renci.SshNet.Session.WaitHandle(WaitHandle waitHandle)
at Renci.SshNet.Session.Connect()
at Renci.SshNet.BaseClient.Connect()
at InvManager.Main.FTPUpload(String fileToBeUploaded, String uploadedFilename) in C:\\Data\\vb.net\\InvManager2.3\\InvManager\\Main.vb:line 562

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:

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. 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.

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 . Try to find out, if there was any change in SSH server configuration. It's quite probable that the server changed the set set of allowed ciphers and other algos, what turned it incompatible with 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.


Other than that, you should be able to solve it by switching to WinSCP .NET assembly , as WinSCP works for you. If your code is as simple as in your question, it would be trivial to switch.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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