shell脚本批量推送公钥实现免密码登录

@努力的小王  March 26, 2018

2018-03-26 14:45:56 星期一
实现基本思路,先检测局域网有多少台存活服务器,然后将密钥批量推送

#!/bin/env bash
##this is Public Key Push script
##author xw 2018.3.26
>passwd.txt
Passwd=p0-p0-p0-  ##定义所有被推送服务器统一的密码
rpm -q expect &>/dev/null
if [ $? -ne 0 ];then
        yum install -y expect
fi

if [ ! -f ~/.ssh/id_rsa ];then
       ssh-keygen -P "" -f ~/.ssh/id_rsa

fi

for z in {3..254}
do

       {
       ip=192.168.187.$z
       ping -c1 $ip &>/dev/null
       if [ $? -eq 0 ];then
               echo "$ip" |tee -a passwd.txt
               /usr/bin/expect <<-EOF
               set timeout 10
               spawn ssh-copy-id -i $ip
               expect {
                       "yes/no" { send "yes\r"; exp_continue }
                       "password:" { send "$Passwd\r" }
               
                }
               expect eof
EOF

        fi
        }&
done
wait
echo "please Wait a moment"

添加新评论