Connecting SSH to MySQL using the SSH.NET library

I was given a link to use this library to connect through an SSH connection to a MySQL database with C #.

Here is the LINK to the library, as many of you will find it interesting, since you do not need to open the SSH channel manually.

This is a very detailed library and has many features, but what I want to do is pretty simple. I just want to open the SSH channel every time I want to write to the database.

While I was looking for the source that was provided, I went over to this part of the code, which can be found in TestSshCommand.cs , and I think I can use this part for my connections, but I need your help to make it work, since I have a connection string. I do not know how to put it all together.

the code:

public void Test_Execute_SingleCommand()
        {
            using (var client = new SshClient(Resources.HOST, Resources.USERNAME, Resources.PASSWORD))
            {
                client.Connect();
                var result = ExecuteTestCommand(client);
                client.Disconnect();

                Assert.IsTrue(result);
            }

My code for connecting to a channel without SSH:

con.ConnectionString = ConfigurationManager.ConnectionStrings["Con2"].ConnectionString;
            con.Open();
            cmd = new MySqlCommand("SELECT COUNT(*) FROM " + ConfigSettings.ReadSetting("main"), con);

     //...... more of the code

So, how can I combine this code with the above so that it can open the SSH channel first and then fulfill my request?

Do I need to embed my code inside to connect and disconnect functions?

: .dll , , . -, , , , ( ) ?

EDIT:

, . SSH , .

, . , , - , , mysql

using (var client = new SshClient("cpanel****", "******", "******"))
          {
            client.Connect();
            con = new MySqlConnection(GetConnectionString());
            con.Open();
            cmd = new MySqlCommand(String.Format("insert into {0} values (null, ?Parname , ?Parname2, ?Parname3, ?Parname4, ?Parname5, ?Parname6, ?Parname7);", ConfigSettings.ReadSetting("main")), con);
            cmd.Parameters.Add("?Parname", MySqlDbType.Double).Value = Math.Round(deciLat, 5);
            // ... more parameters
            cmd.ExecuteNonQuery();
            client.Disconnect();
           }
+5
2

SSH-...

SSH, 3306 MySQL MySQL...

MySQL DB server=... server=localhost, SSH IP- MySQL.

3306, , , port= ... . ... SSH - 3306, ...

( SSH.NET, SSH.NET, IMO):

+4

, 3306, MySQL.

8001:

using (var client = new SshClient("ssh_hostname", "ssh_username", "ssh_password"))
{
  client.Connect();
  var tunnel = new ForwardedPortLocal("127.0.0.1", 8001, "127.0.0.1", 3306);
  client.AddForwardedPort(tunnel);
  tunnel.Start();
  using (var DB = new DBContext())
  {
    //MySQL interaction here!
    scores = DB.Table
      .Where(x => !String.IsNullOrEmpty(x.SomeField))
      .ToList();
  }
  tunnel.Stop();
}

, 127.0.0.1 8001

server=127.0.0.1;port=8001

, 127.0.0.1:8001, ssh_hostname: 3306.

+6

All Articles