C#SSH .Net库中的Sudo命令 [英] Sudo Command in the C# SSH .Net library

查看:119
本文介绍了C#SSH .Net库中的Sudo命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我可以找到的SSH .Net库的最简单的sudo实现.但是,我无法使其正常工作.

This is the simplest sudo implementation of the SSH .Net library I can find. However, I can not get it to work.

    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();
        }

我每次都会收到以下错误消息

I get the following error everytime

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

last login: Wed Jan 14 15:51:46 2015 from mycomputer

公司资料

SshNet.Logging详细信息:1:来自服务器的ReceiveMessage:"ChannelDataMessage":"SSH_MSG_CHANNEL_DATA:#0".SshNet.Logging详细信息:1:来自服务器的ReceiveMessage:"ChannelDataMessage":"SSH_MSG_CHANNEL_DATA:#0".[1; 36mThis is BASH [1; 31m4.1 [1; 36m- DISPLAY on [1; 31m:0.0 [m

SshNet.Logging Verbose: 1 : ReceiveMessage from server: 'ChannelDataMessage': 'SSH_MSG_CHANNEL_DATA : #0'. SshNet.Logging Verbose: 1 : ReceiveMessage from server: 'ChannelDataMessage': 'SSH_MSG_CHANNEL_DATA : #0'. [1;36mThis is BASH [1;31m4.1[1;36m- DISPLAY on [1;31m:0.0[m

2015年1月14日星期三15:55:50SshNet.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

Wed Jan 14 15:55:50 CST 2015 SshNet.Logging Verbose: 1 : ReceiveMessage from server: 'ChannelDataMessage': 'SSH_MSG_CHANNEL_DATA : #0'. SshNet.Logging Verbose: 1 : ReceiveMessage from server: 'ChannelDataMessage': 'SSH_MSG_CHANNEL_DATA : #0'. -bash: And,: command not found [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<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;
        }

    }

这篇关于C#SSH .Net库中的Sudo命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆