1

I have to access the database in MySQL that is on the computer with IP (1.1.1.1) with an external network.

I created a tunnel with SSH.NET, but I can't access the IP (1.1.1.1) of the computer where the database is located in MySQL.

Up to the Public IP the tunnel seems to work, but I can't access the Computer IP (1.1.1.1), since I didn't understand how SSH.NET works. Using PuTTY I enter the public IP of the port and in the SSH tunnel section I insert the IP computer (1.1.1.1) with port 3306 and it works very well. The code is this

using (var client = new SshClient("ip public", port, "user", "password")) 
{
    client.Connect();
    if (client.IsConnected)
    {
        var portForwarded = new ForwardedPortLocal("127.0.0.1",22, "1.1.1.1",3306);
        client.AddForwardedPort(portForwarded);
        portForwarded.Start();

        string db = @"Server=127.0.0.1;Port=3306;Database=xxx;Uid=root;Pwd=xxx;";
        {
            var mySQLCon = new MySqlConnection(db);

            mySQLCon.Open();

            if (mySQLCon.State == ConnectionState.Open)
            {
                MainWindow stx = new MainWindow(db);
                stx.ShowDialog();
            }
        }
    }
}

Am I wrong to put the parameters in ForwardedPortLocal?

ForwardedPortLocal("127.0.0.1",22, "1.1.1.1",3306);

I wanted to know if I entered the parameters in ForwardedPortLocal correctly, or where I am wrong in making tunnels with SSH.NET ...

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
IlSanto
  • 61
  • 2
  • 7

1 Answers1

1

The boundPort argument of ForwardedPortLocal is the local port which will be forwarded. That's the port that you connect to with MySQL client. So not 22.

While you can hard-code a port like 3306, you better let SSH.NET auto-assign a free local port by omitting that parameter:

var portForwarded = new ForwardedPortLocal("127.0.0.1", "1.1.1.1", 3306);

And then use ForwardedPortLocal.BoundPort in your connection string, instead of hard-coding 3306.


See also:

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992