CloudFlare API - list zone records

Easy and fast way get all domains or records via CloudFlare API

Script

#/bin/bash

KEY=`cat /srv/cf_api/$1.ini | sed 's/ //g' | grep dns_cloudflare_api_key | awk -F "=" '{print $2}'`
EMAIL=`cat /srv/cf_api/$1.ini | sed 's/ //g' | grep dns_cloudflare_email | awk -F "=" '{print $2}'`

ZONE_OUT=`curl -s -X GET "https://api.cloudflare.com/client/v4/zones" \
  -H "Content-Type:application/json" \
  -H "X-Auth-Key:$KEY" \
  -H "X-Auth-Email:$EMAIL"`

ZONE_IDS=`echo "$ZONE_OUT" | jq -r '.result | .[] | with_entries(select(.key == ("id","name"))) | "\(.id)=\(.name)\r"'`

ZONE_ID=`echo "$ZONE_IDS" | sed 's/ //g' | grep $1 | awk -F "=" '{print $1}'`

DNS_REC=`curl -s -X GET "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/dns_records" \
  -H "Content-Type:application/json" \
  -H "X-Auth-Key:$KEY" \
  -H "X-Auth-Email:$EMAIL"`

echo "$DNS_REC" | jq -r '.result | .[] | with_entries(select(.key == ("id","type","ttl","name","content"))) | "\(.id) \(.type) \(.ttl) \(.name) \(.content)\r"'

Example output

# ./dns-api-cf.sh domain.com
...
31111111111111111111111111111111f A 1 domain.com 1.1.1.1
a11111111111111111111111111111115 A 1 client1.domain.com 2.2.2.2
...
Human Logic, AI Syntax... Note on Content: I'm a Systems Engineer, not a native English writer. To ensure my technical ideas are clear and accessible, I use AI tools to polish the grammar and style. The workflow is simple: I provide the logic, the code, and the real-world experience. The AI handles the "English-to-Human" translation layer. If you find a bug, that's on me. If you find a perfectly placed comma, that's probably the AI.

Comments

Popular posts from this blog

FreeRadius with Google Workspace LDAP

Fixing pssh (parallel-ssh) Problems on Debian 10 with Python 3.7