Skip to content

用API手动修改CloudFlare的某个域名的A记录IP

原理很简单,就是通过邮箱+GlobalAPIKey逐步获得zone_id (对应yyy.zzz),再获得record_id (对应xxx.yyy.zzz),然后就可以通过curl post来直接修改xxx.yyy.zzz对应的A记录了。

参考

https://github.com/XIU2/CloudflareSpeedTest/issues/40

获得Global API Key

获得zone_id (yyy.zzz)

CF_EMAIL="[email protected]"
CF_GLOBAL_API_KEY="e59702XXXXXXXXXXXXXXXXXXXXXX"

curl -X GET "https://api.cloudflare.com/client/v4/zones" \
  -H "X-Auth-Email: $CF_EMAIL" \
  -H "X-Auth-Key: $CF_GLOBAL_API_KEY" \
  -H "Content-Type: application/json"

#当中找到你需要的域名对应的zone_id

获得对应的record_id (xxx.yyy.zzz)

CFZONE_ID='a7651XXXXXXXXXXXXXXXXX'

curl -X GET "https://api.cloudflare.com/client/v4/zones/$CFZONE_ID/dns_records?page=1&per_page=100&order=type&direction=asc" \
-H "X-Auth-Email: $CF_EMAIL" \
-H "X-Auth-Key: $CF_GLOBAL_API_KEY" \
-H "Content-Type: application/json" | vim  -

curl+post 来更新A记录IP值

CFDOMAIN_RECORD_ID='420e6XXXXXXXXXXXXXXXXXX'
CFDOMAIN_RECORD_NAME='cfbest.yyy.zzz'
CFDOMAIN_RECORD_NEW_IP='104.18.54.1'

curl -X PUT "https://api.cloudflare.com/client/v4/zones/$CFZONE_ID/dns_records/$CFDOMAIN_RECORD_ID" \
-H "X-Auth-Email: $CF_EMAIL" \
-H "X-Auth-Key: $CF_GLOBAL_API_KEY" \
-H "Content-Type: application/json" \
--data '{"type":"A","name":"'$CFDOMAIN_RECORD_NAME'","content":"'$CFDOMAIN_RECORD_NEW_IP'","ttl":1,"proxied":false}'

写了一个bash function

function update_cfbest_ip() {
  # update_cfbest_ip 199.198.197.196

  CFDOMAIN_RECORD_NEW_IP=$1

  CF_EMAIL="[email protected]"
  CF_GLOBAL_API_KEY="e5970XXXXXXXXXX"
  CFZONE_ID='a765XXXXXXXXXX'
  CFDOMAIN_RECORD_ID='420e6XXXXXXXXXX'
  CFDOMAIN_RECORD_NAME='cfbest.yyy.zzz'

  echo curl -X PUT "https://api.cloudflare.com/client/v4/zones/$CFZONE_ID/dns_records/$CFDOMAIN_RECORD_ID" \
    -H "X-Auth-Email: $CF_EMAIL" \
    -H "X-Auth-Key: $CF_GLOBAL_API_KEY" \
    -H "Content-Type: application/json" \
    --data '{"type":"A","name":"'$CFDOMAIN_RECORD_NAME'","content":"'$CFDOMAIN_RECORD_NEW_IP'","ttl":1,"proxied":false}'

  curl -X PUT "https://api.cloudflare.com/client/v4/zones/$CFZONE_ID/dns_records/$CFDOMAIN_RECORD_ID" \
    -H "X-Auth-Email: $CF_EMAIL" \
    -H "X-Auth-Key: $CF_GLOBAL_API_KEY" \
    -H "Content-Type: application/json" \
    --data '{"type":"A","name":"'$CFDOMAIN_RECORD_NAME'","content":"'$CFDOMAIN_RECORD_NEW_IP'","ttl":1,"proxied":false}'
}

Leave a Reply

Your email address will not be published.