Skip to content

Instantly share code, notes, and snippets.

@presswizards
Created January 15, 2025 05:28
Show Gist options
  • Select an option

  • Save presswizards/c6bdb7e0e74b90d294bbdd7844316df1 to your computer and use it in GitHub Desktop.

Select an option

Save presswizards/c6bdb7e0e74b90d294bbdd7844316df1 to your computer and use it in GitHub Desktop.
Cloudflare DNS IP Checker Shell Script
#!/bin/bash
# Cloudflare DNS IP checker shell script:
# This script checks for server IP assigned to DNS records so you can remove them and replace
# the A record with a CNAME that points to the server hostname instead making migrations easy!
# Cloudflare Account 1
CLOUDFLARE_EMAIL="[email protected]" # Replace with your Cloudflare email
CLOUDFLARE_API_KEY="<cloudflare global api key>" # Replace with your Cloudflare global API key
# Cloudflare Account 2 - To use, comment out above two lines, uncomment out below
#CLOUDFLARE_EMAIL="[email protected]" # Replace with your Cloudflare email
#CLOUDFLARE_API_KEY="cloudflare global api key>" # Replace with your Cloudflare global API key
SEARCH_IP="5.123.456.789" # The IP to search for
# Cloudflare API base URL
API_URL="https://api.cloudflare.com/client/v4"
# Search for DNS records matching the target IP
search_dns_records() {
local zone_id="$1"
local domain_name="$2"
# Get DNS records for the zone
dns_records=$(curl -s -X GET "$API_URL/zones/$zone_id/dns_records" \
-H "X-Auth-Email: $CLOUDFLARE_EMAIL" \
-H "X-Auth-Key: $CLOUDFLARE_API_KEY" \
-H "Content-Type: application/json")
# Check for errors in the API response
if [[ $(echo "$dns_records" | jq '.success') != "true" ]]; then
echo "Error fetching DNS records for $domain_name: $(echo "$dns_records" | jq '.errors')"
return
fi
echo "Checking domain: $domain_name "
# Search for the IP in DNS records
matching_records=$(echo "$dns_records" | jq -r ".result[] | select(.content == \"$SEARCH_IP\") | .name")
if [[ -n "$matching_records" ]]; then
echo "-- Found matching DNS records in domain $domain_name:"
echo "$matching_records"
fi
}
# Fetch zones with pagination
fetch_zones() {
page=1
while true; do
echo "Fetching zones, page $page..."
response=$(curl -s -X GET "$API_URL/zones?page=$page&per_page=50" \
-H "X-Auth-Email: $CLOUDFLARE_EMAIL" \
-H "X-Auth-Key: $CLOUDFLARE_API_KEY" \
-H "Content-Type: application/json")
# Check for errors in the API response
if [[ $(echo "$response" | jq '.success') != "true" ]]; then
echo "Error fetching zones: $(echo "$response" | jq '.errors')"
exit 1
fi
# Process each zone in the current page
echo "$response" | jq -c '.result[]' | while read -r zone; do
zone_id=$(echo "$zone" | jq -r '.id')
domain_name=$(echo "$zone" | jq -r '.name')
search_dns_records "$zone_id" "$domain_name"
done
# Check if this is the last page
total_pages=$(echo "$response" | jq -r '.result_info.total_pages')
if (( page >= total_pages )); then
break
fi
((page++))
done
}
# Start the script
echo "Searching DNS entries for IP: $SEARCH_IP..."
fetch_zones
echo "Search completed."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment