ssh-copy-id automation with a list of hosts


Warning: count(): Parameter must be an array or an object that implements Countable in /home/fbsqlcom/public_html/youdidwhatwithtsql.com/wp-content/plugins/wp-codebox/main.php on line 31

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