-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfor-ip.sh
54 lines (47 loc) · 1.8 KB
/
for-ip.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
!/bin/bash
# Cloudflare API 凭据
AUTH_EMAIL="xxxxx" # Cloudflare 账户邮箱
AUTH_KEY="xxxxx" # 你的 Cloudflare Global API Key
CF_ZONE_ID="xxxxx" # 你的 Cloudflare Zone ID
RECORD_NAME="cera.xxxx.xyz" # 你希望更新的 DNS 记录名称
PRIMARY_IP="xxx.xxx.xxx.xxx" # 主 IP 地址
BACKUP_IP="xxx.xxx.xxx.xxx" # 备用 IP 地址
PORT="48873" # 你希望检查的端口
# 检查IP端口是否开放
check_port() {
timeout 5 bash -c "cat < /dev/null > /dev/tcp/${1}/${PORT}" 2> /dev/null
return $?
}
# 更新Cloudflare DNS记录
update_dns() {
local ip=$1
# 获取DNS记录ID
local record_id=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones/${CF_ZONE_ID}/dns_records?type=A&name=${RECORD_NAME}" \
-H "X-Auth-Email: ${AUTH_EMAIL}" \
-H "X-Auth-Key: ${AUTH_KEY}" \
-H "Content-Type: application/json" | jq -r ".result[0].id")
# 检查是否成功获取DNS记录ID
if [[ $record_id == "null" ]]; then
echo "Failed to retrieve DNS record ID."
return
fi
# 更新DNS记录
local update_response=$(curl -s -X PUT "https://api.cloudflare.com/client/v4/zones/${CF_ZONE_ID}/dns_records/${record_id}" \
-H "X-Auth-Email: ${AUTH_EMAIL}" \
-H "X-Auth-Key: ${AUTH_KEY}" \
-H "Content-Type: application/json" \
--data '{"type":"A","name":"'"${RECORD_NAME}"'","content":"'"${ip}"'"}')
# 输出结果
echo "DNS record updated to IP: $ip."
}
# 主循环
while true; do
if check_port $PRIMARY_IP; then
# 如果主 IP 可用且当前 DNS 不是主 IP,则更新为主 IP
update_dns $PRIMARY_IP
else
# 如果主 IP 不可用,则切换到备用 IP
update_dns $BACKUP_IP
fi
sleep 10
done