Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
because I'm using this script to set up the user/publickey. – devin Aug 30 '11 at 18:37
up vote 8 down vote accepted

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

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:"
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, 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
The only problem is The authenticity of host can't be established. Are you sure you want to continue connecting (yes/no)? for new servers. – Envek Oct 6 '15 at 11:54

This is a problem with ssh-copy-id; it also adds a key every time you run it. If you are automating the process, your authorized_keys file quickly gets cluttered with duplicate keys. Here is a Python program that avoids both problems. It runs from the control server and puts the keys from one remote server into another remote server.

import subprocess
def Remote(cmd,IP):
    cmd = '''ssh root@%s '''%(IP)+cmd
    lines = subprocess.check_output(cmd.split())
    return '\n'.join(lines)
source = '123.456.78.90'
target = '239.234.654.123'
getkey = 'cat /root/.ssh/'
getauth = 'cat /root/.ssh/authorized_keys'
sourcekey = Remote(getkey, source).replace('\n','').strip()
authkeys = Remote(getauth, target).replace('\n','').strip()
if sourcekey not in authkeys: 
    keycmd=''' echo "%s" >>/root/.ssh/authorized_keys; 
    chmod 600 /root/.ssh/authorized_keys '''%(sourcekey) # A compound shell statement
    print 'Installed key', Remote(keycmd,target)
else: print 'Does not need key'
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
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

functionally the same question also asked at

there's a perl module, but it does not seem to be maintained much :

as mentioned in other answers, sshpass is likely the easiest solution

share|improve this answer

Your Answer


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.