Skip to content

Instantly share code, notes, and snippets.

@nicholasdille
Created June 25, 2019 15:45
Show Gist options
  • Save nicholasdille/00dd4194f51e72cf9cf4dfd54b989c1d to your computer and use it in GitHub Desktop.
Save nicholasdille/00dd4194f51e72cf9cf4dfd54b989c1d to your computer and use it in GitHub Desktop.
CloudFlare using bash
get_zone_id() {
local email=$1
local token=$2
local domain=$3
curl -sLfH "X-Auth-Email: ${email}" -H "X-Auth-Key: ${token}" https://api.cloudflare.com/client/v4/zones | jq --raw-output ".result[] | select(.name == \"${domain}\") | .id"
}
get_zone_records() {
local email=$1
local token=$2
local zone_id=$3
curl -sLfH "X-Auth-Email: ${email}" -H "X-Auth-Key: ${token}" https://api.cloudflare.com/client/v4/zones/${zone_id}/dns_records | jq --raw-output ".result[]"
}
check_zone_record() {
local email=$1
local token=$2
local zone_id=$3
local type=$4
local name=$5
local content=$6
if [[ "$(get_zone_records ${email} ${token} ${zone_id} | jq "select(.type == \"${type}\") | select(.name == \"${name}\") | select(.content == \"${content}\") | .id" | wc -l)" == "1" ]]; then
true
else
false
fi
}
new_zone_record() {
local email=$1
local token=$2
local zone_id=$3
local type=$4
local name=$5
local content=$6
curl -sLfX POST -H "X-Auth-Email: ${email}" -H "X-Auth-Key: ${token}" https://api.cloudflare.com/client/v4/zones/${zone_id}/dns_records -d "{\"type\":\"${type}\",\"name\":\"${name}\",\"content\":\"${content}\",\"proxied\":false}"
}
CLOUDFLARE_EMAIL=xxx
CLOUDFLARE_TOKEN=yyy
CLOUDFLARE_DOMAIN=go-nerd.de
CLOUDFLARE_DOMAIN_ID=$(get_zone_id ${CLOUDFLARE_EMAIL} ${CLOUDFLARE_TOKEN} ${CLOUDFLARE_DOMAIN})
if ! check_zone_record ${CLOUDFLARE_EMAIL} ${CLOUDFLARE_TOKEN} ${CLOUDFLARE_DOMAIN_ID} "A" "*.seat2.go-nerd.de" "1.2.3.4"; then
echo creating
new_zone_record ${CLOUDFLARE_EMAIL} ${CLOUDFLARE_TOKEN} ${CLOUDFLARE_DOMAIN_ID} "A" "*.seat2.go-nerd.de" "1.2.3.4"
fi
#!/bin/bash
set -o errexit
#
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment