#!/usr/bin/expect
vms=(
192.168.0.170
192.168.0.171
192.168.0.172
192.168.0.173
192.168.0.174
)
server="192.168.0.175"
server_pwd="123456"
client_pwd="654321"
rm -rf ~/.ssh/*
ssh-keygen -t rsa -P '' -f ~/.ssh/id_rsa
for i in ${!vms[@]}; do
sleep 1
expect <<EOF
spawn ssh ${vms[$i]}
expect "yes/no" {send "yes\r"}
expect "assword" {send "${client_pwd}\r"}
expect "#" {send "rm -rf ~/.ssh/*\r"}
expect "#" {send "ssh-keygen -t rsa -P '' -f ~/.ssh/id_rsa -q\r"}
expect "#" {send "ssh-copy-id ${server}\r"}
expect "yes/no" {send "yes\r"}
expect "assword" {send "${server_pwd}\r"}
expect "#" {send "exit\r"}
expect "#" {send "exit\r"}
EOF
done
for i in ${!vms[@]}; do
sleep 1
expect <<EOF
spawn ssh-copy-id ${vms[$i]}
expect "assword" {send "${client_pwd}\r"}
expect "#" {send "echo\r"}
EOF
done