我想从使用c#运行的Windows框中通过ssh运行命令

Rom*_*her 9 c# unix windows ssh

请注意,这必须在Windows框中,因为我使用c#来访问有关Windows的信息

(我需要来自windows box和linux box的信息,而且我认为制作一个没有gui运行的程序/脚本并且在没有用户干预的情况下从linux盒子访问windows会更加困难,如果不是这样,请告诉我,我很乐意在*nix上运行,只有访问windows信息在Windows上运行的部分).

有一个很好的c#api从windows获取这些信息,在*nix上它足够简单,可以运行命令并根据我的需要解析输出.

关于在c#中使用ssh似乎没有太多体面的建议,sharpSSH和Granados好像多年来都没有更新过,它们是否合适?我是否可能担心安全问题?

(我正在检索的信息不敏感但是如果ssh实现可能有未修补的安全漏洞(如果它们多年未更新)我担心有人窃取我的凭据.

还有其他像样的c#ssh库吗?如果命令输出很简单,我应该运行plink/putty(难以为plink运行windows cmd shell,并捕获输出(有没有办法在没有shell弹出的情况下执行)?

PS虽然商业图书馆看起来不错,但我更喜欢免费的东西(如果可能的话,在成本和免费资源中).

Mar*_*obr 6

示例代码

C#有几个商业SSH客户端库.以下代码显示了如何连接到*nix框,运行命令并使用我们的Rebex SSH Shell读取响应.

// create client, connect and log in  
Ssh client = new Ssh();
client.Connect(hostname);
client.Login(username, password);

// run the 'uname' command to retrieve OS info 
string systemName = client.RunCommand("uname -a");
// display the output 
Console.WriteLine("OS info: {0}", systemName);

client.Disconnect();
Run Code Online (Sandbox Code Playgroud)

对于高级方案(例如交互式命令),请参阅SSH Shell教程.

参考和稳定性

您可能已经在使用Rebex SSH核心库而不知道它.Rebex SFTP(使用此SSH lib作为传输层)被Microsoft用于包括Expression Web和Visual Studio 2010在内的多个产品中.Robex SSH Shell是"正好"的另一层(最值得注意的是添加终端)仿真器).

您可以从http://www.rebex.net/ssh-shell.net/download.aspx下载试用版.支持论坛使用与此站点非常相似的引擎,并在http://forum.rebex.net/上运行

免责声明:我参与了Rebex SSH的开发


Alb*_*nbo 3

调用 plink 非常容易,不会弹出 shell。

不显示窗口的技巧是设置ProcessStartInfo.CreateNoWindow = true.
添加一些错误处理就可以了。

--- PlinkWrapper.cs ---

using System.Collections.Generic;
using System.Diagnostics;

namespace Stackoverflow_Test
{
    public class PlinkWrapper
    {
        private string host { get; set; }
        /// <summary>
        /// Initializes the <see cref="PlinkWrapper"/>
        /// Assumes the key for the user is already loaded in PageAnt.
        /// </summary>
        /// <param name="host">The host, on format user@host</param>
        public PlinkWrapper(string host)
        {
            this.host = host;
        }

        /// <summary>
        /// Runs a command and returns the output lines in a List&lt;string&gt;.
        /// </summary>
        /// <param name="command">The command to execute</param>
        /// <returns></returns>
        public List<string> RunCommand(string command)
        {
            List<string> result = new List<string>();

            ProcessStartInfo startInfo = new ProcessStartInfo("plink.exe");
            startInfo.UseShellExecute = false;
            startInfo.RedirectStandardOutput = true;
            startInfo.CreateNoWindow = true;
            startInfo.Arguments = host + " " + command;
            using (Process p = new Process())
            {
                p.StartInfo = startInfo;
                p.Start();
                while (p.StandardOutput.Peek() >= 0)
                {
                    result.Add(p.StandardOutput.ReadLine());
                }
                p.WaitForExit();
            }

            return result;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

--- 结束 PlinkWrapper.cs ---

称呼它为

PlinkWrapper plink = new PlinkWrapper("albin@mazarin");
foreach (var str in plink.RunCommand("pwd"))
    Console.WriteLine("> " + str);
Run Code Online (Sandbox Code Playgroud)

输出会像

> /home/albin
Run Code Online (Sandbox Code Playgroud)

的好处plink是它经过充分验证并且与pageant.