This project is read-only.

SSH Tunnel / Port Forwarding

Sep 12, 2011 at 11:44 PM
Edited Sep 13, 2011 at 6:07 PM

I am trying to establish an SSH tunnel from a local port to the RDP port on a remote machine.

In PuTTY/PLink or another SSH client, I would use the following arguments:

ssh [server] -L [local_port]:[remote_host]:[remote_port_3389] -l [Username] -pw [Password]
(eg.  ssh -L -l user -pw password

Could you please tell me how to achieve the same thing using this library using VB .NET?

Much thanks!


Sep 14, 2011 at 2:08 PM



Sorry for delay response.


This should work:

            using (var client = new SshClient(connectionInfo))
                var port1 = client.AddForwardedPort<ForwardedPortLocal>("localhost", 8084, "", 80);
                port1.Exception += delegate(object sender, ExceptionEventArgs e)
                port1.RequestReceived += delegate(object sender, PortForwardEventArgs e)
                    Console.WriteLine(e.OriginatorHost + ":" + e.OriginatorPort);

                Thread.Sleep(1000 * 60 * 10);


Let me know if you have any questions or problems.




Feb 12, 2012 at 12:56 AM
Public Class SSHTunnelCreator
    Dim SSHTunnel As SshClient
    Dim portforward As New ForwardedPortLocal

    Private Sub CreateTunnel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CreateTunnel.Click
            SSHTunnel = New SshClient(Server.Text, UserName.Text, UserPassword.Text)
            StatusBox.Text = "Connecting SSH"
            StatusBox.Text = "Connected SSH"
            portforward = SSHTunnel.AddForwardedPort(Of ForwardedPortLocal)(5900, Server.Text, 5900)
            If SSHTunnel.IsConnected Then
                StatusBox.Text = "Starting Port forwarding"
                StatusBox.Text = "Started Port forwarding"
                If portforward.IsStarted Then
                    StatusBox.Text = "Sending vncgnome"
                    StatusBox.Text = "Sent vncgnome"
                End If
            End If
        Catch ex As Exception
        End Try
    End Sub

    Private Sub DisconnectTunnel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DisconnectTunnel.Click
            If SSHTunnel.IsConnected Then
                StatusBox.Text = "Sending vncgnome -kill"
                SSHTunnel.RunCommand("vncgnome -kill")
                StatusBox.Text = "Sent vncgnome -kill"
                If portforward.IsStarted Then
                    StatusBox.Text = "Stopping port forwarding"
                    StatusBox.Text = "Stopped port forwarding"
                End If
                StatusBox.Text = "Disconnecting SSH"
                StatusBox.Text = "Disconnected SSH"
            End If
        Catch ex As Exception
        End Try
    End Sub
End Class

Hi I tried this in VB.Net and its not working.  I want to be able to test this by clicking connect and then running vnc to localhost and I was expecting to be able to connect.  I tested it out using Putty with same settings and it works.




Feb 27, 2012 at 2:25 PM


I am not a VB person so not sure where to look for a problem.

One thing could be is may be 5000 is not enough time to send the command, or may be you trying to achieve here something else.




Jun 5, 2012 at 7:04 AM

did this ever work?


IMO, i think what @coluwyvurne wants is simply to achieve the port forwarding functionality close to what Putty  provides.


Jun 5, 2012 at 8:30 AM
It never worked in Didn't matter what I did.

Sent from my iPad

On Jun 4, 2012, at 11:04 PM, "mVincent"<> wrote:

From: mVincent

did this ever work?

IMO, i think what @coluwyvurne wants is simply to achieve the port forwarding functionality close to what Putty provides.

Jun 5, 2012 at 10:33 AM
Edited Jun 5, 2012 at 10:35 AM


I got problem with forwarding as well:/

This is my MySql connection which works fine when im connecting to DB via MySql Workbenhc

ssh Hostname : ssh_host_ip:ssh_port

ssh username: ssh_username

ssh password: ssh_password

MySQL Hostname: sql_hostname

MySQL Server Port: sql_port

Username: sql_username

Password: sql_password


and here is my code:


using System;
using Renci.SshNet;
using Renci.SshNet.Channels;
using MySql.Data.MySqlClient;
using mysql;
using MySql.Data.Entity;
using MySql.Data.Common;
using MySql.Web.Common;
using MySql.Web.General;
using System.Net.Sockets;

namespace mysql
	class MainClass
		public static void Main (string[] args)
			var client = new SshClient ("ssh_host_ip", ssh_port, "ssh_username", "ssh_password"); 
			client.Connect ();
			if (client.IsConnected) {
				System.Console.WriteLine ("tunel ok");
			} else {
				System.Console.WriteLine ("tunel zle");
			var porcik = new ForwardedPortLocal ("localhost", 8011, "sql_hostname", sql_port);



			client.AddForwardedPort (porcik);
			porcik.Start ();
			if (porcik.IsStarted) {
				System.Console.WriteLine ("port ok");
			} else {
				System.Console.WriteLine ("port beee");
			string strConnection = "Server=sql_hostname; Port=sql_port; Database=sql_db; Uid=sql_user; Pwd=sql_password;";
			MySqlConnection sql = new MySqlConnection (strConnection);
			sql.Ping ();
			catch (MySqlException)


Console shows : tunel ok, port ok but then i got :

"Unable to connect to any of the specified MySQL hosts."

As SSH tunel seems to work fine I'm almost sure that i messed up with port forwarding, tried a lot of combinations with no succes:(

Thx for help.

Jun 7, 2012 at 7:49 PM

Sql hostname is localhost  the port you don't need

Jun 7, 2012 at 8:11 PM
I didn't get it to work in vb but it works in c++

Sent from my iPad

On Jun 7, 2012, at 11:49 AM, "Daviscd01"<> wrote:

From: Daviscd01

Sql hostname is localhost the port you don't need

Mar 24, 2013 at 6:52 PM
Edited Mar 24, 2013 at 6:54 PM
I have the same problem. It looks like it is something wrong with the ForwardedPortLocal. I am able to connect to my MySql server when OpenSSH is running, so I am sure my connection string is OK. I'm also able to "redirect", for example, localhost:777 to using ForwardedPortLocal. But I dont know how to use ForwardedPortLocal together with MySql. I tried all the examples I found on the net but without success.

The error is:

MySql.Data.MySqlClient.MySqlException (0x80004005): Unable to connect to any of the specified MySQL hosts.
w MySql.Data.MySqlClient.NativeDriver.Open()
w MySql.Data.MySqlClient.Driver.Open()
w MySql.Data.MySqlClient.Driver.Create(MySqlConnectionStringBuilder settings)
w MySql.Data.MySqlClient.MySqlPool.CreateNewPooledConnection()
w MySql.Data.MySqlClient.MySqlPool.GetPooledConnection()
w MySql.Data.MySqlClient.MySqlPool.TryToGetDriver()
w MySql.Data.MySqlClient.MySqlPool.GetConnection()
w MySql.Data.MySqlClient.MySqlConnection.Open()
Jun 12, 2013 at 12:33 PM
where you able to get it to connect, because i'm having the same problem using C#, trying to connect to mysql database after connecting to the server but no success

by the way i have the same connection setup in the MYSQL workbench and its working fine

for the below code the server is using the host "data" not "localhost", don't know why, but that's how they set it up.

the error i get is "Unable to connect to any of the specified MySQL hosts."
 Console.WriteLine("Ssh connection sucessful");
                            uint sqlPort = 3307;
                            var portFwd = new ForwardedPortLocal("", sqlPort, "data", sqlPort);
                            if (portFwd.IsStarted)
                                Console.WriteLine("Port Forwarding has started.");
                                Console.WriteLine("Port Forwarding has failed.");

                            string connStr = "server=data;database=db_name;uid=my_sql_username;pwd=my_sql_server_password;"; 
                            MySqlConnection mysqlConn = new MySqlConnection(connStr); 

                                Console.WriteLine("Connecting to MySQL...");
                                Console.WriteLine("MySql database connection activated");
                                string query = "select * from table_t1";  
                                MySqlCommand cmd = new MySqlCommand(query, mysqlConn);
                                MySqlDataAdapter da = new MySqlDataAdapter(cmd);
                                DataTable data = new DataTable();


                                dataGridView1.DataSource = data;

                                Console.WriteLine("Query Successful");

                            catch (SocketException ex)

                            Console.WriteLine("Ssh connection failed");
any help would be great

thanks alot
Jan 16, 2014 at 2:00 PM
Edited Jan 16, 2014 at 2:01 PM
In my opinion you are using a wrong connection string.
After forwarding your local port to the remote one through ssh, you must connect using "locahost", not "data".
More, MySql standard port is 3306, not 3307, so you should declare it in connection string.
So you should use
string connStr = "server=localhost;port=3307;database=db_name;uid=my_sql_username;pwd=my_sql_server_password;"; 
Hope this solves your problem,
Jan 23, 2014 at 8:37 AM
I had the problem when I used that override:
public ForwardedPortLocal(uint boundPort, string host, uint port);
var port = new ForwardedPortLocal(27017, "", 27017);
Instead, I used
var port = new ForwardedPortLocal("", 27017, "", 27017);
and forwarding started working.