Operate UNIX Secure Shell From C# .NET Application

Introduction:

Generally, we are going with PUTTY to operate the UNIX secure shell. Based on the putty configuration we can give hostname, Port Number, user name, and password to connect and operate the UNIX command secure shell.

Example

config

But it is not needed always, sometimes you may give an API interface to your application to operate Unix Secure shell. In this article I will demonstrate how to connect and interact a UNIX secure shell from native C# .NET application.

Before starting you must know about secure shell and its uses. I recommended reading the following:

  • SSH is used to post file to UNIX SFTP (Secure File Transfer Protocol)
  • SSH is a channel and it acts as a client to UNIX Server.
  • SSH is a replacement thing for TELNET and unsecured Remote Shell.
  • SSH allows Session Management
  • SSH enabled Cryptography so that PUBLIC and PRIVATE key authentication supports.
  • SSH provides Client login and execute UNIX commands
  • SSH is one of the Message Passing Interface (MPI).
  • SSH is a protocol and server.
  • SSH can enable from mobile.

Conceptual View:

Conceptual View

So, you can operate UNIX Commands from your C# .NET applications with SSH Client.

How to Develop SSH Client?

The following nugget packages will help you to establish a connection between Shells to C# .NET (Windows)

PM > Install-Package SSH.Client
Or
PM> Install-Package SSH.NET -Version 2013.4.7

How to enable SSH?

IConnectionInfoInternal, IConnectionInfo used to enable SSH process. ConnectionInfo class accomplishes all connection policies. The following code helps you to get a basic connection.

  1. ConnectionInfoconnInfo = newConnectionInfo("HOSTNAME", postnumber, "username",  
  2.     newAuthenticationMethod[]  
  3.     {  
  4.         newPasswordAuthenticationMethod("username""password"),  
  5.   
  6.     });  
How to enable RSA-SSH connection

The following series of steps explains about RSA SSH Connection. I will show you the process with a GUI pattern and later you can verify it with C# coding. See the below screen to get fair idea:

dos

Wrong passphrase identified if you entered any wrong one,

output

You can find and got types of Secure shell log ins.
 
So, it won’t allow without SSH-RSA and passphrase key.

key

The following code helps you to do the above process programmatically.
  1. public ConnectionInfoCreateConnection()   
  2. {   
  3.   
  4.     const string PrivateKeyFilePath = @ "D:\SSH-RSA";  
  5.     ConnectionInfo connection;  
  6.     ConnectionInfoconnInfo = new ConnectionInfo("HostName", portNumber, "username",  
  7.         new AuthenticationMethod[] {  
  8.             new PasswordAuthenticationMethod("Username""password"),  
  9.                 new PrivateKeyAuthenticationMethod("username", newPrivateKeyFile[]   
  10.                 {  
  11.                     new PrivateKeyFile(PrivateKeyFilePath, "passphrase")  
  12.                 }),  
  13.         });  
  14.     return connInfo;  
  15. }  
Now, let’s test and check command execution,
  1. static void Main(string[] args)  
  2. {  
  3.     try  
  4.       
  5.     {  
  6.         using(varssh = newSshClient(newProgram().CreateConnection()))  
  7.         {  
  8.             ssh.Connect();  
  9.             var command = ssh.CreateCommand("uptime");  
  10.             var result = command.Execute();  
  11.             Console.WriteLine("yup ! UNIX Commands Executed from C#.net Application");  
  12.             Console.WriteLine("Response came form UNIX Shell" + result);  
  13.             ssh.Disconnect();  
  14.         }  
  15.     } catch (Exception ex)  
  16.     {  
  17.         Console.WriteLine(ex.Message.ToString());  
  18.     }  
  19.     Console.Read();  
  20.   
  21. }  
dos
 
Find the complete source code from my MSDN Samples.

References
Read more articles on C# Programming:


Similar Articles