我第一次使用库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 peutpasêtrenull.Nomduparamè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 += ...
无法识别。我被卡住了。
任何想法?我是在正确的方式吗?
答案 0 :(得分:1)
正确的代码是(从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
答案 1 :(得分:-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,则不能将其用于任何计数。 请先检查空值,空值不等于零。