C# 无法使用SSH.NET库连接到AIX(Unix)框-错误:值不能为null

C# 无法使用SSH.NET库连接到AIX(Unix)框-错误:值不能为null,c#,.net,ssh,putty,aix,C#,.net,Ssh,Putty,Aix,我正在尝试连接到一个AIX框,并使用SSH.NET库执行一些命令。 下面是代码snipplet KeyboardInteractiveAuthenticationMethod kauth = new KeyboardInteractiveAuthenticationMethod(username); PasswordAuthenticationMethod pauth = new PasswordAuthenticationMethod(username, password); Connect

我正在尝试连接到一个AIX框,并使用
SSH.NET
库执行一些命令。 下面是代码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()行进行连接时,会收到以下异常消息

{“值不能为空。\r\n参数名称:数据”}

堆栈跟踪为

   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
客户端登录。有什么想法吗?

经过研究,我找到了解决办法。希望它能帮助别人

应使用键盘交互式身份验证,并且应使用以下功能覆盖
身份验证提示
事件

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();
KeyboardInteractiveAuthenticationMethod kauth=新的KeyboardInteractiveAuthenticationMethod(用户名);
PasswordAuthenticationMethod pauth=新的PasswordAuthenticationMethod(用户名、密码);
kauth.AuthenticationPrompt+=新事件处理程序(HandleKeyEvent);
ConnectionInfo ConnectionInfo=新的ConnectionInfo(服务器名、端口、用户名、pauth、kauth);
sshClient=新的sshClient(connectionInfo);
sshClient.Connect();

我已经将整个东西封装起来,以便更易于使用。警告:尚未实施try/catch!DLL可在以下位置获得: 使用SLES(11.164)、Debian(6)、AIX(5.3、6.1、7)进行测试

使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.Windows.Forms;
使用Renci.SshNet;
使用Renci.SshNet.Common;
名称空间SSH2
{
公共部分类Form1:Form
{
公共表格1()
{
初始化组件();
SSH client=newssh(“服务器名”、“用户名”、“密码”);
Show(client.command(“ls-la/”));
}
}
公共类SSH
{
字符串servername;
国际港口;
字符串用户名;
字符串密码;
SshClient服务器=null;
公共SSH(字符串服务器名、int端口、字符串用户名、字符串密码)
{
this.servername=servername;
this.port=端口;
this.username=用户名;
this.password=密码;
this.init();
}
公共SSH(字符串服务器名、字符串用户名、字符串密码)
{
this.servername=servername;
这个端口=22;
this.username=用户名;
this.password=密码;
this.init();
}
私有void init()
{
KeyboardInteractiveAuthenticationMethod kauth=新的KeyboardInteractiveAuthenticationMethod(this.username);
PasswordAuthenticationMethod pauth=新的PasswordAuthenticationMethod(this.username,this.password);
kauth.AuthenticationPrompt+=新事件处理程序(HandleKeyEvent);
this.Server=newsshclient(newconnectioninfo(this.servername,this.port,this.username,pauth,kauth));
}
无效HandleKeyEvent(对象发送方,AuthenticationPromptEventArgs e)
{
foreach(e.Prompts中的AuthenticationPrompt提示符)
{
if(prompt.Request.IndexOf(“密码:”,StringComparison.InvariantCultureIgnoreCase)!=-1)
{
prompt.Response=this.password;
}
}
}
公共字符串命令(字符串cmd)
{
this.Server.Connect();
var output=this.Server.RunCommand(cmd);
this.Server.Disconnect();
返回输出结果;
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using Renci.SshNet;
using Renci.SshNet.Common;

namespace SSH2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            SSH client = new SSH("servername", "username", "password");

            MessageBox.Show(client.command("ls -la /"));
        }

    }

    public class SSH
    {
        string servername;
        int port;
        string username;
        string password;

        SshClient Server = null;


        public SSH(string servername, int port, string username, string password)
        {
            this.servername = servername;
            this.port = port;
            this.username = username;
            this.password = password;

            this.init();
        }

        public SSH(string servername, string username, string password)
        {
            this.servername = servername;
            this.port = 22;
            this.username = username;
            this.password = password;

            this.init();
        }


        private void init()
        {
            KeyboardInteractiveAuthenticationMethod kauth = new KeyboardInteractiveAuthenticationMethod(this.username);
            PasswordAuthenticationMethod pauth = new PasswordAuthenticationMethod(this.username, this.password);

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

            this.Server = new SshClient(new ConnectionInfo(this.servername, this.port, this.username, pauth, kauth));
        }


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

        public string command(string cmd)
        {
            this.Server.Connect();

            var output = this.Server.RunCommand(cmd);

            this.Server.Disconnect();

            return output.Result;
        }
    }
}