0

I normally use scp like so:

myUser@myMachine:~/myapp$ scp [email protected]:/path/to/bin/*.derp .
[email protected]'s password: ********
herp1.derp                                100%  732     0.7KB/s   00:00    
herp2.derp                                100%  215     0.2KB/s   00:00    
herp3.derp                                100%  682     0.7KB/s   00:00    
myUser@myMachine:~/myapp$

I now want to write a Bash script that, among other things, does this for me, but where the password is stored in the script, and the script doesn't query the user to enter it:

sh dostuff.sh

Just running that should scp all the *.derp files to the user's local directory.

So I ask: how can I supply the password to scp from inside my script? Thanks in advance!

2
  • Thanks but that question is actually about rsync not scp (it's improperly titled) Commented Apr 24, 2013 at 2:10
  • No it's not. The question never mentions rsync. Commented Apr 24, 2013 at 2:12

1 Answer 1

2

Instead of hardcoding password in a shell script, use SSH keys, its easier and secure.

$ scp -i ~/.ssh/id_rsa [email protected]:/path/to/bin/*.derp .

assuming your private key is at ~/.ssh/id_rsa

To generate a public / private key pair :

$ ssh-keygen -t rsa

The above will generate 2 files, ~/.ssh/id_rsa (private key) and ~/.ssh/id_rsa.pub (public key)

To setup the SSH keys for usage (one time task) : Copy the contents of ~/.ssh/id_rsa.pub and paste in a new line of ~devops/.ssh/authorized_keys in a new line in myserver.org server. If ~devops/.ssh/authorized_keys doesn't exist, feel free to create it.

A lucid how-to guide is available here.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks @galuano1 (+1) - what would the file look like at path/to/my/private/key, and how do I generate it? Thanks again!