Created
January 26, 2023 08:26
-
-
Save UZziell/0b598616864830d4f48d9267ee19547b to your computer and use it in GitHub Desktop.
Add an A record to given cloudflare domains using API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
DOMAINS="example.com|example.info" | |
# Global API Key and email | |
EMAIL="userCF@email.com"; | |
KEY="XXX"; | |
# New Record details | |
SUB="subdomain" | |
TYPE="A"; | |
CONTENT="1.2.3.4"; | |
PROXIED="true"; | |
TTL="1"; | |
# Get all available zones | |
CF_ZONES=$(curl -X GET "https://api.cloudflare.com/client/v4/zones" \ | |
-H "X-Auth-Email: $EMAIL" \ | |
-H "X-Auth-Key: $KEY" \ | |
-H "Content-Type: application/json") | |
# Select zones mathing input domains and add A record | |
echo "$CF_ZONES" | jq -r ".result[] | select(.name | match(\"^($DOMAINS)$\")) | [.name, .id ] | join(\" \") " | \ | |
while IFS=' ' read -r col1 col2; do | |
echo ZONE: $col1 ID: $col2; | |
DOMAIN=$col1 | |
ZONE_ID=$col2 | |
curl -X POST "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/dns_records/" \ | |
-H "X-Auth-Email: $EMAIL" \ | |
-H "X-Auth-Key: $KEY" \ | |
-H "Content-Type: application/json" \ | |
--data '{"type":"'"$TYPE"'","name":"'"${SUB}.${DOMAIN}"'","content":"'"$CONTENT"'","proxied":'"$PROXIED"',"ttl":'"$TTL"'}' \ | |
| jq; | |
sleep 3 | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
source: Using the Cloudflare API to Manage DNS Records