脚本功能介绍

  • 一键添加域名解析
  • 一键修改域名解析
  • 一键删除域名解析

申请 CloudFlare API

前往 https://dash.cloudflare.com/profile/api-tokens 登陆后,右上角选择个人资料,点击创建令牌

为了安全,用自定义令牌,没有用全局令牌。如你非要用全局令牌,请修改脚本 Authorization 全部替换为 X-Auth-Key

获取脚本

wget https://blog.wxlost.com/wp-content/uploads/2022/05/cloudFlare.sh && chmod +x cloudFlare.sh

编辑 cloudFlare.sh 内的参数(API与Email)后.运行脚本 添加/更新 域名的解析

bash cloudFlare.sh 域名

如需删除域名解析,使用参数1即可

bash cloudFlare.sh 域名 1

其他问题

2024年04月13日 更新,添加了新的IP4和6的区分与解析过程

官方API文档

脚本开源

#!/usr/bin/env bash
#https://blog.wxlost.com/cloudflare-domain.html
#https://developers.cloudflare.com/api/operations/dns-records-for-a-zone-list-dns-records
#bash xxx.sh domain 1  (不填写1 为自动替换IPV4模式,1为纯删除DNS记录,6为添加IPV6记录)

#clear
echo -e "https://blog.wxlost.com/cloudflare-domain.html"

#核心! CloudFlare
Authorization="你的秘钥-如果全局记得替换参数文本" #api
auth_email="你的账号"

record_name="$1"
record_del="$2"
zone_name="${record_name#*.}"

#IP解析
local_ipv4=$(curl -4 -s --max-time 5 api64.ipify.org)
#local_ipv4=$(curl -s ipv4.ip.sb) #备用IP4
#local_ipv4_asterisk=$(awk -F"." '{print $1"."$2".*.*"}' <<<"${local_ipv4}")
if [ "$record_del" = "6" ]; then
local_ipv6=$(curl -6 -s --max-time 5 api64.ipify.org) 
fi
#local_ipv6=$(curl -s ipv6.ip.sb) #备用IP6
#local_ipv4_asterisk=$(awk -F"." '{print $1"."$2".*.*"}' <<<"${local_ipv4}")
#local_ipv6_asterisk=$(awk -F":" '{print $1":"$2":"$3":*:*"}' <<<"${local_ipv6}")
#UA_Browser="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.87 Safari/537.36"
#local_isp4=$(curl -s -4 --max-time 10 --user-agent "${UA_Browser}" "https://api.ip.sb/geoip/${local_ipv4}" | cut -f1 -d"," | cut -f4 -d '"')
#local_isp6=$(curl -s -6 --max-time 10 --user-agent "${UA_Browser}" "https://api.ip.sb/geoip/${local_ipv6}" | cut -f1 -d"," | cut -f4 -d '"')

ip4=${local_ipv4}
ip6=${local_ipv6}

if [[ -z "$local_ipv4" ]]; then
    ip4="N/A"
fi
if [[ -z "$local_ipv6" ]]; then
    ip6="N/A"
fi
if [[ -z "$record_name" ]]; then
    echo -e "错误: 域名不能为空!!"
    exit 1
fi
if [[ -z "$record_del" ]]; then
    record_del="0"
fi
echo -e "一键CloudFlare api [v2024031601] 待操作域名: $1"
echo -e "本机IP4: $ip4 \n本机IP6: $ip6"

function int(){
#echo "----------" && echo -e "$record_name CloudFlare 核心参数读取中..."
#获取域ID
zone_identifier=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones?name=$zone_name" -H "X-Auth-Email: $auth_email" -H "Authorization: $Authorization" -H "Content-Type: application/json" | grep -Po '(?<="id":")[^"]*' | head -1 )
if [ "${zone_identifier}" = '' ]; then
    echo && echo -e "$record_name CloudFlare 域ID获取失败!" && exit 0;
fi
#默认为处理一条ipv4一条ipv6.超过的请自行在这里修改识别过程
#域名已经添加了几条数据
DNS_Num=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones/$zone_identifier/dns_records?name=$record_name" -H "X-Auth-Email: $auth_email" -H "Authorization: $Authorization" -H "Content-Type: application/json" | grep -Po '(?<="total_count":)[^,]*')
#获取域名ID
record_identifier=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones/$zone_identifier/dns_records?name=$record_name" -H "X-Auth-Email: $auth_email" -H "Authorization: $Authorization" -H "Content-Type: application/json" | grep -Po '(?<="id":")[^"]*')
#获取域名解析类型
#Record_type=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones/$zone_identifier/dns_records?name=$record_name" -H "X-Auth-Email: $auth_email" -H "Authorization: $Authorization" -H "Content-Type: application/json" | grep -Po '(?<="type":")[^"]*')
#获取域名IP
Domain_name_status=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones/$zone_identifier/dns_records?name=$record_name" -H "X-Auth-Email: $auth_email" -H "Authorization: $Authorization" -H "Content-Type: application/json"  | grep -Po '(?<="content":")[^"]*')

if [ "${DNS_Num}" == '0' ]; then
    echo "----------"
fi
if [ "${DNS_Num}" == '1' ]; then
    echo "----------" && echo -e "查询到当前域名已有 [$DNS_Num] 条DNS记录, IP: $Domain_name_status"

fi
if [ "${DNS_Num}" -gt '1' ]; then
    echo "----------" && echo -e "查询到当前域名已有 [$DNS_Num] 条DNS记录,分别为:\n$Domain_name_status"
#拆分为数组
Arr_record_identifier=($record_identifier)
# Arr_Record_type=($Record_type)
Arr_Domain_name_status=($Domain_name_status)
fi
}
function Domain_name_status(){
int
#拆分为数组
if [ "${record_del}" == '0' ]; then
    if [ "${ip4}" == "${Domain_name_status}" ]; then
        echo -e "域名DNS记录IP4 $Domain_name_status 与本机IP4: $ip4 一致,无需操作:)" && echo -e "如需强制删除: bash xxx.sh $record_name 1" && exit 0;
    else
        Domain_Letgo
    fi
else
    if [ "${DNS_Num}" == '0' ]; then
    echo -e "域名不存在DNS记录,自动转成添加模式!" && Domain_Letgo && exit 0;
    fi
    if [ "${DNS_Num}" == '1' ]; then
        Domain_delete1 && exit 0;
    else
        Domain_delete2 && exit 0;
    fi
fi
}
function Domain_Letgo(){
if [ "${DNS_Num}" == '0' ]; then
    if [ ${ip4} != "N/A" ]; then
        Domain_go4
    fi
    if [ ${ip6} != "N/A" ]; then
        Domain_go6
    fi
else
    if [ "${DNS_Num}" == '1' ]; then
        Domain_delete1 && DNS_Num='0' && record_identifier='' && Domain_Letgo
    else
        Domain_delete2 && DNS_Num='0' && record_identifier='' && Domain_Letgo
    fi
fi
}

function Domain_go4(){
if [ "${record_identifier}" = '' ]; then
    #echo -e "域名IPV4首次记录,开始新增DNS记录"
    Domain_add4
else
    #echo -e "域名IPV4存在记录,开始更新DNS记录"
    Domain_update4
fi
}
function Domain_go6(){
if [ "${record_identifier}" = '' ]; then
    #echo -e "域名IPV6首次记录,开始新增DNS记录"
    Domain_add6
else
    #echo -e "域名IPV6存在记录,开始更新DNS记录"
    Domain_update6
fi
}
function Domain_add4(){
result=$(curl -s -X POST "https://api.cloudflare.com/client/v4/zones/$zone_identifier/dns_records" \
     -H "X-Auth-Email: $auth_email" \
     -H "Authorization: $Authorization" \
     -H "Content-Type: application/json" \
     --data '{"type":"A","name":"'"$record_name"'","content":"'"$ip4"'","ttl":1,"priority":10,"proxied":false}')

if [[ $result == *"\"success\":false"* ]]; then
    message="API ADD FAILED. DUMPING RESULTS:\n$result"
    echo -e "$message"
else
    message="添加DNS记录IPV4: $ip4 完成!"
    echo -e "$message"
fi
}

function Domain_add6(){
result=$(curl -s -X POST "https://api.cloudflare.com/client/v4/zones/$zone_identifier/dns_records" \
     -H "X-Auth-Email: $auth_email" \
     -H "Authorization: $Authorization" \
     -H "Content-Type: application/json" \
     --data '{"type":"AAAA","name":"'"$record_name"'","content":"'"$ip6"'","ttl":1,"priority":10,"proxied":false}')

if [[ $result == *"\"success\":false"* ]]; then
    message="API ADD FAILED. DUMPING RESULTS:\n$result"
    echo -e "$message"
else
    message="添加DNS记录IPV6: $ip6 完成!"
    echo -e "$message"
fi
}
function Domain_update4(){
result=$(curl -s -X PUT "https://api.cloudflare.com/client/v4/zones/$zone_identifier/dns_records/$record_identifier" \
     -H "X-Auth-Email: $auth_email" \
     -H "Authorization: $Authorization" \
     -H "Content-Type: application/json" \
     --data '{"type":"A","name":"'"$record_name"'","content":"'"$ip4"'","ttl":1,"proxied":false}')

if [[ $result == *"\"success\":false"* ]]; then
    message="API UPDATE FAILED. DUMPING RESULTS:\n$result"
    echo -e "$message"
else
    message="更新DNS记录IPV4:$Domain_name_status 完成!"
    echo -e "$message"
fi
}
function Domain_update6(){
result=$(curl -s -X PUT "https://api.cloudflare.com/client/v4/zones/$zone_identifier/dns_records/$record_identifier" \
     -H "X-Auth-Email: $auth_email" \
     -H "Authorization: $Authorization" \
     -H "Content-Type: application/json" \
     --data '{"type":"AAAA","name":"'"$record_name"'","content":"'"$ip6"'","ttl":1,"proxied":false}')

if [[ $result == *"\"success\":false"* ]]; then
    message="API UPDATE FAILED. DUMPING RESULTS:\n$result"
    echo -e "$message"
else
    message="更新DNS记录IPV6:$Domain_name_status 完成!"
    echo -e "$message"
fi
}

function Domain_delete1(){
result=$(curl -s -X DELETE "https://api.cloudflare.com/client/v4/zones/$zone_identifier/dns_records/$record_identifier" \
     -H "X-Auth-Email: $auth_email" \
     -H "Authorization: $Authorization" \
     -H "Content-Type: application/json")

if [[ $result == *"\"success\":false"* ]]; then
    message="API DELETE FAILED. DUMPING RESULTS:\n$result"
    echo -e "$message"
    echo -e "删除DNS记录失败!"
else
    message="删除DNS: $Domain_name_status 记录完成!"
    echo -e "$message"
fi
}

function Domain_delete2(){
for ((i=1; i<=$DNS_Num; i++))
do
result=$(curl -s -X DELETE "https://api.cloudflare.com/client/v4/zones/$zone_identifier/dns_records/${Arr_record_identifier[i-1]}" \
     -H "X-Auth-Email: $auth_email" \
     -H "Authorization: $Authorization" \
     -H "Content-Type: application/json")

    if [[ $result == *"\"success\":false"* ]]; then
        message="API DELETE FAILED. DUMPING RESULTS:\n$result"
        echo -e "$message"
        echo -e "删除DNS记录失败!"
    else
        message="删除DNS: ${Arr_Domain_name_status[i-1]} 记录完成!"
        echo -e "$message" 
    fi
done
}
Domain_name_status
#Domain_Letgo