Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sudo Command in the C# SSH .Net library

Tags:

c#

.net

ssh

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

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


company stuff


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

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

like image 612
Nicholas Smith Avatar asked Feb 11 '23 04:02

Nicholas Smith


1 Answers

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

    }
like image 200
Nicholas Smith Avatar answered Feb 13 '23 20:02

Nicholas Smith