c# - 无法使用 SSH.NET 库连接到 AIX(Unix) 框 - 错误 : Value cannot be null

标签 c# .net ssh putty aix

我正在尝试连接到 AIX 机器并使用 SSH.NET 库执行一些命令。 以下是代码片段

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

行连接时,我收到以下异常消息

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

我确信我传递的凭据是有效的,因为我可以使用具有相同凭据的 PuTTY 客户端登录 .有什么想法吗?

最佳答案

经过一番研究,我找到了解决方案。希望对其他人有帮助。

应该使用keyboard interactive authentication并且AuthenticationPrompt事件应该被下面的函数覆盖

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

函数调用代码:

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

关于c# - 无法使用 SSH.NET 库连接到 AIX(Unix) 框 - 错误 : Value cannot be null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15686276/

相关文章:

.net - 关联引用未映射的类异常

hadoop - 使用安全策略在 RHEL 机器上设置 Hadoop

node.js - npm 本地依赖太多?

c# - EAZfuscator 无法自动工作

c# - 扩展方法不适用于 int?

c# - 如何在用户未注销的情况下关闭浏览器时终止 session

linux - 使用双 ssh 从服务器下载文件

c# - 轻松地将 XML 节点映射到对象的属性

c# - 调用 dll 时从未命中断点

c# - 我如何在 C# 中通过 newton 解析非常嵌套的 json?