我试图连接到一个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
客户端使用相同的凭据。 有任何想法吗?
我发现了一些研究后的溶液。 希望它可以帮助别人。
键盘交互认证应使用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();
我已经封装了整个事情,所以它更容易使用。 警告:没有的try / catch一直没有实现! 该DLL可以在这里: https://sshnet.codeplex.com/releases/view/120504用SLES(11.1 64),Debian的(6),AIX测试(5.3,6.1,7)
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;
}
}
}
文章来源: Unable to connect to AIX(Unix) box with SSH.NET Library - Error : Value cannot be null