Run scp command from remote server via SSHClient

Nov 6, 2013 at 3:42 PM
Edited Nov 6, 2013 at 3:44 PM
Hello,
I have a scenario:

I have two server(Server1, Server2). Server1 I can reach via my desktop, using the SftpClient I upload a file(no problem) .

I now have to transfer the file(the one I just uploaded) via SCP from Server1 to Server2. I can only reach Server2 through Server1.

So my question is : Is there a way using the SSHClient to run the SCP command from Server1?

Special note: when I run the command manually it ask me for a password.

the SCP command looks like this :
scp myFile root@1.1.1.1:/folder/folder/floder/folder/folder/myFile

I was hoping to automated this process.

Thanks
Nov 6, 2013 at 4:22 PM
Use Portforwarding. LocalForward the SSH-Port of Server2 via Server1 to your desktop machine.
Then you can connect to the local forwarded port with SshClient or do your SCP directly.
Nov 6, 2013 at 4:58 PM
Hi da_rinkes,

Thanks for your quick reply!

After some research on this forum I found out that I could uses the menthod here:

CreateShellStream link

by using the CreateShellStream I was able to run the command and enter the password from Server2.
But I'm very interested in the Portforwarding idea, (do you have any form links to show some examples) :-) I'll look around for some example myself and see if I can figure out how that works. I really new to this ssh stuff. I'm leaning as I go.

Thanks again kc22scy (Mike)
Nov 6, 2013 at 6:13 PM
Example:
using (var client = new SshClient(Server1IP, 22, Server1Username, Server1Password)
{
  client.Connect(); // Connection Desktop-Machine to Server1

  var port1 = new ForwardedPortLocal("localhost", 2222, Server2IP, 22); // Forward the SSH-Port of Server2 via Server1 on the Desktop-Machine
  client.AddForwardedPort(port1);
  port1.Start();
  // Do your stuff, Open SSH/SCP/SFTP
  var client2 = new  SshClient("localhost", 2222, Server2Username, Server2Password);
  client2.Connect();
}
Nov 15, 2013 at 12:19 AM
Hi da_rinkes,

I have a similar situation in which I use private keys. Any attempt to scp a file from Server1 to Server2 is met with permission denied (publickey).

Is there a way I may provide authentication?

Thanks,
Kelley