Q:

Automating ssh-copy-id

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?

A:

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

A:

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

A:

quanta's answer is pretty good, but it requires you to put your password in a text file.
From the "sshpass" man page: 

If no option is given, sshpass reads the password from the standard
  input.

So, what you can do is to capture the password once during the script, store it in a variable, echo the password and pipe that to sshpass as an input. 
I do this all the time and it works fine. Example:

echo "Please insert the password used for ssh login on remote machine:"
read -r USERPASS
for TARGETIP in $@; do
  echo "$USERPASS" | sshpass ssh-copy-id -f -i $KEYLOCATION "$USER"@"$TARGETIP"
done

