Last active
March 22, 2024 00:51
-
-
Save clarkritchie/f518f5f7a8fb889f9fa9f87e7574cbe4 to your computer and use it in GitHub Desktop.
Quick and Dirty CloudFlare stuff
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 | |
# | |
# this script gets your the cloudflare record id for a given hostname, type and zoneid | |
# | |
# https://www.puppeteers.net/blog/importing-dns-records-from-cloudflare-to-terraform/ | |
# | |
# Get your API key then: | |
# export AUTHKEY="xxx" | |
if [ $# -ne 4 ]; then | |
echo "usage: $(basename $0) [recordname] [type] [zone_id] [email]" | |
exit 1 | |
else | |
NAME=$1 | |
TYPE=$2 | |
ZONE_ID=$3 # get ZONEID this from cloudflare-zone-ids.sh | |
EMAIL=$4 | |
fi | |
BASURL="https://api.cloudflare.com/client/v4/zones" | |
QUERY="?name=${NAME}&type=${TYPE}" | |
curl -s -X GET "${BASURL}/${ZONE_ID}/dns_records${QUERY}" \ | |
-H "X-Auth-Email: ${EMAIL}" \ | |
-H "X-Auth-Key: ${CF_AUTH_KEY}" \ | |
-H "Content-Type: application/json" | jq '.result[] | .id' |
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
#!/bin/bash | |
# this script gets you the zone IDs | |
# | |
# https://www.puppeteers.net/blog/importing-dns-records-from-cloudflare-to-terraform/ | |
# | |
# Get your API key then: | |
# export AUTHKEY="xxx" | |
# | |
if [ $# -ne 0 ]; then | |
echo "usage: $(basename $0)" | |
exit 1 | |
fi | |
BASURL="https://api.cloudflare.com/client/v4/zones" | |
QUERY="?name=${NAME}" | |
EMAIL="me@cool.com" | |
zones=$(curl -s -X GET "${BASURL}" \ | |
-H "X-Auth-Email: ${EMAIL}" \ | |
-H "X-Auth-Key: ${AUTHKEY}" \ | |
-H "Content-Type: application/json" | jq '.result[].name' | tr -d '"') | |
for zone in $zones; do | |
echo -n "$zone: " | |
id=$(curl -s -X GET "${BASURL}?name=${zone}" \ | |
-H "X-Auth-Email: ${EMAIL}" \ | |
-H "X-Auth-Key: ${AUTHKEY}" \ | |
-H "Content-Type: application/json" | jq '.result[].id') | |
echo $id | |
done |
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
import requests, os, sys | |
def delete_dns_record(email, api_key, zone_id, record_id): | |
url = f"https://api.cloudflare.com/client/v4/zones/{zone_id}/dns_records/{record_id}" | |
headers = { | |
"X-Auth-Email": email, | |
"X-Auth-Key": api_key, | |
"Content-Type": "application/json" | |
} | |
response = requests.delete(url, headers=headers) | |
if response.status_code == 200: | |
print("DNS record deleted successfully.") | |
else: | |
print("Failed to delete DNS record. Status code:", response.status_code) | |
print("Response:", response.text) | |
def create_dns_record(email, api_key, zone_id, record_name, record_type, record_content, ttl=1, proxied=False): | |
url = f"https://api.cloudflare.com/client/v4/zones/{zone_id}/dns_records" | |
headers = { | |
"X-Auth-Email": email, | |
"X-Auth-Key": api_key, | |
"Content-Type": "application/json" | |
} | |
data = { | |
"type": record_type, | |
"name": record_name, | |
"content": record_content, | |
"ttl": ttl, | |
"proxied": proxied | |
} | |
response = requests.post(url, headers=headers, json=data) | |
if response.status_code == 200: | |
print("DNS record created successfully.") | |
else: | |
print("Failed to create DNS record. Status code:", response.status_code) | |
print("Response:", response.text) | |
if __name__ == "__main__": | |
api_key = os.getenv("CF_AUTH_KEY") | |
record_type = "CNAME" | |
record_id_to_delete = sys.argv[1].strip('\"\'') | |
record_name = sys.argv[2].strip('\"\'') | |
zone_id = sys.argv[3].strip('\"\'') | |
record_content = sys.argv[4].strip('\"\'') | |
email = sys.argv[5].strip('\"\'') | |
# Delete the existing DNS record | |
delete_dns_record(email, api_key, zone_id, record_id_to_delete) | |
# Create a new DNS record | |
print(f"Email: {email}, API key: {api_key}, Zone ID: {zone_id}, Record: {record_name}, Type: {record_type}, Content: {record_content}") | |
create_dns_record(email, api_key, zone_id, record_name, record_type, record_content) |
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 python3 | |
import requests, os, sys | |
def update_dns_record(email, api_key, zone_id, record_id, new_cname, domain): | |
url = f"https://api.cloudflare.com/client/v4/zones/{zone_id}/dns_records/{record_id}" | |
headers = { | |
"X-Auth-Email": email, | |
"X-Auth-Key": api_key, | |
"Content-Type": "application/json" | |
} | |
data = { | |
"type": "CNAME", | |
"name": domain, # Replace with your domain | |
"content": new_cname, | |
"ttl": 60, | |
"proxied": True # Set to True to proxy through Cloudflare | |
} | |
try: | |
response = requests.put(url, headers=headers, json=data) | |
response.raise_for_status() # Raise exception for 4xx or 5xx status codes | |
print("DNS record updated successfully.") | |
except requests.exceptions.HTTPError as err: | |
print("Failed to update DNS record. HTTP Error:", err) | |
print("Response:", response.text) | |
except Exception as e: | |
print("An unexpected error occurred:", e) | |
if __name__ == "__main__": | |
print(sys.argv) | |
if len(sys.argv) >= 2: | |
email = "foo@bar.com" | |
api_key = os.getenv("CF_AUTH_KEY") | |
zone_id = "1234" | |
record_id = sys.argv[1].strip('\"\'') | |
domain = sys.argv[2].strip('\"\'') | |
new_cname = "foo" | |
print(f"Updating Record ID={record_id}, Domain= {domain}, Zone={zone_id}") | |
update_dns_record(email, api_key, zone_id, record_id, new_cname, domain) | |
else: | |
print("Not enough arguments provided.") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment