【问题标题】:C# SSH ConnectionC# SSH 连接
【发布时间】:2016-01-23 19:49:39
【问题描述】:

我正在尝试将 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 的内容来看,这应该输出我认为应该是“测试”的命令的结果。但是,当我运行程序时,我得到的输出是:

Renci.SshNet.SshCommand

Press any key to continue...

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

谢谢,

杰克

【问题讨论】:

    标签: c# ssh mono monodevelop ssh.net


    【解决方案1】:

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

    【讨论】:

    • 谢谢,完美运行。我曾尝试使用 .Result 但使用不正确。
    • 请标记为答案 :)
    • 刚做完...我正在等待倒计时所需的时间:)
    • 谢谢,我是新人,积极参与。我不知道倒计时。
    猜你喜欢
    • 2019-06-04
    • 1970-01-01
    • 2014-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多