Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Fetch Cloudflare log data for a given Ray ID
#!/bin/bash
# Fetch Cloudflare log data for a given Ray ID
# Please note that the Ray ID is the first part of `CF-RAY` (4a807f60ca5727a4-FRA -> 4a807f60ca5727a4)
CF_ZONE=${1}
RAY_ID=${2}
if ! host -N 0 "${CF_ZONE}" 2>&1 > /dev/null; then
echo "Invalid domain '${CF_ZONE}'"
exit 1
fi
if [[ ! ${RAY_ID} =~ ^[a-f0-9]{16}$ ]]; then
echo "Invalid Ray ID '${RAY_ID}'"
exit 1
fi
ZONE_ID=$(curl -s "https://api.cloudflare.com/client/v4/zones?name=${CF_ZONE}&match=any" -H "X-Auth-Email: ${CLOUDFLARE_EMAIL}" -H "X-Auth-Key: ${CLOUDFLARE_TOKEN}" -H "Content-Type: application/json" | jq -r '.result[0].id')
if [ -z "$ZONE_ID" ]; then
echo "Could not fetch the zone ID for '${CF_ZONE}'"
exit 1
fi
curl -H "X-Auth-Email: ${CLOUDFLARE_EMAIL}" -H "X-Auth-Key: ${CLOUDFLARE_TOKEN}" "https://api.cloudflare.com/client/v4/zones/${ZONE_ID}/logs/rayids/${RAY_ID}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.