|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
IntroductionRecently there was a need to connect to a SSH server from my C# code. I needed to perform a simple task: login to a remote Linux device, execute a command and read the response. I knew there were a number of free Java SSH libraries out there and I hoped to find a free .NET one that will allow me to do just that, but all I could find were commercial components. After experimenting with an open source Java SSH library called JSch I decided to try and port it to C# just for the sake of exercise. The result is the attached sharpSsh library and this article which explains how to use it. BackgroundSSH (Secure Shell) is a protocol to log into another computer over a network, to execute commands in a remote machine, and to move files from one machine to another. It provides strong authentication and secure communications over unsecured channels. The JSch library is a pure Java implementation of the SSH2 protocol suite; It contains many features such as port forwarding, X11 forwarding, secure file transfer and supports numerous cipher and MAC algorithms. JSch is licensed under BSD style license. My C# version is not a full port of JSch. I ported only the minimal required features in order to complete my simple task. The following list summarizes the supported features of the library:
Please check my homepage for the latest version and feature list of SharpSSH. Using the codeLet me begin with a small disclaimer. The code isn't fully tested, and I cannot guarantee any level of performance, security or quality. The purpose of this library and article is to educate myself (and maybe you) about the SSH protocol and the differences between C# and Java. In order to provide the simplest API for SSH communication, I created two wrapper classes under the
Reading and writing data over the SSH channelThe //Create a new SSH stream
SshStream ssh = new SshStream("remoteHost", "username", "password");
//..The SshStream has successfully established the connection.
Now, we can set some properties: //Set the end of response matcher character
ssh.Prompt = "#";
//Remove terminal emulation characters
ssh.RemoveTerminalEmulationCharacters = true;
The The response string will typically contain escape sequence characters which are terminal emulation signals that instruct the connected SSH client how to display the response. However, if we are only interested in the 'clean' response content we can omit these characters by setting the Now, reading and writing to/from the SSH stream will be done as follows: //Writing to the SSH channel
ssh.Write( command );
//Reading from the SSH channel
string response = ssh.ReadResponse();
Of course, it's still possible to use the Transferring files using SCPTransferring files to and from an SSH server is pretty straightforward with the //Create a new SCP instance
Scp scp = new Scp();
//Copy a file from local machine to remote SSH server
scp.To("C:\fileName", "remoteHost",
"/pub/fileName", "username", "password");
//Copy a file from remote SSH server to local machine
scp.From("remoteHost", "/pub/fileName",
"username", "password", "C:\fileName");
The
Running the examplesThe demo project is a simple console application demonstrating the use of Here is a screen shot of an SSH connection to a Linux shell:
And here is a file transfer from a Linux machine to my PC using SCP:
In the demo project zip file you will also find an examples directory containing some classes showing the use of the original JSch API. These examples were translated directly from the Java examples posted with the original JSch library and show the use of advanced options such as public key authentication, known hosts files, key generation, SFTP and others. References
History
|
||||||||||||||||||||||