1

These three lines of code require authentication twice. I don't yet have password-less authentication set up on this server. In fact, these lines of code are to copy my public key to the server and concatenate it with the existing file.

How can I re-write this process with a single ssh command that requires authentication only once?

scp ~/local.txt user@server.com:~/remote.txt

ssh -l user user@server.com

cat ~/remote.txt >> ~/otherRemote.txt 

I've looked into the following possibilities:

I also considered placing local.txt at an openly accessible location, for example, with a public dropbox link. Then if cat could accept this as an input, the scp line wouldn't be necessary. But this would also require an additional step and wouldn't work in cases where local.txt cannot be made public.

Other references:

Using a variable's value as password for scp, ssh etc. instead of prompting for user input every time

https://superuser.com/questions/400714/how-to-remotely-write-to-a-file-using-ssh

Community
  • 1
  • 1
Bobby
  • 1,585
  • 3
  • 19
  • 42
  • Have you tried the `ssh-copy-id` command? It does this automatically for you. – Barmar Dec 22 '16 at 08:55
  • I would need to install that. Do you see any advantage over the solution below? Would it work more easily in a bash script? http://stackoverflow.com/questions/25655450/how-do-you-install-ssh-copy-id-on-a-mac – Bobby Dec 22 '16 at 09:10
  • `ssh-copy-id` does some additional stuff, like creating `~/.ssh` if necessary, and making sure that the permissions are correct. But other than that, it's essentially equivalent to the answer below. – Barmar Dec 22 '16 at 17:18

1 Answers1

1

You can redirect the content to the remote, and then use commands on the remote to do something with it. Like this:

ssh user@server.com 'cat >> otherRemote.txt' < ~/local.txt

The remote cat command will receive as its input the content of ~/local.txt, passed to the ssh command by input redirection.

Btw, as @Barmar pointed out, specifying the username with both -l user and user@ was also redundant in your example.

janos
  • 120,954
  • 29
  • 226
  • 236