C#SSH连接

时间:2016-01-23 19:49:40

标签: c# ssh mono monodevelop ssh.net

我正在尝试将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...

我不明白为什么我得到这个输出(无论命令如何),任何输入都会非常感激。

谢谢,

杰克

1 个答案:

答案 0 :(得分:2)

使用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);
            }
        }
    }
}