#!/usr/bin/expect set timeout 60 set user root set password 123456 set key_path /home/test/.ssh/id_rsa.pub
for {set i 101} {$i < 201} {incr i} { spawn ssh-copy-id -i $key_path -p 22 $user@192.168.56.$i expect { #first connect, no public key in ~/.ssh/known_hosts "Are you sure you want to continue connecting (yes/no)?" { send "yes\r"; exp_continue } #already has public key in ~/.ssh/known_hosts "install the new keys" { send "\r"; exp_continue } "password:" { send "$password\r"; exp_continue } "Now try logging into the machine" { #it has authorized, donothing! ; exp_continue } } }