C# SSH 连接

C# SSH Connection

我正在尝试 运行 SSH 命令作为 C# 应用程序的一部分。我的代码如下:

using System;
using Renci.SshNet;

namespace SSHconsole
{
    class MainClass
    {

        public static void Main (string[] args)
        {
            //Connection information
            string user = "sshuser";
            string pass = "********";
            string host = "127.0.0.1";

            //Set up the SSH connection
            using (var client = new SshClient (host, user, pass))
            {

                //Start the connection
                client.Connect ();
                var output = client.RunCommand ("echo test");
                client.Disconnect();
                Console.WriteLine (output.ToString());
            }

         }
    }
}

根据我所读到的 SSH.NET,这应该输出我认为应该是 'test' 的命令的结果。但是,当我 运行 程序时,我得到的输出是:

Renci.SshNet.SshCommand

Press any key to continue...

我不明白为什么我会得到这个输出(不管命令如何),任何输入都将不胜感激。

谢谢,

杰克

使用 output.Result 代替 output.ToString()

using System;
using Renci.SshNet;

namespace SSHconsole
{
    class MainClass
    {
        public static void Main (string[] args)
        {
            //Connection information
            string user = "sshuser";
            string pass = "********";
            string host = "127.0.0.1";

            //Set up the SSH connection
            using (var client = new SshClient(host, user, pass))
            {
                //Start the connection
                client.Connect();
                var output = client.RunCommand("echo test");
                client.Disconnect();
                Console.WriteLine(output.Result);
            }
        }
    }
}