perform su command

Apr 20, 2012 at 5:01 AM

I am in need of the ability to perform a couple commands as root.  Has anyone been able to successfully execute the su command with the Renci Libs?

Apr 21, 2012 at 2:01 AM

If someone could please help me out here.  This has brought my project to a complete halt.  I have been very happy with this library until I ran into this.  I don't want to have to take the plink route again to do this.  I prefer to keep this native.

Apr 21, 2012 at 5:41 AM

Hi, One idea would be to use shellstream. You will find a lot of examples here :

Apr 23, 2012 at 2:19 AM

Thanks man, it took a little while to iron out the code to do what I needed but I managed to get this to work.  Thanks so much for your recommendation.

Apr 23, 2012 at 10:48 PM

Hey Vstdev, would you mind posting how you got it working? I tried most of those shellstream examples but couldn't get it working.


Apr 23, 2012 at 11:07 PM

Here ya go.  This was my approach.


public SSH (string address, string login, string password, string rootPassword)
                this.login = login;
                this.password = password;
                var connectInfo = new KeyboardInteractiveConnectionInfo(address, login);
                connectInfo.AuthenticationPrompt += new EventHandler<Renci.SshNet.Common.AuthenticationPromptEventArgs>(connectInfo_AuthenticationPrompt);
                sshClient = new SshClient(connectInfo);


                KeyValuePair<Renci.SshNet.Common.TerminalModes, uint> termkvp = new KeyValuePair<Renci.SshNet.Common.TerminalModes, uint>(Renci.SshNet.Common.TerminalModes.ECHO, 53);

                shellStream = sshClient.CreateShellStream("xterm", 80,24, 800, 600, 1024, termkvp);

                //Get System garb
                var rep = shellStream.Expect("$");

                rep = shellStream.Expect(":");

                rep = shellStream.Expect(new Regex(@"[$#]")); //look for either a root prompt or $ prompt
                if (rep.Contains("Sorry")) //got the sorry message error out.
                    throw new Exception("Unable to achieve root level access.");

                rep = shellStream.Expect("#"); // get the prompt.
            }//try to open connection
            catch (Exception ex)
                throw ex;


Here is the delegate if you need that as well.

void connectInfo_AuthenticationPrompt(object sender, Renci.SshNet.Common.AuthenticationPromptEventArgs e)
            foreach (var prompt in e.Prompts)
                if (prompt.Request.Contains("Password"))
                    prompt.Response = password;


Apr 23, 2012 at 11:33 PM

Thanks Vstdev! - It must have been the ExpectAction code I was using because this worked perfectly.