ansible批量推key

ansible批量推key

Scroll Down
#!/bin/bash
q=`cat /tmp/ip.txt`
#passwd_root=(你的各个密码)
passwd_root=(Cdh@AX123 ax_642135 ax_642135 nginx@AX123 Sentry@AX123 k8s@Ali123 Git@ax456 wiki@AX123)
>/tmp/ipok.txt
>/tmp/ipfalse.txt
for ip in $q
do
  for x in ${passwd_root[@]}
  do
      sshpass -p  "$x"  ssh $ip  -o StrictHostKeyChecking=no "lsattr /root/.ssh/authorized_keys" 2>/dev/null |awk '{print $1}' |grep i &>/dev/null
        if [ $? -eq 0 ];then
            #echo "我没有进来"
              sudo  sshpass -p "$x" sudo  ssh root@$ip chattr -i /root/.ssh/authorized_keys &>/dev/null
              [ $? -eq 0 ] && echo "$ip chattr ---ok"
              sudo  sshpass -p "$x" sudo ssh-copy-id -i /root/.ssh/id_rsa.pub $ip &>/dev/null
               if [ $? -eq 0 ];then
                   echo "$ip ---1 ok"
                   echo "$ip" >>/tmp/ipok.txt
                   sed -i "/$ip/d"  /tmp/ipfalse.txt
                   break
               else
                  # echo "$ip ---1 false"
                   echo "$ip" >>/tmp/ipfalse.txt
               fi
        else
               # echo “$x------2”                 
                sudo sshpass -p "$x" sudo ssh-copy-id -i /root/.ssh/id_rsa.pub $ip &>/dev/null
                  if [ $? -eq 0 ];then
                    echo "$ip ---2 ok"
                    echo "$ip" >>/tmp/ipok.txt
                    sed -i "/$ip/d"  /tmp/ipfalse.txt
                    break
                else
                    #echo "$ip ---2 false"
                    echo "$ip" >>/tmp/ipfalse.txt
                fi
        fi
   done
done