#!/bin/bash
start_time=`date +%s`
Concurrency='10'
HostUser='root'
HostPass='1'
Ip_File='/opt/ip_list.txt'
Key_Dir='/root/.ssh'
Pri_Key='id_rsa'
Pub_Key='id_rsa.pub'
Access_Log='/tmp/sshcopykey_access.log'
Error_Log='/tmp/sshcopykey_error.log'
Ssh_Copy_Log='/tmp/ssh-copy.log'
[ -f $Access_Log ] || touch $Access_Log
[ -f $Error_Log ] || touch $Error_Log
[ -f $Ssh_Copy_Log ] || touch $Ssh_Copy_Log
> $Access_Log
> $Error_Log
> $Ssh_Copy_Log
[ -e /tmp/fd1 ] || mkfifo /tmp/fd1
exec 3<>/tmp/fd1
rm -rf /tmp/fd1
for ((n=1;n<=${Concurrency};n++))
do
echo >&3
done
ssh-keygen -t rsa -f ${Key_Dir}/${Pri_Key} -P "" -q &&\
for ip in `cat ${Ip_File}`
do
read -u3
{
echo -e "\n\n================================= 主机: ${ip} =================================" >> $Ssh_Copy_Log
sshpass -p${HostPass} ssh-copy-id -i ${Key_Dir}/${Pub_Key} -o StrictHostKeyChecking=no ${HostUser}@${ip} &>> $Ssh_Copy_Log
if [ $? -eq 0 ]; then
echo "主机 ${ip} 秘钥分发成功!" | tee -a $Access_Log
else
echo "主机 ${ip} 秘钥分发失败!" | tee -a $Error_Log
fi
echo >&3
}&
done
wait
exec 3<&-
exec 3>&-
stop_time=`date +%s`
echo -e "\n所有秘钥分发完成。"
echo "耗时: `expr $stop_time - $start_time` 秒"