ipprobe/choose.sh
2024-01-13 19:47:35 +08:00

110 lines
3.8 KiB
Bash
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
# 以下变量需要填写
API_KEY="your_cloudflare_api_key"
EMAIL="your_email@example.com"
ZONE_ID="your_zone_id"
DOMAIN="your_domain.com"
SUBDOMAIN="sub.your_domain.com"
TTL=300
# 克隆git仓库
git clone https://git.18g.me/feiwujia/iplist || { echo "Git clone failed"; exit 1; }
# 进入iplist目录
cd iplist || { echo "Change directory failed"; exit 1; }
# 创建result文件夹
mkdir -p result || { echo "Create result folder failed"; exit 1; }
# 创建use文件夹
mkdir -p use || { echo "Create use folder failed"; exit 1; }
# 计算文件的行数
total_lines=$(cat result/*.csv | wc -l)
# 遍历iplist目录中的所有txt文件
for file in *.txt
do
# 对每个txt文件执行scan_linux_amd64命令输出结果到result文件夹中的csv文件
./scan_linux_amd64 -c 2000 -t 5 -o result/result_${file%.txt}.csv $file || { echo "scan program failed"; exit 1; }
# 从csv文件中读取每一行并显示进度
cat "result/result_${file%.txt}.csv" | pv -l -s $total_lines | while IFS= read -r line
do
# 将每一行分割成多个字段
IFS=',' read -ra fields <<< "$line"
# 遍历每个字段
for field in "${fields[@]}"
do
# 去除字段两边的空格
field=$(echo $field | xargs)
# 检查字段是否为有效的IPv4地址
if [[ $field =~ ^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
# 使用curl命令测试ip地址
status_code=$(curl --resolve cloudflare.com:443:$field --insecure https://cloudflare.com/cdn-cgi/trace -o /dev/null -w '%{http_code}' -s)
# 如果状态码为200将ip地址保存到use.txt文件中
if [ "$status_code" -eq 200 ]
then
echo "IP $field is available"
echo $field >> use/use.txt
else
echo "IP $field is not available"
fi
else
# 如果字段不是有效的IPv4地址跳出循环
break
fi
done
done
done
# 从use/use.txt文件中读取IP地址并使用Cloudflare API更新DNS记录
# 使用Cloudflare API获取子域名的所有A记录
records=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones/${ZONE_ID}/dns_records?type=A&name=${SUBDOMAIN}" \
-H "X-Auth-Email: ${EMAIL}" \
-H "X-Auth-Key: ${API_KEY}" \
-H "Content-Type: application/json")
# 从返回值中提取出所有A记录的ID
record_ids=$(echo "$records" | jq -r '.result[].id')
# 遍历所有A记录的ID并使用Cloudflare API删除DNS记录
for record_id in $record_ids
do
response=$(curl -s -X DELETE "https://api.cloudflare.com/client/v4/zones/${ZONE_ID}/dns_records/${record_id}" \
-H "X-Auth-Email: ${EMAIL}" \
-H "X-Auth-Key: ${API_KEY}" \
-H "Content-Type: application/json")
if echo "$response" | grep -q '"success":true'; then
echo "DNS record with ID $record_id deleted successfully"
else
echo "Failed to delete DNS record with ID $record_id"
fi
done
# 从use/use.txt文件中读取IP地址并使用Cloudflare API添加DNS记录
while IFS= read -r ip
do
# 使用Cloudflare API添加DNS记录
response=$(curl -s -X POST "https://api.cloudflare.com/client/v4/zones/${ZONE_ID}/dns_records" \
-H "X-Auth-Email: ${EMAIL}" \
-H "X-Auth-Key: ${API_KEY}" \
-H "Content-Type: application/json" \
--data '{"type":"A","name":"'${SUBDOMAIN}'","content":"'${ip}'","ttl":'${TTL}',"proxied":false}')
# 检查返回值中的success字段是否为true
if echo "$response" | grep -q '"success":true'; then
echo "DNS record for IP $ip added successfully"
else
echo "Failed to add DNS record for IP $ip"
fi
done < use/use.txt
# 删除iplist文件夹
cd .. && rm -rf iplist || { echo "Remove iplist folder failed"; exit 1; }