Unable to connect to AIX(Unix) box with SSH.NET Library - Error : Value cannot be null

BlackCursor picture BlackCursor · Mar 28, 2013 · Viewed 7.3k times · Source

I am trying to connect to an AIX box and execute some commands using SSH.NET library. The following is the code snipplet

KeyboardInteractiveAuthenticationMethod kauth = new KeyboardInteractiveAuthenticationMethod(username);
PasswordAuthenticationMethod pauth = new PasswordAuthenticationMethod(username, password);

ConnectionInfo connectionInfo = new(ConnectionInfo(servername, 22, username, pauth,kauth);
SshClient sshClient = new SshClient(connectionInfo);
sshClient.Connect();
SshCommand sshCommand = sshClient.RunCommand("mpstat");
Console.WriteLine(sshCommand.Result);
Console.ReadKey();

I get the following exception message when I try to connect at the line sshClient.Connect()

{"Value cannot be null.\r\nParameter name: data"}

The stack trace is

   at Renci.SshNet.KeyboardInteractiveAuthenticationMethod.Authenticate(Session session)
   at Renci.SshNet.ConnectionInfo.Authenticate(Session session)
   at Renci.SshNet.Session.Connect()
   at Renci.SshNet.BaseClient.Connect()

I am confident that the credentials I pass are valid since I am able to log-in using the PuTTY client with the same credentials. Any ideas?

Answer

BlackCursor picture BlackCursor · Mar 28, 2013

I found the solution after some research. Hope it helps somebody else.

The keyboard interactive authentication should be used and AuthenticationPrompt event should be overridden with the following function

void HandleKeyEvent(Object sender, AuthenticationPromptEventArgs e)
{
    foreach (AuthenticationPrompt prompt in e.Prompts)
    {
        if (prompt.Request.IndexOf("Password:", StringComparison.InvariantCultureIgnoreCase) != -1)
        {
            prompt.Response = password;
        }
    }
}

The function call code :

KeyboardInteractiveAuthenticationMethod kauth = new KeyboardInteractiveAuthenticationMethod(username);
PasswordAuthenticationMethod pauth = new PasswordAuthenticationMethod(username, password);

kauth.AuthenticationPrompt += new EventHandler<AuthenticationPromptEventArgs>(HandleKeyEvent);

ConnectionInfo connectionInfo = new ConnectionInfo(serverName, port, username, pauth, kauth);

sshClient = new SshClient(connectionInfo);
sshClient.Connect();