【问题标题】:Sudo Command in the C# SSH .Net libraryC# SSH .Net 库中的 Sudo 命令
【发布时间】:2017-02-20 06:52:55
【问题描述】:

这是我能找到的最简单的 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:从服务器接收消息:'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

2015 年 1 月 14 日星期三 15:55:50 CST SshNet.Logging 详细:1:来自服务器的 ReceiveMessage:'ChannelDataMessage':'SSH_MSG_CHANNEL_DATA:#0'。 SshNet.Logging 详细:1:来自服务器的 ReceiveMessage:'ChannelDataMessage':'SSH_MSG_CHANNEL_DATA:#0'。 -bash: And,: command not found [0;34musername@host [0;31m[15:55:50]>[0m

【问题讨论】:

    标签: c# .net ssh


    【解决方案1】:
            public void ExpectSSH (string address, string login, string password, string command)
        {
            try
            {
                SshClient sshClient = new SshClient(address, 22, login, password);
    
                sshClient.Connect();
                IDictionary<Renci.SshNet.Common.TerminalModes, uint> termkvp = new Dictionary<Renci.SshNet.Common.TerminalModes, uint>();
                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;
            }
    
        }
    

    【讨论】:

    • 发布此内容以防其他人遇到此问题。这是一个非常简单的示例,说明了如何将 expect 与 ssh .net 库一起使用。它实际上工作得很好。但是,我没有将 AIX 或 Solaris 支持添加到期望中。此外,一些日常服务器消息可能会破坏预期的正则表达式。
    • 感谢您发布一个工作示例。我没有尝试过原始代码,但你有没有弄清楚它为什么不起作用?
    • 你怎么知道命令什么时候执行完毕,万一说你做 ls -R /,那可能需要相当长的时间,有没有办法在此基础上进行同步?
    • 我尝试使用上面的代码,但总是给我“密码错误”。我也尝试过 shellStream.Write(password + "\n") 或 shellStream.Write(password + "\r"),但没有成功。
    • “这个”是谁?
    猜你喜欢
    • 2014-09-08
    • 2011-03-22
    • 1970-01-01
    • 2012-02-26
    • 2012-12-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-14
    • 1970-01-01
    相关资源
    最近更新 更多