Automating ssh-copy-id

Solution 1:

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

Solution 2:

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

Solution 3:

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