czds/czds

67 lines
2.2 KiB
Bash
Executable File

#!/bin/bash
# ICANN API for the Centralized Zones Data Service - developed by acidvegas (https://git.acid.vegas/czds)
# Reference: https://czds.icann.org
# Main program starts here
echo "ICANN Zone Data Service Script"
# Define the current date for data organization
now=$(date +"%Y-%m-%d")
# Define the output directory
output="zonefiles/$now"
# Get username and password (interactive if not set by environment variables)
username=${CZDS_USER:-$(read -p "ICANN Username: " user && echo "$user")}
password=${CZDS_PASS:-$(read -sp "ICANN Password: " pass && echo "$pass")}
echo "Authenticating as $username..."
# Make an authentication request
response=$(curl -s -X POST "https://account-api.icann.org/api/authenticate" \
-H "Content-Type: application/json" \
-H "Accept: application/json" \
-d "{\"username\":\"$username\",\"password\":\"$password\"}")
# Extract and return the access access_token
access_token=$(echo "$response" | grep -o '"accessToken":"[^"]*' | cut -d '"' -f 4)
# Check if authentication was successful
[ -z $access_token ] && echo "error: authentication failed" && exit 1
echo "Authenticated successfully & recieved access_token $access_token"
# Create output directory
mkdir -p $output
echo "Fetching zone report..."
# Get your zone report stats from the API
curl --progress-bar -o $output/.stats.csv -H "Authorization: Bearer $access_token" https://czds-api.icann.org/czds/requests/report
echo "Scrubbing report for privacy..."
# Redact username from report for privacy
sed -i 's/$username/nobody@no.name/g' $output/report.csv
echo "Fetching zone file links..."
# Get the zone file links from the API
zone_links=$(curl -s -H "Authorization: Bearer $access_token" https://czds-api.icann.org/czds/downloads/links | grep -o 'https://[^"]*')
# Download zone files
for url in $zone_links; do
tld=$(basename "$url" .zone)
echo "Downloading $url..."
# Make the GET request and save the response to a file
curl --progress-bar -o $output/$tld.txt.gz -H "Authorization: Bearer $access_token" "$url"
echo "Downloaded $tld zone file to zonefiles/$tld.txt.gz (extracting...)"
# Unzip the zone file
gunzip $output/$tld.txt.gz
done
echo "All zone files downloaded."