2016-06-30 39 views
8

我使用SSH.NET連接到Linux服務器從我的.NET應用程序。我執行的每個命令都按預期完成,但pbrun su - myaccount除外。.NET SSH pbrun蘇

儘管調試,當pbrun蘇 - 執行我的帳戶命令,在調試器中箭頭消失,控制永遠不會返回到Visual Studio,因此要求我手動停止調試應用程序。

+0

的'su'命令可能是等待一個密碼。如果你正在查看標準輸出你不會看到這個,因爲提示符'Password:'被髮送到標準錯誤。在[這個](http://stackoverflow.com/questions/30079526/how-to-write-to-stdin-read-from-stdout-of-an-ssh-connected-remote-process-renc#36360010)交你可以直接寫shell到標準輸入。 – AxelWass

+0

@AxelWass爲什麼會是期待一個密碼,如果運行在終端同pbrun su命令時,一個沒有提示或要求? – HendPro12

+0

我在這裏猜測,但也許ssh用戶和本地用戶不一樣?也許使用'須藤visudo'有助於爲[這裏](http://askubuntu.com/questions/470383/how-to-avoid-prompt-password-for-sudo#470466)。 – AxelWass

回答

1

如果問題是pbrun是要求在標準輸入的東西(密碼,原因等),你可以創建你所controled流的外殼,並在其中寫。

簡單的例子:

using System; 
using System.Text; 

namespace ConsoleApplication1 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      var connectionInfo = new Renci.SshNet.PasswordConnectionInfo("ancardia.us.to", "adom", "adom"); 
      var ssh = new Renci.SshNet.SshClient(connectionInfo); 

      ssh.Connect(); 

      var shell = ssh.CreateShell(Console.OpenStandardInput(), Console.OpenStandardOutput(), Console.OpenStandardOutput()); 
      shell.Start(); 

     while (true) { 
      Console.Read(); 
      } 

     } 


    } 
} 

enter image description here

+0

我用我的服務器詳細信息示例,它顯示控制檯中的遠程終端。但是,如前所述,當我執行pbrun su - myaccount時,它會成功更改用戶,並且不需要密碼, – HendPro12