【问题标题】:Executing command using SSH.NET produces no output使用 SSH.NET 执行命令不会产生任何输出
【发布时间】:2021-11-05 01:20:45
【问题描述】:

我正在尝试制作一个连接到 SSH 服务器然后登录的程序。我已经成功创建了 tracert 或 ping 命令,但是 SSH 连接对我根本不起作用。我正在使用 SSH.NET。我的文本框没有输出。我已经听从了 StackOverflow (Execute long time command in SSH.NET and display the results continuously in TextBox) 的一些建议,但它对我不起作用。

代码如下:

private void button5_Click(object sender, EventArgs e)
{
    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;
}

【问题讨论】:

    标签: c# winforms ssh ssh.net


    【解决方案1】:

    您的命令可能有问题,因为您没有阅读错误输出,所以您不会知道。

    • 尝试读取ExtendedOutputStream 以查看该命令可能产生的任何错误输出。

    • 为了简化调试,您可以(暂时)使用2>&1 shell 语法将两个流合并为一个。


    尽管在您的代码中,您在命令名称及其参数之间遗漏了一个空格。应该是

    "check-host " + ipaddress
    

    【讨论】:

      猜你喜欢
      • 2022-01-10
      • 2022-10-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多