作用:快速自动生成密钥,并批量推送到远程主机,适用于为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}