(1)安装expect-5.42.1-1.x86_64.rpm,可以用 yum -y install 安装最新版本的 expect

(2)
#!/bin/bash


set timeout 30
for i in `cat ./serverlist.ini |grep "="|awk -F= '{print $2}'`
do
        echo $i
expect<   spawn ssh root@$i ""
   expect "Are you sure you want to continue connecting (yes/no)? "
   send "yes\n"
expect eof
exit
END
done





(3)
#!/bin/bash


set timeout 30
expectexpect "Enter file in which to save the key (/root/.ssh/id_rsa): "
send "\n"
expect "Overwrite (y/n)? "
send "\n"
expect eof
exit
END


for i in `cat ./serverlist.ini |grep "="|awk -F= '{print $2}'`
do
#       echo $i
expect<#   spawn scp /root/.ssh/id_rsa.pub [email protected]:.ssh/id_rsa.pub
#   expect "password: "
#   send "km@123\n"


   spawn ssh root@$i "mkdir /root/.ssh/"
   expect "password: "
   send "123456789\n"
expect eof
exit
END
expect<   spawn scp /root/.ssh/id_rsa.pub root@$i:/root/.ssh/id_rsa.pub
   expect "password: "
   send "123456789\n"
expect eof
exit
END
expect<   spawn ssh root@$i "touch /root/.ssh/authorized_keys"
   expect "password: "
   send "123456789\n"
expect eof
exit
END
expect<   spawn ssh root@$i "cat /root/.ssh/id_rsa.pub >> /root/.ssh/authorized_keys"
   expect "password: "
   send "123456789\n"
expect eof
exit
END
#   #scp *.sh $i:/root/
done




#set timeout 30
#spawn ssh [email protected]
#expect "password: "
#send "df@123\n"




#expect {
#    "(yes/no)?" {
#       send "yes\n"
#
#       }
#   "pwd:"{
#       ...
#       }
#}


#expect{
#       "yes/no" {send "yes\r"; exp_continue}
#       "pwd:{send"abc\n"}"
#}
11-10 13:03