.NET SSH port forwarding

Ronnie Overby picture Ronnie Overby · May 14, 2010 · Viewed 16.4k times · Source

I am trying to build in SSH port forwarding into a .NET application that I am writing.

I have tried using sharpSSH, but it requires the user to input their password every time, and I don't want that. I am going to handle storing the password.

I have downloaded Granados, but there is basically zero documentation for it. How do I accomplish port forwarding with Granados or any other free SSH library for .NET?

Answer

SRoe picture SRoe · Jun 15, 2012

The SSH.NET library is a simple way to achieve this:

using (var client = new SshClient("client.net", "user", "password"))
{
    client.Connect();

    var port = new ForwardedPortLocal("localhost", 10000, "remote.net", 80);
    client.AddForwardedPort(port);

    port.Exception += delegate(object sender, ExceptionEventArgs e)
    {
        Console.WriteLine(e.Exception.ToString());
    };
    port.Start();

    // ... hold the port open ... //

    port.Stop();
    client.Disconnect();
}