0

I normally use scp like so:

myUser@myMachine:~/myapp$ scp devops@myserver.org:/path/to/bin/*.derp .
devops@myserver.org'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!

1 Answers1

2

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

$ scp -i ~/.ssh/id_rsa devops@myserver.org:/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.

Pradeep Pati
  • 5,779
  • 3
  • 29
  • 43
  • Thanks @galuano1 (+1) - what would the file look like at `path/to/my/private/key`, and how do I generate it? Thanks again! –  Apr 24 '13 at 02:12