基于sshpass批量实现主机间的key验证脚本

脚本1:for实现(不需要ip清单文件)

#!/bin/sh
IPLIST="
10.0.0.58
10.0.0.68"
COLOR="echo -e \E[1;32m"
COLOR1="echo -e \E[1;31m"
END="\E[0m"
PASS=123456


#判断软件是否安装,如未安装则安装
rpm -q sshpass &> /dev/null || yum -y install sshpass


#判断密钥文件是否存在,如不存在则生成
[ -f /root/.ssh/id_rsa ] || ssh-keygen -f /root/.ssh/id_rsa -P ''


#基于Key认证
for IP in $IPLIST;do
  sshpass -p$PASS ssh-copy-id -o StrictHostKeyChecking=no $IP && ${COLOR}${IP}基于key认证配置成功!${END} || ${COLOR1}${IP}基于key认证配置失败!${END}
done

脚本2:while实现(需要ip清单文件)

[root@centos8 ~]# cat hosts.list
10.0.0.18
10.0.0.28

[root@centos8 ~]# cat push_ssh_key.sh
#!/bin/sh

#判断软件是否安装,如未安装则安装
rpm -q sshpass &> /dev/null || yum -y install sshpass

#判断密钥文件是否存在,如不存在则创建
[ -f /root/.ssh/id_rsa ] || ssh-keygen -f /root/.ssh/id_rsa -P ''

#定义变量
export SSHPASS=123456

while read IP;do
  sshpass -e ssh-copy-id -o StrictHostKeyChecking=no $IP
done < hosts.list

原文地址:https://www.cnblogs.com/nj-duzi/p/13831663.html