2013-03-28 31 views
8

我試圖連接到一個AIX盒和使用SSH.NET庫執行某些命令。 以下是代碼snipplet無法連接到與SSH.NET圖書館AIX(UNIX)箱 - 錯誤:值不能爲空

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客戶端使用相同的憑據登錄。有任何想法嗎?

回答

14

經過一番研究,我找到了解決方案。希望它能幫助別人。

鍵盤交互認證應使用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(); 
1

我封裝的全讓它更容易使用。警告:尚未實施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; 
     } 
    } 
}