1

I am using scp to copy a file from one server to another. Both of the servers need a password to be passed with sshpass so I need to use sshpass twice in the same command. Note that I am using the -3 flag because server foo cannot directly communicate with server boo. I tried sshpass -p 'foo' scp -3 foo@foo.com:/home/foo/foo.txt sshpass -p 'boo' boo@boo.com:/home/boo/ but it didn't work, no error message, just didn't copy the file. Is there a way to accomplish this?

Note: Please don't suggest answers using key pair, I want to pass in the password rather than using keys (or prompting the user to type the password), I know it is visible to the whole system and that it is not secure, I still want to do it.

Silidrone
  • 1,471
  • 4
  • 20
  • 35
  • 1
    Two ideas: ☐copy the file to your local machine and then, in a different step, to the 2nd server; ☐ssh to the first server, and then scp the file from there to the second server (this could even be done non-interactively, e.g. `sshpass -p 'foo' ssh foo@foo.com 'sshpass ...'`) – bitinerant Dec 26 '20 at 20:31
  • That seems like a solution, although I should have mentioned I am using the -3 flag and that server foo cannot access server boo and vice versa. Only my local machine can access server boo because it has a private VPN connection to it, I wanted to make a minimal reproducable example but I guess I should've included that. Do you recommend me editing the question? – Silidrone Dec 27 '20 at 15:01
  • Thanks for clarifying. Yes, it would be good to add `scp ... -3 ...` to the OP. My first suggestion should work, then, unless you don't have enough disk space. Another option is to stream the file from remote1 to your local computer and then on to remote2. I could probably write this up as an answer if you think it would solve your problem. – bitinerant Dec 27 '20 at 16:24
  • I actually did that, but I was specifically wondering if it is possible to use sshpass in this way to pass the password to two servers. I solved my problem by copying to my local machine from server foo and then from my local machine copying the file to server boo. Although I am still wondering if it is actually possible to accomplish this with sshpass. So I guess if you write an answer, what I'd accept is an answer that uses sshpass passing the password to the both servers. – Silidrone Dec 27 '20 at 17:03
  • But you may add the first suggestion as an answer and I will upvote it so that future visitors benefit from it. – Silidrone Dec 27 '20 at 17:08
  • 1
    "it didn't work, no error message" -- [sshpass is broken in many ways.](https://github.com/clarkwang/passh/blob/master/sshpass-broken.md) use it carefully. – sexpect - Expect for Shells Dec 29 '20 at 03:58

1 Answers1

1

This is not possible.

According to scp manual:

  • -3

    Copies between two remote hosts are transferred through the local host. Without this option the data is copied directly between the two remote hosts. Note that this option disables the progress meter and selects batch mode for the second host, since scp cannot ask for passwords or passphrases for both hosts.

And according to ssh_config manual:

  • BatchMode

    If set to yes, user interaction such as password prompts and host key confirmation requests will be disabled. This option is useful in scripts and other batch jobs where no user is present to interact with ssh(1). The argument must be yes or no (the default).

pynexj
  • 19,215
  • 5
  • 38
  • 56
  • You have done a good job of explaining why using the `-3` option means `sshpass` won't work. However, it should still be possible to ssh to the first host, `cat` the file, and then on the local computer, pipe the output to the second host, all while using `sshpass` for both hosts. – bitinerant Dec 28 '20 at 16:59
  • "since scp cannot ask for passwords or passphrases for both hosts.", I think this is true for even when not using the -3 flag, right? Because: `sshpass -p 'foo' scp foo@foo.com:/home/foo/foo.txt sshpass -p 'boo' boo@boo.com:/home/boo/` results the same as with the -3 flag. – Silidrone Dec 29 '20 at 09:26
  • that would not work. sshpass is a command. it's not suppose to be another commands parameters. – pynexj Dec 29 '20 at 09:32
  • you can try like this: `sshpass -p pass ssh host1 cat /src/file | sshpass -p pass ssh host2 "cat > /dst/file"` – pynexj Dec 29 '20 at 09:34