我正在尝试连接到 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/