I have a target computer which can be accessed only with SSH, I do not have a direct connection, so I need to connect through another SSH machine.
Until now, I connect with putty (SSH) to machine A and then when I am logged in I SSH the machine B.
I'm trying to create an application that will run a script in machine B and I use SSH.NET, here is my code:
using (var client = new SshClient("hostMachineA", "usernameMachineA", "passwordMachineA"))
{
client.Connect();
try
{
if (client.IsConnected)
{
using (var terminalConnection = new SshClient("hostMachineB", "usernameMachineB", "passwordMachineB"))
{
terminalConnection.Connect();
var command = terminalConnection.CreateCommand("ls -ltr");
command.Execute();
MessageBox.Show(command.Result);
terminalConnection.Disconnect();
}
}
client.Disconnect();
}
catch (Exception command)
{
MessageBox.Show(command.Message);
}
}
The problem is that my application connects to Machine A but it cannot establish connection with Machine B, it tries to create a new connection not using the connection which is already established from Machine A.
Any idea?