首页 > 解决方案 > 无法使用 VB.NET 和 SSH.NET 连接到 SSH/SFTP 服务器 - 但 WinSCP 和 FileZilla 可以连接

问题描述

多年来,下面的代码一直有效。然后在 4 月 11 日,它停止了。我在 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 

抛出异常:

Renci.SshNet.dll Renci.SshNet.Common.SshConnectionException 中的
Renci.SshNet.Common.SshConnectionException:已建立的连接被主机中的软件中止。
在 Renci.SshNet.Session.WaitHandle(WaitHandle waitHandle)
在 Renci.SshNet.Session.Connect()
在 Renci.SshNet.BaseClient.Connect()
在 InvManager.Main.FTPUpload(String fileToBeUploaded, String uploadFilename) 在 C:\Data \vb.net\InvManager2.3\InvManager\Main.vb:第 562 行

已验证凭据在服务器端仍然有效。

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  

将不胜感激一些帮助,让这个工作再次。

标签: .netvb.netsshsftpssh.net

解决方案


再也没有让原始代码再次工作......仍然不确定 ssh.net 发生了什么改变破坏了代码。但是根据@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

推荐阅读