每日一shell(十一) 批量快速推送key到远程主机

前端之家收集整理的这篇文章主要介绍了每日一shell(十一) 批量快速推送key到远程主机前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

作用:快速自动生成密钥,并批量推送到远程主机,适用于为ansible环境批量推送key

#!/bin/bash

keypath=/root/.ssh
iplist=/root/ip.txt
[[ -e ${iplist} ]] && > ${iplist}
for i in `seq 81 91`
do
   echo -E "192.168.118.$i" >> ${iplist}
   ssh-keyscan 192.168.118.$i >> ${keypath}/known_hosts
done
cat ${iplist}
[ -d {keypath} ] || mkdir -p ${keypath}
rpm -q expect &> /dev/null || yum install expect -y
[[ -e ${keypath}/id_rsa.pub ]] || ssh-keygen -t rsa -f ${keypath}/id_rsa -P ""
password=u7wtiYTl0
while read ip;do
expect << EOF
set timeout 5
spawn ssh-copy-id $ip
expect {

“yes/no” { send "yes\n";exp_continue }
"password" { send "$password\n"  }

}
expect off
EOF      
done < ${iplist}
原文链接:https://www.f2er.com/bash/387354.html

猜你在找的Bash相关文章