1

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?

MindSwipe
  • 7,193
  • 24
  • 47
  • The second using is just trying to connect to machine B directly, which you stated you cannot do. You need to tell your `client` object to connect to machine B instead of going directly for it – MindSwipe Mar 19 '19 at 11:36
  • "You need to tell your client object to connect to machine B instead of going directly for it" how can I do that? – Valantis Stergiopoulos Mar 19 '19 at 11:38
  • You are probably port forwarding from A to B and need to do the same thing. This is old but still might help - https://stackoverflow.com/questions/2835646/net-ssh-port-forwarding – JAZ Mar 19 '19 at 11:38
  • Just like you would do it on your command line, so something along the lines of `var command = client.CreateCommand("ssh id@server");` and then execute that. There might be an alternative although I'm not very experienced with ssh.net – MindSwipe Mar 19 '19 at 11:41
  • Opening a 2nd client does nothing. You need to send commands through the first SshClient to open a connection to MachineB. – jdweng Mar 19 '19 at 11:43
  • @jdweng, i've tried this, the command does not run because I can't pass the password for machine B using var command = client.CreateCommand("ssh id@server"); – Valantis Stergiopoulos Mar 19 '19 at 11:48
  • As far as I understand [this](https://dotnetfiddle.net/RMk6pw) should work. The problem with supplying the password can be fixed using [this](https://serverfault.com/questions/241588/how-to-automate-ssh-login-with-password) serverfault question to set up passwordless authentication. And if for some reasons you cannot use key based authentication [this](https://serverfault.com/a/512220) answer shows how to do it using a package – MindSwipe Mar 19 '19 at 11:48
  • @MindSwipe, the first link is exactly what I need, but I do not have permission to create ssh-keygen for RCA keys in any of both machines, I need to pass the password with user input. Is there any other way to pass the password from my app for the 2nd machine? – Valantis Stergiopoulos Mar 19 '19 at 11:58
  • Check out [this](https://serverfault.com/a/512220) answer on the question – MindSwipe Mar 19 '19 at 11:59
  • It you can connect by typing password in putty there should not be any reason you can't do same through connection unless the client is using a different encoding than putty. What connection options are you using in putty? – jdweng Mar 19 '19 at 12:10
  • @jdweng: userMachineA@MachineA:/path/path/path$ ssh root@MachineB root@MachineB's password: Putty asks for password after ssh command, I need a command to pass the password after Machine's A prompt. – Valantis Stergiopoulos Mar 19 '19 at 12:20
  • Automating `ssh` execution and password input is a *hack, not a solution*. The *standard API* for your problem is port forwarding. – Martin Prikryl Mar 19 '19 at 12:22
  • 1
    Guys, indeed issue solved with port forwarding, thank you all! – Valantis Stergiopoulos Mar 19 '19 at 12:28
  • Why do you need a command? Just send the password if you have a text window opened. Do you have a shell window opened or is the password in a dialog? – jdweng Mar 19 '19 at 12:29

0 Answers0