Skip to content

Instantly share code, notes, and snippets.

@Skinner927
Created March 5, 2025 22:03
Show Gist options
  • Save Skinner927/3df8023ef8c85226492597f1f86cab17 to your computer and use it in GitHub Desktop.
Save Skinner927/3df8023ef8c85226492597f1f86cab17 to your computer and use it in GitHub Desktop.
whois rdap domain lookup script
#!/bin/bash
# This script requires jq
set -e
DNS_JSON="${DNS_JSON:-"$HOME"/Downloads/dns.json}"
DOMAIN="${1:?Pass domain name as first argument}"
TLD="${DOMAIN/#*.}"
die() { echo "$*" >&2; exit 1; }
[[ "$#" -eq 1 ]] || die 'Invalid number of arguments. Expected 1: the domain name'
[[ -n "$DOMAIN" ]] || die 'Domain name is empty'
[[ -n "$TLD" ]] || die 'Could not resolve TLD'
[[ "$TLD" != "$DOMAIN" ]] || die 'TLD and domain are the same. Are there no dots?'
echo "Looking up TLD $TLD for domain $DOMAIN"
# Download the dns.json bootstrap file if it doesn't exist
if [[ ! -f "$DNS_JSON" ]]; then
echo 'Downloading DNS file'
curl -sSk https://data.iana.org/rdap/dns.json -o "$DNS_JSON" \
|| die 'Failed to download dns.json'
fi
FOUND_API=false
FOUND_DOMAIN=false
while read -r -d '' api; do
[[ -n "$api" ]] || continue
FOUND_API=true
if RESP="$(curl -sSk "${api}domain/${DOMAIN}")"; then
if [[ -z "$RESP" ]]; then
echo 'Empty API data' >&2
continue
fi
FOUND_DOMAIN=true
if IS_EXPIRED="$(echo "$RESP" | jq -cr '.events | map(select(.eventAction == "expiration"))
| [(length > 0) , (map(select(.eventDate | fromdateiso8601 > now)) | length > 0)]')"
then
if [[ "$IS_EXPIRED" = '[true,true]' ]]; then
printf '\nRegistered and valid: %s\n' "$DOMAIN"
exit 0
elif [[ "$IS_EXPIRED" = '[true,false]' ]]; then
printf '\nExpired: %s\n' "$DOMAIN"
exit 1
else
echo 'Missing expiration date' >&2
fi
else
echo 'Failed to parse domain data' >&2
fi
fi
done < <(jq --raw-output0 '.services[] | select(.[0][] | select(. == "'"$TLD"'")) | .[1][]' "$DNS_JSON")
failed() { echo ''; echo "$*"; exit 1; }
if ! $FOUND_API ; then failed "Failed to find API for TLD '$TLD': $DOMAIN"; fi
if ! $FOUND_DOMAIN ; then failed "Failed to find domain: $DOMAIN"; fi
failed "Unknown reason, but the domain was not found: $DOMAIN"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment