yum install -y sshpass
#! /bin/bash
#
# 设置日志输出文件
#exec 1>> ssh_keygen.log 2>&1
# 初始化变量
clear
root_pass=root
hosts_info=`cat ./hosts_info.conf`
hosts_ip=`echo "$hosts_info" | awk '{print $1}'`
if [ -e ~/.ssh/id_rsa ];then
rm -f ~/.ssh/id_rsa
fi
# 2、管理主机针对创建密钥对
#echo "" | ssh-keygen -t rsa
echo "y\n" | ssh-keygen -t rsa -q -N "" -f ~/.ssh/id_rsa
pub_key=`cat ~/.ssh/id_rsa.pub`
# 3、利用ssh非免密在所有需要管理主机上创建用户、设置密码
# 4、实现拷贝管理主机的公钥内容到对端主机
echo "$hosts_ip" | while read host_ip;do
echo "______________________SEND To $host_ip rsa______________________________"
sshpass -p$root_pass ssh-copy-id -o StrictHostKeyChecking=no root@$host_ip
# ssh -o StrictHostKeyChecking=no root@$node_host 'df -h'
host_name=`echo "$hosts_info" | grep $host_ip | awk '{print $2}'`
echo "设置主机名称为$host_name"
ssh -o StrictHostKeyChecking=no root@$host_ip "hostnamectl set-hostname $host_name" < /dev/null
echo "修改/etc/hosts"
scp hosts_info.conf root@$host_ip:/tmp < /dev/null
ssh -o StrictHostKeyChecking=no root@$host_ip "grep -wvf /etc/hosts /tmp/hosts_info.conf >> /etc/hosts" < /dev/null
ssh -o StrictHostKeyChecking=no root@$host_ip "cat /etc/hosts" < /dev/null
done
hosts_conf
192.168.56.11 master1
192.168.56.12 master2
192.168.56.13 master3
192.168.56.14 worker1
192.168.56.15 worker2