S#SSH .Net库中的Sudo命令

这是我能找到的SSH .Net库最简单的sudo实现。 但是,我无法让它发挥作用。

using (var ssh = new SshClient("hostname", "username", "password")) { ssh.Connect(); var input = new MemoryStream(); var sr = new StreamWriter(input); var output = Console.OpenStandardOutput(); var shell = ssh.CreateShell(input, output, output); shell.Stopped += delegate { Console.WriteLine("\nDisconnected..."); }; shell.Start(); sr.WriteLine("sudo ls"); sr.Flush(); Thread.Sleep(1000 * 1); sr.WriteLine("password"); sr.Flush(); Thread.Sleep(1000 * 100); shell.Stop(); } 

我每次都会收到以下错误

上次登录时间:2015年1月14日星期三15:51:46来自mycomputer


公司的东西


SshNet.Logging详细:1:来自服务器的ReceiveMessage:’ChannelDataMessage’:’SSH_MSG_CHANNEL_DATA:#0’。 SshNet.Logging详细:1:来自服务器的ReceiveMessage:’ChannelDataMessage’:’SSH_MSG_CHANNEL_DATA:#0’。 [1; 36m这是BASH [1; 31m4.1 [1; 36m-显示在[1; 31m:0.0 [m]

1月14日星期三15:55:50 CST 2015 SshNet.Logging详细:1:来自服务器的ReceiveMessage:’ChannelDataMessage’:’SSH_MSG_CHANNEL_DATA:#0’。 SshNet.Logging详细:1:来自服务器的ReceiveMessage:’ChannelDataMessage’:’SSH_MSG_CHANNEL_DATA:#0’。 -bash:而且,:找不到命令 [0; 34musername @ host [0; 31m [15:55:50]> [0m

  public void ExpectSSH (string address, string login, string password, string command) { try { SshClient sshClient = new SshClient(address, 22, login, password); sshClient.Connect(); IDictionary termkvp = new Dictionary(); termkvp.Add(Renci.SshNet.Common.TerminalModes.ECHO, 53); ShellStream shellStream = sshClient.CreateShellStream("xterm", 80,24, 800, 600, 1024, termkvp); //Get logged in string rep = shellStream.Expect(new Regex(@"[$>]")); //expect user prompt this.writeOutput(results, rep); //send command shellStream.WriteLine(commandTxt.Text); rep = shellStream.Expect(new Regex(@"([$#>:])")); //expect password or user prompt this.writeOutput(results, rep); //check to send password if (rep.Contains(":")) { //send password shellStream.WriteLine(password); rep = shellStream.Expect(new Regex(@"[$#>]")); //expect user or root prompt this.writeOutput(results, rep); } sshClient.Disconnect(); }//try to open connection catch (Exception ex) { System.Console.WriteLine(ex.ToString()); throw ex; } }