免密认证脚本

#!/bin/bash

# 保证解析已经写入hosts,已经安装了expect并创建了密钥,保证本文件EOF字符后没有空格
passwd=1qaz@WSX
vm=(cache4 cache3 cache2 cache1 cache0 lvs2 lvs1 proxy2 proxy1 cc2 cc1 sql1 sql2 sql3 sql4 sql5 sql6 csp1 csp2 csp3 csp4 cts2 cts1 cts0 oss5 oss4 oss3 oss2 oss1 oss0)

# 初始化安装依赖
yum install -y epel-release
yum install -y expect

# 免密脚本
read -p "需要做免密么?请确保需要免密的机器已经配置完毕!(y or n)" flag
if [ $flag = "y" ]
then
for i in ${vm[@]}
do
expect <<-EOF 
set timeout 5
spawn ssh-copy-id $i
expect {  
"(yes/no)" {send "yes\n"; exp_continue}
"password:" {send "${passwd}\n"}
}
expect eof   
EOF
sleep 1
done
fi
unset flag

Q.E.D.


刚毕业走上社会被毒打的搬砖狗