expect --批量传递公钥

#!/binbash
! rpm -qa |grep expect &> /dev/null && yum -y install expect   #判断是否已下载expect 若没有则下载该服务
if [ ! -e /root/.ssh/id_rsa.pub ];then                 #判断公钥是否已存在 没有则生成 /usr/bin/expect <<EOF set timeout 10 spawn ssh-keygen expect "id_rsa" send "\n" expect "passphrase" send "\n" expect "agian" send "\n" expect eof EOF fi while read file;do                     #遍历文件(ip 密码) ip=`echo "$file" |awk '{print $1}'`      #截取出ip passwd=`echo "$file" | awk '{print $2}'`   #截取出密码 /usr/bin/expect <<EOF set timeout 5 spawn ssh-copy-id -i $ip expect { "yes" { send "yes\n"; exp_continue } "$ip" { send "$passwd\n" } } expect eof EOF done < /root/host_name                   #保存ip 密码的文件路径 可自定义

cat /root/host_name

192.168.198.150    xxxxxxx
192.168.198.151    xxxxxxx
192.168.198.152    xxxxxxx
192.168.198.153    xxxxxxx
......
posted @ 2020-11-02 20:09  心恩惠动  阅读(160)  评论(0)    收藏  举报