2013-03-28 18 views
8

Próbuję połączyć się z AIX polu i wykonać kilka poleceń za pomocą SSH.NET bibliotekę. Poniżej znajduje się kod snippletNie można połączyć AIX (UNIX) pudełka z SSH.NET do Biblioteki - Błąd: Wartość nie może być null

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

uzyskać następujące wyjątek wiadomość gdy próbuję łączyć w linii sshClient.Connect()

{ "Wartość nie może być null \ r \ nParameter nazwy.: dane "}

ślad stos jest

at Renci.SshNet.KeyboardInteractiveAuthenticationMethod.Authenticate(Session session) 
    at Renci.SshNet.ConnectionInfo.Authenticate(Session session) 
    at Renci.SshNet.Session.Connect() 
    at Renci.SshNet.BaseClient.Connect() 

Jestem przekonany, że poświadczenia mijam są ważne, ponieważ jestem stanie zalogować się użyciu klienta PuTTY z tych samych poświadczeń. Jakieś pomysły?

Odpowiedz

14

Po znalezieniu rozwiązania znalazłem rozwiązanie. Mam nadzieję, że pomaga komuś innemu.

klawiatura interaktywne uwierzytelniania powinny być wykorzystywane i AuthenticationPrompt zdarzenie powinno zostać zastąpione z następujących funkcji

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

Kod wywołanie funkcji:

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

mam obudowane całość coś, dzięki czemu jest łatwiejsze w użyciu. Ostrzeżenie: jeszcze nie wdrożono try/catch! DLL jest dostępny tutaj: https://sshnet.codeplex.com/releases/view/120504 Testowany z 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; 
     } 
    } 
} 
Powiązane problemy