我想通过ssh使用C#运行在Windows中运行一个命令 [英] I'd like to run a command over ssh from a windows box running using c#
问题描述
请注意,这已经是在Windows中,因为我使用C#访问有关窗口的信息
Note that this has to be on a windows box as I am using c# to access information about windows
(我需要同时从窗户框和一个Linux的信息箱,再加上我觉得做节目/脚本,没有GUI运行,并从Linux中访问的窗口,无需用户干预会比较困难,如果这是不正确的,请告诉我,我会喜欢做让在* nix这个运行只有在Windows上运行的窗口访问信息)的一部分。
(I need information from both a windows box and a linux box, plus I think that making a program/script that runs without gui and accesses windows from a linux box without user intervention would be more difficult, if this is not true please tell me, I would love to do get this running on *nix with only the part that access windows info running on windows).
有一个很好的C#API从窗口信息,在* nix它足够简单,运行命令和输出解析到我的需求。
There is a nice c# api to get this information from windows, on *nix its simple enough to run a command and parse the output to my needs.
似乎没有约使用ssh从C#那里太多像样的建议,sharpSSH和格拉纳多斯似乎多年来一直没有更新,他们是体面?我应该有可能担心安全问题?
There doesn't seem to much decent advice about using ssh from c# out there, sharpSSH and Granados seem to have not been updated for years, are they decent? should I be possible worried about security issues?
(我检索信息不敏感,但如果SSH实现可能有未打补丁的安全漏洞(如果他们没有为年更新一次),我担心有人偷了我的凭据。
(the info I'm retrieving isn't sensitive but if the ssh implementation might have unpatched security flaws(if they haven't been updated for years) I'm worried about someone stealing my credentials.
还有没有其他像样的C#SSH库。如果命令的输出很简单,我应该只跑砰砰/腻子(是难以运行在Windows CMD shell中砰砰,并捕获输出(并且是有办法做到这一点没有外壳弹出)?
Are there any other decent c# ssh libraries. If the command output is simple should I just run plink/putty(is it difficult to run a windows cmd shell for plink, and capture output(and is there a way to do it without the shell popping up)?
PS而商业库看上去不错我喜欢一些免费的(如成本和免费的来源,如果可能的话)。
P.S. while the commercial library seems nice I prefer something free (as in cost and free in source if possible).
推荐答案
这是很容易调用砰砰没有外壳爆裂了。
It is quite easy to call plink without the shell popping up.
不显示一个窗口,关键是要设置的 ProcessStartInfo.CreateNoWindow = TRUE
。结果
添加一些错误处理这个就大功告成了。
The trick to not show a window is to set ProcessStartInfo.CreateNoWindow = true
.
Add some error handling to this and you're done.
--- PlinkWrapper.cs ---
--- 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<string>.
/// </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;
}
}
}
--- END PlinkWrapper。 CS ---
--- END PlinkWrapper.cs ---
这样称呼它
PlinkWrapper plink = new PlinkWrapper("albin@mazarin");
foreach (var str in plink.RunCommand("pwd"))
Console.WriteLine("> " + str);
和输出会像
> /home/albin
与砰砰
是,它是很好的证明,并与选美
。
The nice thing with plink
is that it is well proven and integrates well with pageant
.
这篇关于我想通过ssh使用C#运行在Windows中运行一个命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!