#!/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 "} expect "assword" {send "${client_pwd} "} expect "#" {send "rm -rf ~/.ssh/* "} expect "#" {send "ssh-keygen -t rsa -P '' -f ~/.ssh/id_rsa -q "} expect "#" {send "ssh-copy-id ${server} "} expect "yes/no" {send "yes "} expect "assword" {send "${server_pwd} "} expect "#" {send "exit "} expect "#" {send "exit "} EOF done for i in ${!vms[@]}; do sleep 1 expect <<EOF spawn ssh-copy-id ${vms[$i]} expect "assword" {send "${client_pwd} "} expect "#" {send "echo "} EOF done