This project is read-only.

Run scp command from remote server via SSHClient

Nov 6, 2013 at 4:42 PM
Edited Nov 6, 2013 at 4:44 PM
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@

I was hoping to automated this process.

Nov 6, 2013 at 5: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 5: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 7:13 PM
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
  // Do your stuff, Open SSH/SCP/SFTP
  var client2 = new  SshClient("localhost", 2222, Server2Username, Server2Password);
Nov 15, 2013 at 1: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?