0

I'm using for the first time the library Renci.SshNet.dll. I downloaded the version 2014.4.6-beta2.

I want to ssh connect to a server. Of course, the connection works with Putty using my login and password.

Here is my code:

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()

The connect() method gives me an ArgumentNullException, message (in french):

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

The 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
...

I tried others versions of the dll, but the error is the same.

I found this topic:
Unable to connect to AIX(Unix) box with SSH.NET Library - Error : Value cannot be null
The problem seems to be quite similar, so I tried to translate c# to 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

But

KIAuthMeth.AuthenticationPrompt += ...

is not recognised. I'm stuck.

Any Idea ? Am I on the right way ?

Community
  • 1
  • 1
  • What does it mean *"is not recognised"*? There is `KeyboardInteractiveAuthenticationMethod.AuthenticationPrompt` in SSH.NET 2014.4.6-beta2. – Martin Prikryl Oct 23 '15 at 09:56
  • OK, you do not know how to assign an event handler in VB.NET, right? – Martin Prikryl Oct 23 '15 at 10:00
  • I DID not know how to assign an event, but now I Know ;) Thanks – Yoann ARNAUD Oct 23 '15 at 10:42
  • Actually, I couldn't choose the AuthenticationPrompt event because I started my line with "KIAuthMeth." So, I DID not know how to assign an event, but now I Know ;) Thanks. I think this post is not a duplicate because the original problem concerns KeyboardInteractiveAuthenticationMethod. However, it's maybe a duplicate of the post I cited, just the language is different. – Yoann ARNAUD Oct 23 '15 at 10:48
  • I believe your actual problem is solved in http://stackoverflow.com/q/15686276/850848 But what you were missing was how to convert that to VB.NET. So that was the question you should have posted, and I've marked it as duplicate as such. – Martin Prikryl Oct 23 '15 at 11:02

2 Answers2

1

The correct code is (translated from 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
Community
  • 1
  • 1
-1

Please using try catch before using any connection

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 ur using some action like Reader, please also check the return value is null or not, like:

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

Or u can try to use

' vbNull can check the null value in vb
vbNull

If the object/ value was null, u cannot use it for any count. Please check the null value first, null value not same as zero.

Koi Tsang
  • 44
  • 4