#!/bin/bash # Cloudflare DDNS 双栈自动更新 (IPv4+IPv6) | 飞牛任务计划专用 # 已填入你的 Token / 域名 | 强制开启橙云加速 | 修复IPv4获取失败 # ========== 你的配置(已自动填好) ========== API_TOKEN="你的APItoken" DOMAIN="你的域名" # ============================================ # 自动获取 Zone ID ZONE_ID=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones?name=$DOMAIN" \ -H "Authorization: Bearer $API_TOKEN" \ -H "Content-Type: application/json" | grep -o '"id":"[^"]*"' | head -1 | cut -d'"' -f4) if [ -z "$ZONE_ID" ]; then echo "❌ 获取 Zone ID 失败,请检查 Token 权限/域名状态" exit 1 fi # ====================== 更新 IPv4 (A 记录) ====================== update_ipv4() { RECORD_ID=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/dns_records?name=$DOMAIN&type=A" \ -H "Authorization: Bearer $API_TOKEN" | grep -o '"id":"[^"]*"' | head -1 | cut -d'"' -f4) if [ -z "$RECORD_ID" ]; then echo "⚠️ 未找到 IPv4(A) 记录,跳过"; return; fi # 修复:替换为国内稳定IP接口,解决获取失败 CURRENT_IP=$(curl -s --max-time 5 http://myip.ipip.net || curl -s --max-time 5 http://ip.3322.org) CURRENT_IP=$(echo "$CURRENT_IP" | grep -oE '[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+' | head -1) if [ -z "$CURRENT_IP" ]; then echo "⚠️ IPv4 获取失败,跳过"; return; fi CF_IP=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/dns_records/$RECORD_ID" \ -H "Authorization: Bearer $API_TOKEN" | grep -o '"content":"[^"]*"' | head -1 | cut -d'"' -f4) if [ "$CURRENT_IP" != "$CF_IP" ]; then RES=$(curl -s -X PUT "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/dns_records/$RECORD_ID" \ -H "Authorization: Bearer $API_TOKEN" \ -d "{\"type\":\"A\",\"name\":\"$DOMAIN\",\"content\":\"$CURRENT_IP\",\"proxied\":true,\"ttl\":1}") if echo "$RES" | grep -q '"success":true'; then echo "✅ IPv4 更新成功:$CURRENT_IP" else echo "❌ IPv4 更新失败" fi else echo "ℹ️ IPv4 无变化" fi } # ====================== 更新 IPv6 (AAAA 记录) ====================== update_ipv6() { RECORD_ID=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/dns_records?name=$DOMAIN&type=AAAA" \ -H "Authorization: Bearer $API_TOKEN" | grep -o '"id":"[^"]*"' | head -1 | cut -d'"' -f4) if [ -z "$RECORD_ID" ]; then echo "⚠️ 未找到 IPv6(AAAA) 记录,跳过"; return; fi CURRENT_IP=$(curl -s --max-time 5 https://api64.ipify.org) if [ -z "$CURRENT_IP" ]; then echo "⚠️ IPv6 获取失败,跳过"; return; fi CF_IP=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/dns_records/$RECORD_ID" \ -H "Authorization: Bearer $API_TOKEN" | grep -o '"content":"[^"]*"' | head -1 | cut -d'"' -f4) if [ "$CURRENT_IP" != "$CF_IP" ]; then RES=$(curl -s -X PUT "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/dns_records/$RECORD_ID" \ -H "Authorization: Bearer $API_TOKEN" \ -d "{\"type\":\"AAAA\",\"name\":\"$DOMAIN\",\"content\":\"$CURRENT_IP\",\"proxied\":true,\"ttl\":1}") if echo "$RES" | grep -q '"success":true'; then echo "✅ IPv6 更新成功:$CURRENT_IP" else echo "❌ IPv6 更新失败" fi else echo "ℹ️ IPv6 无变化" fi } # 执行更新 echo "===== $(date '+%Y-%m-%d %H:%M:%S') =====" update_ipv4 update_ipv6 echo "========================================" exit 0
以上是任务计划脚本 可以替代实现DDNS的功能 大家可以试下