expect 实现批量主机公钥推送
脚本案例
#!/bin/bash
#copy pub.rsa
#v1.0 itwangqiang 20202/12/29
pas=root
rpm -qa | grep expect &> /dev/null
if [ $? -eq 0 ];then
echo "installing........"
yum install -y expect &> /dev/null #安装 expect 命令
fi
if [ ! -f ~/.ssh/id_rsa ];then
ssh-keygen -P "" -f ~/.ssh/id_rsa &> /dev/null #创建密钥
fi
for i in {1..254} #循环推送公钥
do
{
ip=192.168.121.$i
ping -c1 $ip &> /dev/null
if [ $? -eq 0 ];then
echo "$ip" >> /root/euccessful.txt
/usr/bin/expect <<-EOF
spawn ssh-copy-id $ip
expect {
"yes/no" { send "yes\r"; exp_continue }
"password:" { send "$pass\r" }
}
expect eof
EOF
fi
}&
done
wait
echo "Finishing!!!!!!!!!!"