#!/usr/bin/expect
# Just need hosts.txt and replace $password with your password of an ansible user account
# Also id_rsa.pub for your private key you want to use.
set timeout 3
set rootdir /home/ansible
set hostz [file join $rootdir hosts.txt]
set fh [open $hostz r]
while {[gets $fh hosty] != -1} {
spawn scp .ssh/id_rsa.pub ansible@$hosty:
expect {
"Are you sure you want to continue connecting (yes/no)?" { send "yes\r"; exp_continue }
password: {send $password\r; exp_continue}
}
spawn ssh ansible@$hosty mkdir /home/ansible/.ssh && mv id_rsa.pub .ssh/authorized_keys
expect {
password: {send $password\r; exp_continue}
}
}
Comments