4

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

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

    }
Sign up to request clarification or add additional context in comments.

7 Comments

This is posted in case anyone else runs into this issue. It is a very simple example of how expect can be used with the ssh .net library. It actually works very well. However, I haven't add AIX or Solaris support into the expect. Furthermore, some daily server messages could break the expect regex.
Thanks for posting a working example. I haven't tried the original code but did you ever figure out why it wasn't working?
how do you know when the command finished executing, in case say you do ls -R /, that might take quite a bit of time, is there a way to synchronize based on that?
I've tried to use the code above, but always gives me "wrong password". I've tried shellStream.Write(password + "\n") or shellStream.Write(password + "\r") also, with no luck.
who is "this" ?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.