1

How can I properly su into root using SSH.Net? This question is similar to: this and this. I couldn't get any of the solutions to the answers to work for me.

Here is my code:

 using (var client = new SshClient("ip", "username", "password"))
            {
                client.Connect();
                if (client.IsConnected)
                {
                    var shell = client.CreateShellStream("xterm", 80, 24, 800, 600, 1024);
                    var reader = new StreamReader(shell);
                    var writer = new StreamWriter(shell);
                    writer.AutoFlush = true;
                    writer.Write("su - root" + "\n");

                    var output = reader.ReadLine();
                    Console.WriteLine(output);

                    /*while (true)
                    {
                        var output = reader.ReadLine();
                        if (output.EndsWith("Password: "))
                        {
                            Console.WriteLine("YES");
                        }
                    }*/
                }
                else
                {
                    Console.WriteLine("Connection Failed");
                }
                client.Disconnect();
            }

The output is always "Last login: ..." Is there another way to do this?

Community
  • 1
  • 1

1 Answers1

4

The best way I found was this:

sudo = "echo \"" + rootPwd + "\" | sudo -S " + command;

You can even use it with RunCommand:

ssh.RunCommand("echo \"" + rootPwd + "\" | sudo -S " + command);
Timisorean
  • 1,388
  • 7
  • 20
  • 30