6

I came across this article when trying to put together a reverse SSH solution in visual studio C#:

.NET SSH Port Forwarding

Using his code works up until the point where I actually start the port (port.Start()), then it throws an exception. Doing a reverse tunnel with PuTTY works just fine, so I believe the server is set up correctly. Am I doing something wrong in regards to implementing this SSH.NET library?

using Renci.SshNet;
using Renci.SshNet.Messages;
using Renci.SshNet.Common;
using System;
using System.Net;
using System.Threading;

namespace rSSHTunnelerSSHNET
{

    public class Program
    {

        public static void Main(String[] arg)
        {
            String Host = "testserver.remote";
            String Username = "testuser";
            String Password = "password";

            using (var client = new SshClient(Host, Username, Password))
            {
                client.Connect();

                if (client.IsConnected)
                {
                    Console.WriteLine("connected");

                    var port = new ForwardedPortRemote("targetserver.local", 80, "testserver.remote", 8080);
                    client.AddForwardedPort(port);

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

                    port.Start();
                }
                else
                {
                    Console.WriteLine("failed to connect.");
                    Console.ReadLine();
                }
            }
        }

    }
}
Community
  • 1
  • 1
Sean Perryman
  • 251
  • 3
  • 15
  • Just a quick update; I took this piece of code to another environment completely and am getting similar errors. Tried a fresh install of CentOS on a remote machine, and am in a home environment without the benefit of an enterprise class firewall. Still able to properly establish the connection through PuTTY, but not through the program. – Sean Perryman Aug 27 '13 at 00:15
  • Currently, when the code gets to the point of port.Start(), I get a "No such host is known" unhandled socket exception. Found another site with similar examples here http://sshnet.codeplex.com/discussions/348423, but when I use the code they provide it doesn't work. I am entertaining the idea that I am missing a using statement, but I am not quite sure what it would be. – Sean Perryman Aug 27 '13 at 00:19
  • Have you tried the ForwardedPortRemote constructor overload that accepts IP addresses? I've only been able to remote forward ports using this library with the following: var port = new ForwardedPortRemote(IPAddress.Loopback, boundPort, IPAddress.Loopback, localPort); – MrShoes Aug 27 '13 at 08:24
  • I have not tried that, how do I plug in an external address in that space? I apologize for my ignorance, still learning C#. Was starting to think that it might be a problem with a self-signed cert on the remote server, and the app not having a way to allow my to accept the cert anyway. Wondering if possibly it might just accept it by default, but I suppose it could be dropping me because of that. – Sean Perryman Aug 27 '13 at 17:17
  • Hmm. My use of the library has been to allow connections through an SSH Tunnel to hardware through RDP, SQL Server, HTTP etc. I think that if you're trying to connect to a web resource you might want to use the 3-parameter overload of the constructor, eg. var port = new ForwardedPortRemote(boundPort, host, port); – MrShoes Aug 28 '13 at 07:37
  • That's actually what I plan on doing; tunneling VNC and/or RDP between two network independent locations. I keep getting the port cannot start error when I call port.Start, is there any special setup I should be implementing on a standard Ubuntu linux install using the root account to authenticate? I must emphasize, this is just for testing; once production ready the server will be properly secured and the root account will be disallowed ssh access. – Sean Perryman Aug 30 '13 at 15:56

0 Answers0