【问题标题】:renci.shhNet issue with executing commandsrenci.shhNet 执行命令的问题
【发布时间】:2020-06-21 14:22:22
【问题描述】:

我正在尝试与我们的 WLC 控制器建立 ssh 连接。手动使用腻子时,我可以完美连接,因此凭据很好。为此,我尝试使用 renci.ShhNet。

我可以创建 shhclient 并使用连接功能。但是当我尝试使用 client.runco​​mmand 或 client.createcommand 然后执行此命令时,应用程序只是“挂起”。

我的第一次尝试是这样的:

PasswordConnectionInfo conn = new PasswordConnectionInfo("10.49.144.3", WLCUser, WLCPassword);

            using (var client = new SshClient(conn))
            {
                client.Connect();
                var result = client.RunCommand($"config wlan disable {wlanId}");
                Thread.Sleep(5000);
                result = client.RunCommand($"config wlan security wpa akm psk set-key ascii {newPassword} {wlanId}");
                Thread.Sleep(5000);
                result = client.RunCommand($"config wlan enable {wlanId}");

但这会挂在第一个 RunCommand 上,我不太明白为什么会挂起。

作为第二种方法,我尝试使用 CreateShellStream 并以这种方式发送命令。在调查我从这个流中得到的响应时,我可以看到我们的 WLC 再次询问用户名和密码。而且我似乎正在将我的命令发送到用户名和密码提示符。我尝试再次将用户名作为密码作为第一个命令发送,但这也不起作用。

我的第二次尝试:

void mainFunction()
{
PasswordConnectionInfo conn = new PasswordConnectionInfo("10.49.144.3", WLCUser, WLCPassword);

            using (var client = new SshClient(conn))
            {
                client.Connect();
                StringBuilder output = new StringBuilder();
                ShellStream stream = client.CreateShellStream("Customcommand", 80,24,800,600,1024);
                output.Append(sendCommand(WLCUser, stream));
                output.Append(sendCommand(WLCPassword, stream));
                output.Append(sendCommand("blablabla", stream));
                Console.WriteLine(output.ToString());
                client.Disconnect();
            }
}

public StringBuilder sendCommand(string customCMD, ShellStream stream)
        {
            StringBuilder answer;

            var reader = new StreamReader(stream);
            var writer = new StreamWriter(stream);
            writer.AutoFlush = true;
            WriteStream(customCMD, writer, stream);
            answer = ReadStream(reader);
            return answer;
        }

        private void WriteStream(string cmd, StreamWriter writer, ShellStream stream)
        {
            writer.WriteLine(cmd);
            while (stream.Length == 0)
            {
                Thread.Sleep(500);
            }
        }

        private StringBuilder ReadStream(StreamReader reader)
        {
            StringBuilder result = new StringBuilder();

            string line;
            while ((line = reader.ReadLine()) != null)
            {
                result.AppendLine(line);
            }
            return result;
        }

这会为我产生以下输出:


---------------------------------------------------------------------

|                        Connected to TIG                           |

|                                                                   |

|                            WARNING !!!                            |

|                                                                   |

|       YOU HAVE CONNECTED TO A PRIVATE COMPUTING FACILITY          |

|   IF YOU ARE NOT AN AUTHORISED USER, DISCONNECT IMMEDIATELY       |

---------------------------------------------------------------------
(Cisco Controller) 
User: UserSecret
Password:
User:
PasswordSecret
Password:
User:
blablabla
Password:
User:

如您所见,我想他又在期待用户名和密码,但我似乎无法发送?

关于出了什么问题有什么想法吗?

【问题讨论】:

  • 你需要在每个sendCommand的末尾添加一个回车。
  • 您还可以通过 ShellStream 来利用 Ssh.Net 的预期功能。本质上,您是在告诉它期望某个输出以及读取该输出时要输入的内容。用法示例见this答案。
  • 不,最后发送回车会更奇怪

标签: c# ssh


【解决方案1】:

好的,我还没弄清楚为什么,但显然我借用的那段代码(sendcommand 位)不能很好地与 shellstream 配合使用。当我直接写入 shell 流而不是流写入器时,它似乎可以工作。

我现在有了这个,这似乎有效:

using (var client = new SshClient(conn))
            {
                client.Connect();
                if (client.IsConnected)
                {
                    StringBuilder output = new StringBuilder();
                    ShellStream stream = client.CreateShellStream("Customcommand", 0, 0, 0, 0, 1024);
                    var reader = new StreamReader(stream);
                    stream.WriteLine(WLCUser);
                    stream.Flush();
                    stream.WriteLine(WLCPassword);
                    stream.Flush();
                    stream.WriteLine($"config wlan disable {wlanId}");
                    stream.Flush();
                    Thread.Sleep(5000);
                    stream.WriteLine($"config wlan security wpa akm psk set-key ascii {newPassword} {wlanId}");
                    stream.Flush();
                    Thread.Sleep(5000);
                    stream.WriteLine($"config wlan enable {wlanId}");
                    stream.Flush();
                    Thread.Sleep(1000);

                    Console.WriteLine(reader.ReadToEnd());
                    client.Disconnect();
                }
            }

【讨论】:

    猜你喜欢
    • 2015-01-10
    • 2020-12-23
    • 1970-01-01
    • 1970-01-01
    • 2016-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多