批量分发公钥脚本
使用脚本的方式批量分发公钥信息!
一、定义需要管理的主机
[root@localhost ~]# cat ip.txt
192.168.1.2 root 123.com
192.168.1.3 root 123456
二、编写批量分发公钥的脚本
[root@localhost ~]# cat auto_ssh.sh
#!/usr/bin/env bash
set -e
[ -f /root/.ssh/id_rsa ] || ssh-keygen -t rsa -f /root/.ssh/id_rsa -P "" &>/dev/null
[ -f /usr/bin/sshpass ] || yum -y install sshpass &> /dev/null
while read line
do
ip=`echo $line | cut -d " " -f1`
user_name=`echo $line | cut -d " " -f2`
pass_word=`echo $line | cut -d " " -f3`
echo "=============host $ip pub-key start fenfa============="
sshpass -p ${pass_word} ssh-copy-id -i /root/.ssh/id_rsa.pub ${user_name}@${ip} -o "StrictHostKeyChecking=no" &> /dev/null
echo "host $ip fenfa success."
echo "=============host $ip pub-key fenfa end============="
echo ""
done < /root/ip.txt
*************** 当你发现自己的才华撑不起野心时,就请安静下来学习吧!***************