Vb.net &引用;值不能为null,参数名称数据;Renci.SshNet.sshclient.connect()出错

Vb.net &引用;值不能为null,参数名称数据;Renci.SshNet.sshclient.connect()出错,vb.net,ssh,c#-to-vb.net,Vb.net,Ssh,C# To Vb.net,我第一次使用库Renci.SshNet.dll。我下载了2014.4.6-beta2版本 我想用ssh连接到服务器。当然,使用我的登录名和密码与Putty进行连接 这是我的密码: Dim PWAuthMeth = New PasswordAuthenticationMethod(Login, Password) Dim KIAuthMeth = New KeyboardInteractiveAuthenticationMethod(Login) Dim ConnectionInfo As New

我第一次使用库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消息(法语):

“没有数据,参数名称:数据”

堆栈跟踪:

à 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的其他版本,但错误是相同的

我找到了这个主题:

问题似乎很相似,因此我尝试将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
不被承认。我卡住了


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

在使用任何连接之前,请使用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())
}
如果您使用某些操作,如读卡器,请同时检查返回值是否为空,如:

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,则不能将其用于任何计数。 请先检查空值,空值与零不同。

正确的代码是(翻译自):

Private Sub-LancerUpload()
Dim PWAuthMeth=新密码身份验证方法(登录,密码)
Dim KIAuthMeth=新键盘交互身份验证方法(登录)
AddHandler KIAuthMeth.AuthenticationPrompt,HandleKeyEvent的地址
Dim ConnectionInfo作为新的ConnectionInfo(服务器名,22,登录名,pAuthMeth,KIAuthMeth)
将SshClient1设置为新的SshClient(ConnectionInfo)
尝试
SshClient1.Connect()
特例
MsgBox(例如ToString())
结束尝试
MsgBox(SshClient1.未连接)
端接头
Private Sub-HandleKeyEvent(发送方作为对象,e作为Renci.SshNet.Common.AuthenticationPrompteEventArgs)
对于每个提示,如e.提示中的Renci.SshNet.Common.AuthenticationPrompt
如果prompt.Request.IndexOf(“密码:”,StringComparison.InvariantCultureIgnoreCase)-1,则
prompt.Response=密码
如果结束
下一个
端接头

什么是“未被识别”?SSH.NET 2014.4.6-beta2中有
KeyboardInteractiveAuthenticationMethod.AuthenticationPrompt
。好的,您不知道如何在VB.NET中分配事件处理程序,对吗?我不知道如何分配事件,但现在我知道;)感谢您,实际上,我无法选择AuthenticationPrompt事件,因为我是以“KIAuthMeth”开头的。因此,我不知道如何分配事件,但现在我知道了;)谢谢我认为这篇文章不是重复的,因为最初的问题涉及键盘交互身份验证方法。但是,这可能是我引用的帖子的副本,只是语言不同。我相信你的实际问题在中得到了解决,但你缺少的是如何将其转换为VB.NET。这就是你应该发布的问题,我已经将其标记为重复。是的,我将添加try块。
' vbNull can check the null value in vb
vbNull
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