#!/bin/bash
function auto_ssh()
{
expectIsExists=`dpkg -l |grep expect`
if [ -z $expectIsExists ]
then
sudo apt-get update
sudo apt-get install expect -y
fi
[ ! -f /root/.ssh/id_rsa.pub ] && ssh-keygen -t rsa -P '' &>/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`
expect <<EOF
spawn ssh-copy-id -i /root/.ssh/id_rsa.pub $user_name@$ip
expect {
"yes/no" { send "yes\n";exp_continue}
"password" { send "$pass_word\n"}
}
expect eof
EOF
done < host_ip.txt
}
function hosts()
{
while read -r line;do
ip=`echo $line | cut -d " " -f1`
hostname=`echo $line | cut -d " " -f4`
echo "${ip} ${hostname}" >> /etc/hosts
done < host_ip.txt
}
auto_ssh
hosts
# cat host_ip.txt
# 172.21.80.1 root password node-001
# 172.21.80.2 root password node-002
# 172.21.80.3 root password node-003