首页 > 解决方案 > C# 使用 ssh.net 从 SSH 服务器检索数据时出错

问题描述

当我尝试连接到 SSH 服务器时,我不断收到以下错误(在这种情况下,SSH 服务器是戴尔服务器上的 iDRAC):

Renci.SshNet.Common.SshAuthenticationException
  HResult=0x80131500
  Message=No suitable authentication method found to complete authentication (publickey,keyboard-interactive).

我发现的所有内容都引用了 SCP,但我没有使用 SCP,因为我没有下载文件。我需要在 iDRAC 上运行命令以获取系统事件日志并将其显示在文本框中。下面的代码由按钮事件处理程序调用(进一步向下):

public string ConnectSSH(string RemoteHost, string UserName, string PassWord, string sshCommand, out string ServerResponse)
        {            
            using (var client = new SshClient(RemoteHost, 22, UserName, PassWord))
            {                
                client.Connect();
                var cmd = client.CreateCommand(sshCommand);
                var asyncResult = cmd.BeginExecute();
                var outputReader = new StreamReader(cmd.OutputStream);
                string output;
                while (!asyncResult.IsCompleted)
                {
                    output = outputReader.ReadToEnd();
                    ServerResponse = output;
                }
                output = outputReader.ReadToEnd();
                ServerResponse = output;
                client.Disconnect();
                return ServerResponse;                
            }
        }

和按钮事件处理程序代码:

private void BtnGetSel_Click(object sender, EventArgs e)
        {
            RemoteHost = TxtHostName.Text;
            UserName = TxtUserName.Text;
            PassWord = TxtPassWord.Text;
            SshCommand = "racadm getsel -E";
            ConnectSSH(RemoteHost, UserName, PassWord, SshCommand, out ServerResponse);
            LblServerResponse.Text = ServerResponse;
        }

标签: c#ssh.net

解决方案


由于 iDRAC CLI 的限制,我使用 plink.exe(PuTTY 的一部分)通过 SSH 连接到它。我认为问题在于您无法修改 iDRAC 上的 sshd_config 文件(它们将其锁定,因此出于安全原因您无法访问底层文件系统)。Plink 允许我做我想做的事,但 PuTTY 需要成为应用程序安装过程的一部分。


推荐阅读