Skip to content

Instantly share code, notes, and snippets.

@UZziell
Created January 26, 2023 08:26
Show Gist options
  • Save UZziell/0b598616864830d4f48d9267ee19547b to your computer and use it in GitHub Desktop.
Save UZziell/0b598616864830d4f48d9267ee19547b to your computer and use it in GitHub Desktop.
Add an A record to given cloudflare domains using API
#!/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
@UZziell
Copy link
Author

UZziell commented Jan 26, 2023

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