Code valid for me, using sshkeyfile (*.pem), C# Mysql -> Aurora in Amazon Web Services:
class Program
{
static string SshHostName = "***";
static string SshUserName = "***";
static string SshKeyFile = @"C:\Work\pems\***.pem";
static string Server = "***.eu-west-1.rds.amazonaws.com";
static uint Port = 3306;
static string UserID = "***";
static string Password = "***";
static string DataBase = "***";
static void Main(string[] args)
{
ConnectionInfo cnnInfo;
using (var stream = new FileStream(SshKeyFile, FileMode.Open, FileAccess.Read))
{
var file = new PrivateKeyFile(stream);
var authMethod = new PrivateKeyAuthenticationMethod(SshUserName, file);
cnnInfo = new ConnectionInfo(SshHostName, 22, SshUserName, authMethod);
}
using (var client = new SshClient(cnnInfo))
{
client.Connect();
if (client.IsConnected)
{
var forwardedPort = new ForwardedPortLocal("127.0.0.1", Server, Port);
client.AddForwardedPort(forwardedPort);
forwardedPort.Start();
string connStr = $"Server = {forwardedPort.BoundHost};Port = {forwardedPort.BoundPort};Database = {DataBase};Uid = {UserID};Pwd = {Password};";
using (MySqlConnection cnn = new MySqlConnection(connStr))
{
cnn.Open();
MySqlCommand cmd = new MySqlCommand("SELECT * FROM PostalCodes LIMIT 25;", cnn);
MySqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
Console.WriteLine($"{reader.GetString(1)}, {reader.GetString(2)}, {reader.GetString(3)}");
Console.WriteLine("Ok");
cnn.Close();
}
client.Disconnect();
}
}
}
}
plink
program that comes as part of Putty. Perhaps not the most elegant, but it should work. The-L
(forward a local port) parameter will be of interest here. Credentials can be supplied in a number of methods. I recommend using PKI. – Romeliaromelle