简体   繁体   English

如何从C#应用程序创建SSH连接?

[英]How can I create an SSH connection from a C# application?

I am working on creating a GUI to interface with a Citrix XEN server. 我正在创建一个GUI来与Citrix XEN服务器连接。 I dont know how to execute commands from my within my application on my widows system on the XEN Server. 我不知道如何在XEN服务器上的widows系统上从我的应用程序中执行命令。 I was thinking using SSH but again I dont know how. 我在想使用SSH,但我又不知道怎么做。 Does anyone have an example of how to do this? 有没有人有如何做到这一点的例子? How to establish a SSH tunnel when the user pushes a button? 如何在用户按下按钮时建立SSH隧道? I want to be able to run the command xe vm-list and then display the output in a label. 我希望能够运行命令xe vm-list,然后在标签中显示输出。 Thats just to start my next one will be to create a VM and name is what the user wants, but for now I just need to figure out how to execute commands on the XEN Server. 这就是开始我的下一个将是创建一个VM,名称是用户想要的,但是现在我只需要弄清楚如何在XEN服务器上执行命令。

I have used SharpSSH with great success. 我使用SharpSSH取得了巨大成功。

This can be downloaded from http://www.tamirgal.com/blog/page/SharpSSH.aspx . 这可以从http://www.tamirgal.com/blog/page/SharpSSH.aspx下载。

Finding yourself an ssh component will allow you to do more meaningful things, but at the base level, you can do something like this: 找到一个ssh组件可以让你做更有意义的事情,但在基层,你可以这样做:

public void ExecuteExternalCommand(string command)
{
 try
 {
  // process start info
  System.Diagnostics.ProcessStartInfo processStartInfo = new System.Diagnostics.ProcessStartInfo("cmd", "/c " + command);
  processStartInfo.RedirectStandardOutput = true;
  processStartInfo.UseShellExecute = false;
  processStartInfo.CreateNoWindow = true; // Don't show console

  // create the process
  System.Diagnostics.Process process = new System.Diagnostics.Process();
  process.StartInfo = processStartInfo;
  process.Start();

  string output = process.StandardOutput.ReadToEnd();
  Console.WriteLine(output);
 }
 catch (Exception exception)
 {
  //TODO: something Meaninful
 }
}

Which will let you run arbitrary external executables via the cmd.exe interface and then respond to it. 这将允许您通过cmd.exe界面运行任意外部可执行文件,然后对其进行响应。

Here's some Links: 这里有一些链接:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM