SSH port forwarding in .Net, C# example

By | March 25, 2019

The example is based on Renci.sshNet Library. Actually it is a genuinely undocumented library, well the site has some documentation yet it’s not too supportive for this kind of task, so I spent several hours to develop this projects from scratch. When the project is created you need to install Renci.SshNet library using Visual Studio NuGet Package Manager console from Visual Studio Tool menu. After opening Package Manager Console execute the following command “Install-Package SSH.net” and Rensi.SshNet will be added to the project references. The presented example is console .Net application, the full code may be downloaded from here.
The main part tunnelText.exe application code is:
Renci.SshNet port forwarder

The application connects to the ssh server and begins listening on the bound port for ssh client connection. The total connection diagram looks like:
port forwarding

The tunnelTest application may be starte4d from command prompt and uses the following arguments (the order is important): bound end point, remote ssh server end point, ssh user name and ssh password. For example:
tunnel testing result

The network connection status and associated processes for such port forwarding looks like presented below:
tunnel connection
where 19136 is tunnelTest PID, 6132 is putty PID, 10.199.30.14:22 is ssh server IP and port, 127.0.0.1:5022 is listening port opened by tunnelTest application, 49218 is ephemeral port assigned to putty.

Leave a Reply

Your email address will not be published. Required fields are marked *