This project is read-only.

SSH command using the RunCommand() doesn't work?

Jul 7, 2013 at 2:20 PM
Hi Guys,
I was just wondering why ssh command doesn't work on RunCommand. It hangs up.
Can anyone direct me? if it's not possible, is there any other way to do it? Thanks.
            using (var client = new SshClient("192.168.1.10", "root", "magic!0n3"))
            {
                client.Connect();
                var cmd1 = client.RunCommand("ssh root@192.168.1.11");
                //MessageBox.Show(cmd1.Result);
                client.Disconnect();
            }
Jul 8, 2013 at 4:47 PM
Most likely it is waiting for a password on the remote shell. I'm having similar issues and posted a question regarding secondary logins but with no help yet.
Jul 9, 2013 at 4:26 AM
Hi BitFlipper,

Base on my experiment. I hope this helps. Anyone is invited to add new ideas.
This works for me. RSA-Key generated then throw secondary command.. something like LS.
            using (var client = new SshClient("192.168.1.10", "root", "magic!0n3"))
            {
                client.Connect();
                var cmd1 = client.RunCommand("ssh root@192.168.1.11;ls;");
                //MessageBox.Show(cmd1.Result);
                client.Disconnect();
            }
Jul 9, 2013 at 6:13 AM
Hi,

There are two ways to do this
  1. ShellStream and Async write/read (quite difficult)
  2. Use PortForwarding
    In your case you want the ssh port (22) of the machine 192.168.1.11
(untested pseudo code)
         using (var client = new SshClient("192.168.1.10", "root", "magic!0n3"))
            {
                client.Connect();

                // foward remote ssh port to your local machine
                ForwardedPortLocal forwarding =  new ForwardedPortLocal("127.0.0.1", "12345", "192.168.1.11", "22");
                client.AddForwardedPort(forwarding);
                forwarding.Start();

                // connect to it
                SshClient forwardedSsh = new SshClient("127.0.0.1", 12345, "root", "password/key");
                forwardedSsh.Connect();

                // run your commands
                MessageBox.Show(forwardedSsh.RunCommand("ls").Result);
            }
RunCommand() is for short and non-interactive commands.
Another Ssh is a long and interactive command.