#!/bin/bash
#======================================================================
# Author: 开源
# Description: ping通则推送秘钥
# Date: 2024-12-31
# Version: 2.0
# Notes: 啥也不用改,直接用
# ======================================================================
# 检查sshpass是否安装
if ! command -v sshpass &> /dev/null; then
echo "没有安装sshpass,正在安装..."
sudo yum install -y epel-release
sudo yum install -y sshpass
if [ $? -ne 0 ]; then
echo "未安装sshpass。安装sshpass失败。请手动安装"
exit 1
else
echo "sshpass安装成功。"
fi
fi
# 定义一个函数来ping IP并推送密钥
ping_ip() {
local ip=$1
ping -c 1 $ip &> /dev/null
if [ $? -eq 0 ]; then
# 执行ssh-copy-id操作
sshpass -p 'oldboy123.com' ssh-copy-id -o StrictHostKeyChecking=no $ip &> /dev/null
if [ $? -eq 0 ]; then
echo "$ip: 连接正常,推送key成功!"
fi
fi
}
max_processes=50 # 同时运行的最大进程数
process_count=0
for i in {2..253}; do
ping_ip 10.0.0.$i &
process_count=$((process_count + 1))
if [ $process_count -eq $max_processes ]; then
wait -n # 等待任意一个进程完成
process_count=$((process_count - 1))
fi
done
# 等待所有剩余的后台进程完成
wait
暂无评论