I can successfully connect to the remote SSH server with PowerShell using config:
Host *MySSHHost*
HostName *sshHostName*
Port *sshPort*
User *sshUserName*
In my C# code I can successfully connect and disconnect to the SSH server remotely as well.
try
{
client = new SshClient(connectionInfo);
client.Connect();
if (client.IsConnected)
{
if (forwardedPort == null)
{
forwardedPort = new ForwardedPortLocal("localhost", *sshHostName*, remoteSQlPort);
client.AddForwardedPort(forwardedPort);
forwardedPort.Start();
}
string sqlConnectOverSSH = $"Server = {forwardedPort.BoundHost};Port = {forwardedPort.BoundPort};Database = {remoteSqlDatabaseName};Uid = {*sshUserName*};";
}
}
catch (Exception ex)
{
Debug.WriteLine(ex);
}
This is the SQL connection string that works to connect locally to the SQL Server on the SSH server:
sqlConnect = @"integrated security=SSPI;data source=" + userMachineName + @"\SQLEXPRESS;persist security info=False;initial catalog=" + remoteSqlDatabaseName;
When trying to use the sqlConnectOverSSH string to get data over the SSH tunnel from the remote SQL database, I get the error
System.ArgumentException: Keyword not supported: 'port'
(The SQL ports of the connecting PC and the Server is different.) I am using Renci.SSH.Net
I believe my error is in the connection string?