CloudFlare API - list zone records

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


#/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"'

As result fine, readable list
# ./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
...

Comments

Popular posts from this blog