.NET SSH port forwarding

If you set up an DSA key on the SSH server remotely, you could save a key for the user (do this as a one-time thing) and then save the key on the server as an authorized user.


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();
}