Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I have some arbitrary number of servers with the same user/pass combination. I want to write a script (that I call once) so that

ssh-copy-id user@myserver

is called for each server. Since they all have the same user/pass this should be easy but ssh-copy-id wants me to type the password in separately each time which defeats the purpose of my script. There is no option for putting in a password, ie ssh-copy-id -p mypassword user@myserver.

How can I write a script that automatically fills in the password field when ssh-copy-id asks for it?

share|improve this question
    
why do you use user/pass identification instead of user/publickey identification? –  kagali-san Aug 30 '11 at 18:20
3  
because I'm using this script to set up the user/publickey. –  devin Aug 30 '11 at 18:37

3 Answers 3

You can use expect to listen for the password prompt and send your password:

#!/usr/bin/expect -f
spawn ssh-copy-id $argv
expect "password:"
send "YOUR_PASSWORD\n"
expect eof

Save the script, make it executable, and call it like: ./login.expect user@myserver

share|improve this answer
    
Do you need a newer version of bash to use spawn? For reasons I can't control I'm stuck with bash v3.2. –  devin Aug 30 '11 at 20:38
    
Bash version shouldn't matter. I tested with expect 5.44.1.15, but I've used similar with older versions of expect. Are you having trouble using the script? –  MonkeeSage Aug 30 '11 at 21:20
    
spawn: command not found –  devin Aug 31 '11 at 12:02
    
spawn is an expect keyword (see expect(1) manual). Sounds like the script is being interpreted as shell rather than expect. Do you have expect installed? What happens if you run expect directly: expect -f login.expect user@myserver –  MonkeeSage Aug 31 '11 at 13:51

Take a look at sshpass. Place your password in a text file and do something like this:

$ sshpass -f password.txt ssh-copy-id user@yourserver
share|improve this answer

One of the parallel SSH tools (clusterssh, mssh, pssh) may be appropriate for you.

For instance, use cssh to log into all the machines and append the key yourself.

share|improve this answer
1  
I already have a set of custom tools for doing everything I need, except for copying the key that is. –  devin Aug 30 '11 at 18:36
    
Exactly… so use this one tool for doing the one task that's missing. Though if this is going to be an ongoing thing, the script that MonkeeSage posted (adapted to read password from stdin and work on multiple servers) would probably be your best bet. –  MikeyB Aug 30 '11 at 22:57

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.