Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I'd like to run a command over ssh from a windows box running using c#

Tags:

c#

unix

windows

ssh

Note that this has to be on a windows box as I am using c# to access information about 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).

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.

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?

(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.

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)?

P.S. while the commercial library seems nice I prefer something free (as in cost and free in source if possible).

like image 951
Roman A. Taycher Avatar asked Sep 04 '10 12:09

Roman A. Taycher


People also ask

Can you SSH to a Windows machine?

Windows has a built-in SSH client that you can use in Windows Terminal.

Can you SSH from Windows to Linux?

Use the open source tool, PuTTY to establish an SSH connection from a Windows machine to a Linux system. The secure shell protocol (SSH) is the most common method for controlling remote machines over the command line in the Linux world.


1 Answers

Sample Code

There are several commercial SSH client libraries for C#. Following code shows how to connect to a *nix box, run a command and read the response using our 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();

For advanced scenarios (such as interactive commands) see SSH Shell Tutorial.

References & Stability

You might be already using Rebex SSH core library without knowing about it. The Rebex SFTP (which uses this SSH lib as a transport layer) is used by Microsoft in several products including Expression Web and Visual Studio 2010. The Rebex SSH Shell is 'just' another layer on top of it (most notable addition is a terminal emulator).

You can download a trial from http://www.rebex.net/ssh-shell.net/download.aspx. Support forum uses engine very similar to this site and runs on http://forum.rebex.net/

Disclaimer: I am involved in development of Rebex SSH

like image 174
Martin Vobr Avatar answered Oct 05 '22 22:10

Martin Vobr