vim aa.sh
#!/bin/bash
yum -y install expect
if [ -f /root/.ssh/id_rsa ]||[ -f id_rsa.pub ];then #判断是否有公密钥
continue
else #否则生产公密钥
/usr/bin/expect <<EOF
spawn ssh-keygen -t rsa
expect ":" {send "\n;"}
expect ":" {send "\n;"}
expect ":" {send "\n;"}
expect eof
EOF
fi
read -ep "输入需要免密的IP,可以输入多个,用空格隔开:" IP
for i in $IP #循环你免密的IP
do
/usr/bin/expect <<EOF
spawn ssh-copy-id root@$i
expect "yes/no" {send "yes\n;exp_continue"}
expect "password" {send "123456\n;"}
expect eof
EOF
done
sh aa.sh
注意用空格隔开