Skip to content

Instantly share code, notes, and snippets.

@amanjuman
Created March 26, 2023 18:29
Show Gist options
  • Save amanjuman/178b8bf75459cb0cabadf0068e083b7b to your computer and use it in GitHub Desktop.
Save amanjuman/178b8bf75459cb0cabadf0068e083b7b to your computer and use it in GitHub Desktop.
Export all CloudFlare DNS Zone IDs and Domain names in a CSV
#!/bin/bash

# Set CloudFlare API credentials
CF_API_EMAIL="YOUR_CLOUDFLARE_EMAIL"
CF_API_KEY="YOUR_CLOUDFLARE_API_KEY"

# Set output file name
OUTPUT_FILE="cloudflare_dns_zones.csv"

# Get all DNS zones for the account
DNS_ZONES=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones" \
     -H "X-Auth-Email: ${CF_API_EMAIL}" \
     -H "X-Auth-Key: ${CF_API_KEY}" \
     -H "Content-Type: application/json")

# Extract zone ID and domain name and write to output file
echo "Zone ID,Domain" > "${OUTPUT_FILE}"
for row in $(echo "${DNS_ZONES}" | jq -r '.result[] | @base64'); do
    zone_id=$(echo ${row} | base64 --decode | jq -r '.id')
    domain=$(echo ${row} | base64 --decode | jq -r '.name')
    echo "${zone_id},${domain}" >> "${OUTPUT_FILE}"
done

echo "All DNS zones have been exported to ${OUTPUT_FILE}."

Make sure you have jq installed. To use this script, replace the YOUR_CLOUDFLARE_EMAIL and YOUR_CLOUDFLARE_API_KEY with your actual API credentials, and specify the output file name in the OUTPUT_FILE variable.

Save the script in a file with a .sh extension (e.g. export_cloudflare_dns_zones.sh), make it executable (chmod +x export_cloudflare_dns_zones.sh), and run it using ./export_cloudflare_dns_zones.sh.

The script will create a CSV file with the zone ID and domain name for each DNS zone in your CloudFlare account. The file will be saved in the same directory as the script.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment