无法连接到AIX(UNIX)与SSH.NET库中 - 错误:值不能为空连接到、为空、库中、错误

2023-09-03 07:55:10 作者:じ☆為い愛沉淪

我试图连接到一个 AIX 框和使用的 SSH.NET 库执行某些命令。 以下是code snipplet

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

我碰到下面的异常消息:当我试图连接在该行 sshClient.Connect()

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

{值不能为空\ r \ n参数名:数据}

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

该堆栈跟踪

   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?

推荐答案

我发现了一些研究后的溶液。希望它可以帮助别人。

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

在键盘交互认证应使用和 AuthenticationPrompt 事件应该用下面的函数来覆盖

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;
        }
    }
}

函数调用code:

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();
 
精彩推荐