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

I am working on a graphical interface for interacting with the Citrix XEN server. I do not know how to execute commands from my application on my widows system on the XEN server. I thought using SSH, but again I do not know how to do this. Does anyone have an example of how to do this? How to set up SSH tunnel when user clicks button? I want to be able to run the xe vm-list command and then display the output in a label. Just starting my next one will be creating a virtual machine, and the name is what the user wants, but for now I just need to figure out how to execute the commands on the XEN server.

+3
source share
2 answers

Finding yourself in the ssh component will allow you to do more meaningful things, but at a basic level, you can do something like this:

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
 }
}

Allows you to run arbitrary external executable files through the cmd.exe interface, and then respond to it.

Here are some links:

+1
source

I have used SharpSSH with great success.

This can be downloaded from http://www.tamirgal.com/blog/page/SharpSSH.aspx .

+2
source

Source: https://habr.com/ru/post/1772504/


All Articles