需要提前修改好对应的password
#!/usr/bin/bash
#主机批量推送ssh公钥
#by racon 2022-04-21
>ip.txt
password=1
rpm -q expect &> /dev/null
if [ $? -ne 0 ];then
yum -y install expect
fi
if [ ! -f ~/.ssh/id_rsa ];then
ssh-keygen -P "" -f ~/.ssh/id_rsa
fi
for i in {3..254}
do
{
ip=192.168.220.$i
ping -c1 -W1 $ip &> /dev/null
if [ $? -eq 0 ];then
echo "$ip" >> ip.txt
/usr/bin/expect <<-EOF
set timeout 10
spawn ssh-copy-id $ip
expect {
"yes/no" { send "yesr"; exp_continue }
"password:" { send "$passwordr" }
}
expect eof
EOF
fi
}&
done
wait
echo "finished"



