2016-01-23 113 views
2

我試圖運行SSH命令作爲C#應用程序的一部分。我的代碼如下:C#SSH連接

using System; 
using Renci.SshNet; 

namespace SSHconsole 
{ 
    class MainClass 
    { 

     public static void Main (string[] args) 
     { 
      //Connection information 
      string user = "sshuser"; 
      string pass = "********"; 
      string host = "127.0.0.1"; 

      //Set up the SSH connection 
      using (var client = new SshClient (host, user, pass)) 
      { 

       //Start the connection 
       client.Connect(); 
       var output = client.RunCommand ("echo test"); 
       client.Disconnect(); 
       Console.WriteLine (output.ToString()); 
      } 

     } 
    } 
} 

從我讀到的有關SSH.NET,這應該輸出命令的結果,我相信應該'測試'。然而,當我運行程序,輸出我得到的是:

Renci.SshNet.SshCommand 

Press any key to continue... 

我不明白爲什麼我得到這個輸出(與命令)和任何輸入將不勝感激。

感謝,

傑克

回答

2

使用output.Result而不是output.ToString()

using System; 
using Renci.SshNet; 

namespace SSHconsole 
{ 
    class MainClass 
    { 
     public static void Main (string[] args) 
     { 
      //Connection information 
      string user = "sshuser"; 
      string pass = "********"; 
      string host = "127.0.0.1"; 

      //Set up the SSH connection 
      using (var client = new SshClient(host, user, pass)) 
      { 
       //Start the connection 
       client.Connect(); 
       var output = client.RunCommand("echo test"); 
       client.Disconnect(); 
       Console.WriteLine(output.Result); 
      } 
     } 
    } 
} 
+0

謝謝,工作完美。我曾嘗試使用.Result,但它使用不正確。 –

+0

標記爲答案:) –

+0

剛剛完成...我正在等待所需的時間來倒計時:) –