"value cannot be null, parameter name data" Renci.SshNet.sshclient.connect() 错误

"value cannot be null, parameter name data" error with Renci.SshNet.sshclient.connect()

我是第一次使用库 Renci.SshNet.dll。我下载的是2014.4.6-beta2版本。

我想通过 ssh 连接到服务器。当然,连接可以使用 Putty 使用我的登录名和密码。

这是我的代码:

Dim PWAuthMeth = New PasswordAuthenticationMethod(Login, Password)
Dim KIAuthMeth = New KeyboardInteractiveAuthenticationMethod(Login)
Dim ConnectionInfo As New ConnectionInfo(ServerName, 22, Login, PWAuthMeth, KIAuthMeth)
Dim SshClient1 As New SshClient(ConnectionInfo)
SshClient1.Connect()

connect() 方法给我一个 ArgumentNullException,消息(法语):

"La valeur ne peut pas être null. Nom du paramètre : data"

StackTrace:

à Renci.SshNet.KeyboardInteractiveAuthenticationMethod.Authenticate(Session session)
à Renci.SshNet.AuthenticationMethod.Renci.SshNet.IAuthenticationMethod.Authenticate(ISession session)
à Renci.SshNet.ClientAuthentication.TryAuthenticate(ISession session, AuthenticationState authenticationState, ICollection`1 allowedAuthenticationMethods, SshAuthenticationException& authenticationException)
à Renci.SshNet.ClientAuthentication.Authenticate(IConnectionInfoInternal connectionInfo, ISession session)
à Renci.SshNet.ConnectionInfo.Authenticate(ISession session)
à Renci.SshNet.Session.Connect()
à Renci.SshNet.BaseClient.Connect()
à ConsoleApplication1.Upload.LancerUpload() dans D:\Travail\ENSAM\Promotion\Logiciel\PromoAM-Export\PromoAM-Export\Class_Upload.vb:ligne 19
...

试过其他版本的dll,还是一样的错误

我找到了这个主题:
Unable to connect to AIX(Unix) box with SSH.NET Library - Error : Value cannot be null
问题好像挺相似的,所以我试着把c#翻译成vb.net:

Private Sub HandleKeyEvent(sender As Object, e As Renci.SshNet.Common.AuthenticationPromptEventArgs)
    For Each prompt As Renci.SshNet.Common.AuthenticationPrompt In e.Prompts
        If prompt.Request.IndexOf("Password:", StringComparison.InvariantCultureIgnoreCase) <> -1 Then
            prompt.Response = Password
        End If
    Next
End Sub

但是

KIAuthMeth.AuthenticationPrompt += ...

无法识别。我卡住了。

有什么想法吗?我走对了吗?

请在使用任何连接之前使用 try catch

Dim PWAuthMeth = New PasswordAuthenticationMethod(Login, Password)
Dim KIAuthMeth = New KeyboardInteractiveAuthenticationMethod(Login)
Dim ConnectionInfo As New ConnectionInfo(ServerName, 22, Login, PWAuthMeth, KIAuthMeth)
Dim SshClient1 As New SshClient(ConnectionInfo)

try {
     SshClient1.Connect()
} catch (Exception e) {
     ' Check you error when Connect
     MsgBox(e.ToString())
}

如果您使用 Reader 等操作,请同时检查 return 值是否为空,例如:

If reader.IsDBNull(0) Then
     ' Null & ur action
Else
     ' Not null & ur action
End If

或者你可以尝试使用

' vbNull can check the null value in vb
vbNull

如果对象/值为空,则您不能将其用于任何计数。 请先检查空值,空值不等于零。

正确的代码是(翻译自Unable to connect to AIX(Unix) box with SSH.NET Library - Error : Value cannot be null):

Private Sub LancerUpload()

    Dim PWAuthMeth = New PasswordAuthenticationMethod(Login, Password)
    Dim KIAuthMeth = New KeyboardInteractiveAuthenticationMethod(Login)
    AddHandler KIAuthMeth.AuthenticationPrompt, AddressOf HandleKeyEvent
    Dim ConnectionInfo As New ConnectionInfo(ServerName, 22, Login, PWAuthMeth, KIAuthMeth)
    Dim SshClient1 As New SshClient(ConnectionInfo)

    Try
        SshClient1.Connect()
    Catch ex As Exception
        MsgBox(ex.ToString())
    End Try

    MsgBox(SshClient1.IsConnected)

End Sub

Private Sub HandleKeyEvent(sender As Object, e As Renci.SshNet.Common.AuthenticationPromptEventArgs)
    For Each prompt As Renci.SshNet.Common.AuthenticationPrompt In e.Prompts
        If prompt.Request.IndexOf("Password:", StringComparison.InvariantCultureIgnoreCase) <> -1 Then
            prompt.Response = Password
        End If
    Next
End Sub