Skip to content

Instantly share code, notes, and snippets.

@mihaitodor
Created February 12, 2019 21:49
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mihaitodor/155b1c6ddafb8c0db475078bfb72cec2 to your computer and use it in GitHub Desktop.
Save mihaitodor/155b1c6ddafb8c0db475078bfb72cec2 to your computer and use it in GitHub Desktop.
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