1、把各个服务服务器的ip 用户名,用户密码,写入如下文件中
vim hostname.txt
47.100.47.180 root ZHj1203110904!47.102.201.136 root 55555
2、运行下面的localtoremote.sh脚本。hostname.txt文件中的每一行,继而把公钥添加
vim localtoremote.sh
#!/bin/bash #by zhangjia#2019.07.29add_pub() { yum install -y sshpass [ -f ~/.ssh/id_rsa ] || ssh-keygen -t rsa -f ~/.ssh/id_rsa -P '' &> /dev/null cat hostname | while read ips users pwds; do sshpass -p $pwds ssh-copy-id -i ~/.ssh/id_rsa.pub ${users}@$ips -o StrictHostKeyChecking=no done}del_pub() { ansible all -u root -m lineinfile -a "path=/root/.ssh/authorized_keys regexp=\"zhj5551@zhj5551$\" state=absent"}#add_p