This repository has been archived on 2024-03-28. You can view files and clone it, but cannot push or open issues or pull requests.
dotfiles/common/bin/check_domain

91 lines
2.6 KiB
Plaintext
Raw Permalink Normal View History

2019-08-07 11:42:38 -05:00
#!/usr/bin/env bash
2019-08-07 12:06:28 -05:00
# TODO:
# + trap exit signals to cleanup all temp files and cancel all jobs
# + run in batches to prevent spawning way too many processes
# + detect when piping into another application and suppress colors?
# + temp file control (or none for no disk space)?
# + command line options or env vars?
# + --tlds "com,net,org"
# + --max-concurrent-whois-jobs
# + --avail-regex and --timeout-regex
2019-08-07 11:42:38 -05:00
# suggested usage: ./check_domain | tee /tmp/chkdmn && sort /tmp/chkdmn
# constants
AVAIL_REGEX='^No match|^NOT FOUND|^Not fo|AVAILABLE|^No Data Fou|has not been regi|No entri'
TIMEOUT_REGEX='^Timeout'
2019-08-07 11:43:12 -05:00
COLOR_RESET="\x1b[0m" COLOR_GREEN="\x1b[32m"
2019-08-07 11:42:38 -05:00
COLOR_YELLOW="\x1b[33m"
COLOR_RED="\x1b[31m"
DOMAINS=( \
'.com' '.net' '.org' '.eu' '.in' '.it' '.sk' '.ac' '.ae' '.af' '.ag' '.al' \
'.am' '.as' '.at' '.ax' '.be' '.bi' '.bo' '.by' '.bz' '.cc' '.cd' '.cf' '.cg' \
'.ch' '.ci' '.cl' '.cm' '.cn' '.co' '.cr' '.cx' '.cz' '.dk' '.dm' '.do' '.ec' \
'.ee' '.es' '.fi' '.fm' '.fo' '.ga' '.gd' '.gf' '.gg' '.gl' '.gp' '.gq' '.gr' \
'.gs' '.gt' '.gy' '.hk' '.hm' '.hn' '.ht' '.id' '.im' '.in' '.io' '.ir' '.is' \
'.je' '.ke' '.kg' '.kz' '.la' '.lc' '.li' '.lt' '.lu' '.lv' '.ly' '.me' '.mg' \
'.mk' '.ml' '.mn' '.mq' '.ms' '.mu' '.mw' '.mx' '.na' '.ne' '.ng' '.nl' '.nu' \
'.nz' '.pe' '.ph' '.pk' '.pl' '.pr' '.pt' '.pw' '.qa' '.ro' '.rs' '.ru' '.rw' \
'.sb' '.sc' '.sd' '.se' '.sh' '.si' '.sl' '.sr' '.st' '.su' '.sx' '.sg' '.tk' \
'.tl' '.to' '.tv' '.tw' '.ug' '.vc' '.ve' '.vg' '.vn' '.vu' '.ws' \
)
# check dependencies
if command -v whois >/dev/null 2>&1; then
:
else
echo "You need to install whois before proceeding!"
exit 2
fi
# check arguments
if [ "$#" == "0" ]; then
echo "You need to supply at least one argument!"
exit 1
fi
# main function
check_domain() {
2020-04-20 10:38:19 -05:00
local tmp
local domain
2019-08-07 11:42:38 -05:00
if [ "$#" == "0" ]; then
echo "No domain specified."
return
fi
2020-04-20 10:38:19 -05:00
domain="$1"
2019-08-07 12:10:02 -05:00
2019-08-07 12:06:28 -05:00
# create a unique temporary file
2020-04-20 10:38:19 -05:00
tmp=$(mktemp "${domain}_XXX")
2019-08-07 12:10:02 -05:00
2019-08-07 12:06:28 -05:00
# dump whois output into temp file
2019-08-07 12:10:02 -05:00
whois "$domain" > "$tmp" 2>&1
2019-08-07 12:06:28 -05:00
# check contents and output appropriately
2020-04-20 10:38:19 -05:00
if grep -E -q "$AVAIL_REGEX" "$tmp" > /dev/null 2>&1; then
2019-08-07 12:06:28 -05:00
echo -e "$COLOR_GREEN$domain / probably available$COLOR_RESET"
2020-04-20 10:38:19 -05:00
elif grep -E -q "$TIMEOUT_REGEX" "$tmp" > /dev/null 2>&1; then
2019-08-07 12:06:28 -05:00
echo -e "$COLOR_YELLOW$domain / timed out$COLOR_RESET"
2019-08-07 11:42:38 -05:00
else
2019-08-07 12:06:28 -05:00
echo -e "$COLOR_RED$domain / unavailable$COLOR_RESET"
2019-08-07 11:42:38 -05:00
fi
2019-08-07 12:10:02 -05:00
# cleanup
rm "$tmp"
2019-08-07 11:42:38 -05:00
}
# iterate all provided domain names combined with all TLDs and check them
# concurrently
elements=${#DOMAINS[@]}
while (( "$#" )); do
2020-04-20 10:38:19 -05:00
for (( i=0;i<elements;i++ )); do
2019-08-07 12:10:02 -05:00
check_domain "$1${DOMAINS[${i}]}" &
2019-08-07 11:42:38 -05:00
done
shift
done
wait