#检查expect是否已经安装
/usr/bin/which expect
if [ $? -ne 0 ]
then
yum -y install expect &>/dev/null
fi
#生成远程连接密钥
if [ ! -f ~/.ssh/id_rsa ]
then
ssh-keygen -P "" -f /root/.ssh/id_rsa &>/dev/null
fi
#传密钥
pass=1
for i in {2..254}
do
{
ip=192.168.74.$i
ping -c 1 -W 1 $ip &>/dev/null
(
if [ $? -eq 0 ]
then
/usr/bin/expect <<-EOF
spawn ssh-copy-id $ip
expect {
"yes/no" {send "yes\r";exp_continue}
"password:" {send "$pass\r"};
}
expect eof
EOF
fi
) &>/dev/null
} &
done
wait
#连接主机
ssh root@192.168.74.10