#!/usr/bin/expect
set user root
set passwd password
set ips {192.168.6.64 192.168.6.47 192.168.6.161}
#################连接VM###################
foreach localIp ${ips} {
foreach ip ${ips} {
spawn ssh ${user}@${ip}
expect {
"*yes/no" { send "yes\r"; exp_continue}
"*password:" { send "${passwd}\r"}
}
expect eof
send "yum install expect -y\r"
send "cd ~\r"
send "mkdir .ssh\r"
send "cd .ssh\r"
send "touch authorized_keys\r"
expect {
"Overwrite*" {send "n\r"; exp_continue}
}
expect eof
send "chmod 644 authorized_keys\r"
send "ssh-keygen -t rsa\r"
expect {
"Enter*" {send "\r"; exp_continue}
"Overwrite*" {send "n\r"; exp_continue}
}
expect eof
send "cat id_rsa.pub | ssh ${user}@${localIp} 'cat - >> ~/.ssh/authorized_keys'\r"
#send "scp id_rsa.pub ${user}@${localIp}:~/.ssh/id_rsa.pub.${ip}\r"
expect {
"*yes/no" { send "yes\r"; exp_continue}
"*password:" { send "${passwd}\r"}
}
expect eof
send "exit\r"
send "ssh ${user}@${localIp}"
send "exit\r"
}
}