This project is read-only.

PortForwarding in new version not Working...

Mar 13, 2012 at 4:02 PM

Hi all,

 

I already have a project with the previous library and because it works to set up port forwarding as described in the help.

With the new version of 9 March does not so this. How do you set up in the new version of the port forwarding?
Sample Code:
using (var client = new SshClient("host", "username", "password"))
{
    client.Connect();
    var port = client.AddForwardedPort<ForwardedPortLocal>(8084, "host to be forwarded", 80);
    port.Exception += delegate(object sender, ExceptionEventArgs e)
    {
        Console.WriteLine(e.Exception.ToString());
    };
    port.Start();

    Thread.Sleep(1000 * 60 * 20); //    Wait 20 minutes for port to be forwarded

    port.Stop();
}

Please send me a short answer. Thank you.
Best regards,
Tora
Coordinator
Mar 13, 2012 at 4:08 PM

Sorry, forgot to mention that in release notes, I will add it there too in a min but here is a change I made:

                //var port1 = ssh.AddForwardedPort<ForwardedPortLocal>("localhost", 8084, "www.renci.org", 80);
                var port1 = new ForwardedPortLocal("localhost", 8084, "www.renci.org", 80);

Hope it helps,

 

Sorry for this change but had to make it to keep it consistent for new dynamic port functionality of which I didnt know when I was developing port forwarding feature.

 

Thanks,

Oleg

Mar 13, 2012 at 4:08 PM

I did something like this to get it to work...

this.sshClient.Connect();
AddForwardedPort(this.sshClient, "127.0.0.1", 22000, "localhost", 80);

private void AddForwardedPort(SshClient client, string boundHost, uint boundPort, string host, uint port)
{
	var forwarded = new ForwardedPortLocal(boundHost, boundPort, host, port);
	forwarded.Start();
	client.AddForwardedPort(forwarded);
}


Then when you disconnect the client, it will automatically close your forwarded ports.

Mar 14, 2012 at 10:16 AM

Thanks, that works.

Apr 9, 2012 at 12:07 AM
Edited Apr 9, 2012 at 12:08 AM

I have a remote server (10.18.224.50) that has a postgresSQL DB on port 5430.  I am trying to establish a tunnel and the connection to the DB times out.

I have validated the connection is valid using a couple command line send/returns.

Here is my code for the port Forwarding.

 

 

var port = new ForwardedPortLocal("localhost",5430,"10.18.224.50",5430);
                    port.Start();
                    sshClient.AddForwardedPort(port);
                    

                    string connstring = string.Format("Server=localhost;Port=5430;User Id=myLogin;Database=testDV;Pooling=false;");
                    conn = new NpgsqlConnection(connstring);
                    conn.Open();

 

I have done this in the past by opening a plink pipe with the following.

-L 5430:localhost:5430
in the params.  Obviously I would like to get to all native code if possible so I am hoping someone can help me get this to work.
Thanks
Coordinator
Apr 9, 2012 at 5:48 PM

Hi,

 

If I remember correctly you need to start port after you add it and not before. I guess I should throw an exception in that case, if so please open an issue so I could get back to it when I have a little of time.

Here the code that I use to test:

            using (var client = new SshClient(connectionInfo))
            {
                client.Connect();

                var port1 = new ForwardedPortRemote(8081, "www.renci.org", 80);
                client.AddForwardedPort(port1);
                port1.Exception += delegate(object sender, ExceptionEventArgs e)
                {
                    Console.WriteLine(e.Exception.ToString());
                };
                port1.RequestReceived += delegate(object sender, PortForwardEventArgs e)
                {
                    Console.WriteLine(e.OriginatorHost + ":" + e.OriginatorPort);
                };
                port1.Start();

                var port2 = new ForwardedPortLocal("152.54.8.108", 8084, "www.renci.org", 80);
                client.AddForwardedPort(port2);

                port2.Exception += delegate(object sender, ExceptionEventArgs e)
                {
                    Console.WriteLine(e.Exception.ToString());
                };
                port2.RequestReceived += delegate(object sender, PortForwardEventArgs e)
                {
                    Console.WriteLine(e.OriginatorHost + ":" + e.OriginatorPort);
                };
                port2.Start();

                Thread.Sleep(1000 * 60 * 10);

            }


Hope it helps.

 

Thanks,

Oleg

Apr 9, 2012 at 6:07 PM

Hi,

thank you for the anwser. I have corrected my code and it work.

Now I wait for a new version with ssh compressen with or without 3th library. I hope the ssh.net team found some minutes for this topic.

Big thanks....

Best regards,

Torakas

Apr 10, 2012 at 2:42 AM

Sadly I am still having problems.  I have a small netstat like application I use to monitor this.  Here is what it looks like when plink is established with the port forward.

127.0.0.1:5430 is listening
::1:5430 is listening

When I establish the connection through 5430 it looks like this.

::1:15430 <-> ::1:53038 ESTABLISHED
::1: 53038 <-> ::1:15430 ESTABLISH

When I use the library the listen looks like this.

::1:5430 is listening

and I see an established connection momentarily.

::1:5430 is listening
::1:5430 <-> ::1:53293 ESTABLISHED
 But i get the following exception every time.

A timeout has occured. If you were establishing a connection, increase Timeout value in ConnectionString. If you were executing a command, increase the CommandTimeout value in ConnectionString or in your NpgsqlCommand object.

Here is my current code for setting up the tunnel

var port = new ForwardedPortLocal("localhost",5430,"10.18.224.50",5430);                    
                    sshClient.AddForwardedPort(port);
                    port.Start();

Do I have to sleep to allow it to setup or something?

Coordinator
Apr 13, 2012 at 2:00 PM

Hi,

 

Sorry for late response.

Well, the error that you get is coming probably from postgres.

After you start port listening you do need either to set a timer or to do something else, to ensure the program still running, cuase whtat proablky happens is the connection closes and port forwarding no longer happens.

 

Hope it helps.

 

Thanks,

Oleg

Apr 13, 2012 at 5:39 PM

Hi all,

I dont know you have the same problem what I have for a long time.

I have write a SQL client / server sync library between two SQL Server over SSH.

My problem was after close all SQL connection and ssh connection I have in NETSTAT dead port connections. If I reconnect for a resync in the same program session I have SQL connection timeouts. If I restart the program the dead port conntections thats not longer there, only after the sync process.

I have try a many with the ssh.net library but the problem is not in the ssh library. The problem is in the connection pooling of Windows. Windows know it have already a port conntection to .... and will use the, but the connection is not longer available.....

If you have dead port connection in NETSTAT, ty to clear the connection pool.

In C# will clear the SQL Client connection pool with this two code lines:

  • SqlConnection.ClearPool( SqlConnectionObject ).
  • SqlConnection.ClearAllPools().

I have this for my ssh connection....

Try this. I hope it help or is the way what you search.....

 

Best regards,

Torakas

 

Apr 13, 2012 at 5:45 PM

I do not believe my code or the pooling is the case.  I can substitute the usage of plink with the tunnell and it works without issue.  I will create a sample application this weekend that demonstrates this and post it here.

 

Thanks

Apr 19, 2012 at 3:35 PM

Sorry for the delay but here is what I figured out.

You were correct that the port was being forwarded.  What I ran across was the name provided for the remote port.

Meaning when I had this...

            if (sshClient.IsConnected)
            {
                try
                {
                    var port = new ForwardedPortLocal("localhost",5430,"readhat_lab",5430);                    
                    sshClient.AddForwardedPort(port);
                    port.Start();                                        
                }//try
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
         
            }

The application would time out.  I was playing with different host names this morning and I tried the IP address and finally received an error from Postgress that the IP didn't have permissions for the connection.  So after a couple tries, I found that it would connect with the following.

 

            if (sshClient.IsConnected)
            {
                try
                {
                    var port = new ForwardedPortLocal("localhost",5430,"localhost",5430);                    
                    sshClient.AddForwardedPort(port);
                    port.Start();                                        
                }//try
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
         
            }
This will allow me to connect and get straight into PostgresSQL.  A little odd to me, but it works.