C# SSH .Net库中的Sudo命令

4
这是我能找到的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,来自我的电脑


公司的东西


SshNet.Logging Verbose: 1 : 从服务器接收消息:'ChannelDataMessage':'SSH_MSG_CHANNEL_DATA:#0'。 SshNet.Logging Verbose: 1 : 从服务器接收消息:'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 Verbose: 1 : 从服务器接收消息:'ChannelDataMessage':'SSH_MSG_CHANNEL_DATA:#0'。 SshNet.Logging Verbose: 1 : 从服务器接收消息:'ChannelDataMessage':'SSH_MSG_CHANNEL_DATA:#0'。 -bash:And,:未找到命令 [0;34m用户名@主机[0;31m[15:55:50]>[0m

1个回答

8
        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;
        }

    }

1
这篇文章是为了让其他人遇到同样问题时可以参考。这是一个非常简单的例子,展示了如何使用expect和ssh .net库。它实际上运行得非常好。然而,我还没有将AIX或Solaris支持添加到expect中。此外,一些每日服务器消息可能会破坏expect正则表达式。 - Nicholas Smith
感谢您发布一个可工作的示例。 我还没有尝试过原始代码,但是您是否弄清楚它为什么不起作用了? - redspidermkv
当执行命令完成后,你如何知道呢?比如说你执行 ls -R /,这可能需要相当长的时间,有没有一种基于此进行同步的方法? - user90843
我尝试使用上面的代码,但总是提示“密码错误”。我也尝试过使用shellStream.Write(password + "\n")或shellStream.Write(password + "\r"),但都没有成功。 - Alkampfer
我相信这是包含一些信息的答案的原始来源。 https://refactoragain.com/2016/05/27/ssh-net-echo-sudo-s/ - TimothyP
显示剩余2条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接