How to su with SSH.Net?

孤者浪人 提交于 2019-12-12 04:32:46

问题


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?


回答1:


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


来源:https://stackoverflow.com/questions/40918265/how-to-su-with-ssh-net

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!