0

I have a case when there are 2 remote hosts, each with a unique port number, which i both need to indicate when using SCP command. There is no problem indicating the unique (non 22) port number for the first one, but i can't figure out how to indicate it for the second one.

Here is how it looks like now -

scp -P 1234 username@site.com:/path/to/file username2@site2.com:/path/to/directory
edgarsvil
  • 15
  • 7

2 Answers2

1

The way that you are running scp in your question, it would just invoke the scp program on the first host, telling it to send the file to the second host. You can directly invoke ssh to run the scp command on the first host however you like it:

ssh -p1234 user1@host1 'scp -P2345 /path/to/file user2@host2:/path/to/directory'

If the first host can't connect directly to the second host, you'd normally use the scp option -3 to send the data from the first host to the second host through you local host. If you're in that position, you could emulate how scp runs that kind of transfer, though it's sort of a hack:

yes "" | tr '\n' '\0' |
    ssh -p1234 user1@host1 'scp -f /path/to/file' |
    ssh -p2345 user2@host2 'scp -t /path/to/directory'

This is based on how the SCP protocol works. The first ssh line runs scp on the first remote host in "remote source" mode to send the source file. The second ssh lines runs scp on the second host in "remote sink" mode to receive the file and write it to the directory. "remote source" and "remote sink" are what scp normally runs on the remote system when receiving or sending files, respectively.

The yes "" | tr '\n' '\0' | line creates a stream of NUL (Ascii 0) bytes which are needed for the protocol to work. The scp -f instance reads these and won't send files without them. There are other ways to generate a stream of NUL bytes if you like.

Kenster
  • 23,465
  • 21
  • 80
  • 106
  • I think, there is also a problem with SSH keys. It seems, they are not getting read correctly. I am entering passphrases, they are not accepted, and then i am getting lead to enter password, and it doesn't get accepted either. This is for the second option that you offer in your answer. When i SSH separately into both accounts i have no problems with passphrases and reading SSH keyfiles. It is confusing. – edgarsvil May 19 '15 at 19:14
  • Yes, with the second example, it'll be almost impossible to enter passwords or passphrases for both ssh instances. You can only bend the command-line utilities so far. – Kenster May 19 '15 at 19:41
-1

no idea how to do this with arguments, but you can try editing ~/.ssh/config

Host test
  HostName test.com
  Port 22000
  User me