Csharp/C#教程:无法使用SSH.NET库连接到AIX(Unix)框 – 错误:值不能为空分享


无法使用SSH.NET库连接到AIX(Unix)框 – 错误:值不能为空

我正在尝试连接到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()行连接时收到以下exception消息

{“值不能为空。 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客户端登录 。 有任何想法吗?

经过一些研究,我找到了解决方案。 希望它能帮助别人。

应使用键盘交互式身份validation ,并使用以下函数覆盖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(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)进行测试

上述就是C#学习教程:无法使用SSH.NET库连接到AIX(Unix)框 – 错误:值不能为空分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!

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

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/1025119.html

(0)
上一篇 2022年1月8日
下一篇 2022年1月8日

精彩推荐