#!/bin/bash
#push publickey to aap-servers
#将局域网内可以ping通的主机ip保存到一个文件
> ip_up.txt
for i in {2..10}
do
{
ip=10.1.1.$i
ping -c1 $ip &>/dev/null
[ $? -eq 0 ] && echo $ip |tee -a ip_up.txt
}& //并行放到后台运行
done
wait //等待进程结束
#将yunwei用户目录下的公钥推送到可以ping的服务器上
#1. 判断yunwei用户下有没有公钥
[ ! -f ~/.ssh/id_rsa.pub ] && ssh-keygen -P "" -f ~/.ssh/id_rsa
#2.将id_rsa.pub公钥远程推送到指定服务器
#2.1 判断expect程序是否安装,没安装则安装它
{
rpm -q expect
[ $? -ne 0 ] && sudo yum -y install expect
for remote_ip in `cat ip_up.txt`
do
/usr/bin/expect <<-EOF
spawn ssh-copy-id root@$remote_ip
expect {
"yes/no" { send "yes
";exp_continue }
"password:" { send "123
" }
}
expect eof
EOF
done
} &>/dev/null
#测试验证
test_ip=`tail -1 ip_up.txt`
ssh root@$test_ip hostname
test $? -eq 0 && echo "公钥推送成功。"