#!/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
文章作者: 鲜花的主人
版权声明: 本站所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 爱吃可爱多
Shell Shell
喜欢就支持一下吧
打赏
微信 微信
支付宝 支付宝