【问题标题】:"value cannot be null, parameter name data" error with Renci.SshNet.sshclient.connect() [duplicate]Renci.SshNet.sshclient.connect()出现“值不能为空,参数名称数据”错误[重复]
【发布时间】:2016-01-22 18:20:39
【问题描述】:

我第一次使用库 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"

堆栈跟踪:

à 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 += ...

无法识别。我被卡住了。

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

【问题讨论】:

  • “无法识别”是什么意思? SSH.NET 2014.4.6-beta2 中有KeyboardInteractiveAuthenticationMethod.AuthenticationPrompt
  • 好吧,你不知道如何在 VB.NET 中分配事件处理程序,对吧?
  • 我不知道如何分配活动,但现在我知道了;)谢谢
  • 实际上,我无法选择 AuthenticationPrompt 事件,因为我以“KIAuthMeth”开头。所以,我不知道如何分配事件,但现在我知道了;)谢谢。我认为这篇文章不是重复的,因为最初的问题涉及 KeyboardInteractiveAuthenticationMethod。但是,它可能与我引用的帖子重复,只是语言不同。
  • 我相信您的实际问题已在stackoverflow.com/q/15686276/850848 中解决,但您缺少的是如何将其转换为 VB.NET。这就是你应该发布的问题,我已将其标记为重复。

标签: vb.net ssh c#-to-vb.net


【解决方案1】:

请在使用任何连接之前使用 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之类的动作,还请检查返回值是否为null,例如:

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

或者你可以尝试使用

' vbNull can check the null value in vb
vbNull

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

【讨论】:

  • 是的,我将添加 try 块。
【解决方案2】:

正确的代码是(翻译自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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-04
    • 1970-01-01
    • 1970-01-01
    • 2014-02-03
    • 1970-01-01
    相关资源
    最近更新 更多