簡體   English   中英

C# ssh.net 沒有輸出到 textBox + 按鈕問題

[英]C# ssh.net no output to textBox + problem with a button

我正在嘗試制作一個連接到 ssh 服務器然后讓我登錄的程序。我已經成功創建了 tracert 或 ping 命令,但是 ssh 連接對我根本不起作用。 我正在使用 SSH.Net。 我的textBox沒有輸出,我已經遵循了 Stackoverflow 的一些建議( 在 SSH.NET 中執行長時間命令並在 TextBox 中連續顯示結果),但它對我不起作用。 此外,我正在嘗試在按鈕內制作一個按鈕。 在方法 button8_Click 中,我需要使用 button2_Click(button1_Click 將啟動函數,而 button9_Click 將發送 ^C 以結束命令)。 有可能嗎? 這是代碼:

        {
            string host = "someserverip";
            string username = textBox6.Text;
            string password = textBox8.Text;
            string ipaddress = textBox12.Text;

            using (var radius = new SshClient(host, port, username, password))
            {
                radius.Connect();
                var cmd = radius.CreateCommand("check-host" + ipaddress);
                var result = cmd.BeginExecute();

                using (var reader = new StreamReader(
                      cmd.OutputStream, Encoding.UTF8, true, 1024, true))
                {
                    while (!result.IsCompleted || !reader.EndOfStream)
                    {
                        string line = reader.ReadLine();
                        if (line != null)
                        {
                            textBox10.Invoke(
                                (Action)(() =>
                                    textBox10.AppendText(line + Environment.NewLine)));
                        }
                    }
                }
                cmd.EndExecute(result);
            }
            textBox10.ScrollBars = ScrollBars.Vertical;
        }

您的命令可能有問題,因為您沒有閱讀錯誤輸出,所以您不會知道。

  • 嘗試閱讀ExtendedOutputStream以查看該命令可能產生的任何錯誤輸出。

  • 為了簡化調試,您可以(暫時)使用2>&1 shell 語法將兩個流合並為一個。


盡管在您的代碼中,您錯過了命令名稱及其參數之間的空格。 應該是

"check-host " + ipaddress

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM