2015-10-23 65 views
0

我第一次使用庫Renci.SshNet.dll。我下載了2014.4.6-beta2版本。「值不能爲空,參數名稱數據」與Renci.SshNet.sshclient.connect()的錯誤

我想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,消息(法語):

「香格里拉valeur東北peut考績理由空喃杜paramètre :數據」

堆棧跟蹤:

à 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

是什麼意思* 「無法識別」 *? SSH.NET 2014.4.6-beta2中有'KeyboardInteractiveAuthenticationMethod.AuthenticationPrompt'。 –

+0

好吧,你不知道如何在VB.NET中分配一個事件處理程序,對吧? –

+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

請使用嘗試捕捉使用如果烏拉圭回合使用相似的閱讀器有些行動的任何連接

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 

或U可以嘗試使用

' vbNull can check the null value in vb 
vbNull 

如果對象/值爲空,則u不能將其用於任何計數。 請首先檢查空值,空值不等於零。

+0

是的,我會添加try塊。 –

相關問題