ssh-copy-id automation with a list of hosts

Here’s another version of my ssh-copy-id script this time using a text file containing a list of hosts. The hosts file should contain a single host per line.

#!/bin/bash

export SSH_USER="user"
read -s PASSWORD
export PASSWORD


while read HOST; do
            export HOST;
            expect -c '
            set SSH_USER $env(SSH_USER)
            set HOST $env(HOST)
            set PASSWORD $env(PASSWORD)
            spawn ssh-copy-id $SSH_USER@$HOST
            expect {
                        "continue" {
                                    send "yes\n";
                                    exp_continue
                        }
                        "assword:" {
                                    send "$PASSWORD\n";
                        }
            }
            expect eof'
            echo "Done $HOST"
done < "$1"

Execute the script and pass the path to the text file as a parameter. i.e.

./auto_ssh.sh /path/to/host/list.txt;

Leave a Reply