如何使用SSH.Net进行安装? [英] How to su with SSH.Net?

查看:247
本文介绍了如何使用SSH.Net进行安装?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用SSH.Net正确地进入根目录?这个问题类似于: this .我无法获得任何答案的解决方案来为我工作.

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.

这是我的代码:

 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?

推荐答案

我发现的最好方法是:

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

您甚至可以将其与RunCommand一起使用:

You can even use it with RunCommand:

ssh.RunCommand("echo \"" + rootPwd + "\" | sudo -S " + command);

这篇关于如何使用SSH.Net进行安装?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆