Skip to content

Instantly share code, notes, and snippets.

@jpmckinney
Last active September 24, 2015 11:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jpmckinney/744627 to your computer and use it in GitHub Desktop.
Save jpmckinney/744627 to your computer and use it in GitHub Desktop.
Checks if a domain exists in all top-level domains
# blog post: http://blog.slashpoundbang.com/post/2346603512/checking-if-a-domain-exists-in-all-top-level-domains
name = "domain" # CHANGE THIS
require 'whois'
# deleted, unimplemented, or not in use: bv cs dd eh er gb sj ss yu
tlds = %w(aero asia biz cat com coop info int jobs mobi museum name net org post pro tel travel xxx) + # generic
%w(edu gov mil) + # USA
%w(arpa) + # infrastructure
%w(ac ad ae af ag ai al am an ao aq ar as at au aw ax az ba bb bd be bf bg bh bi bj bm bn bo br bs bt bw by bz ca cc cd cf cg ch ci ck cl cm cn co cr cu cv cx cy cz de dj dk dm do dz ec ee eg es et eu fi fj fk fm fo fr ga gd ge gf gg gh gi gl gm gn gp gq gr gs gt gu gw gy hk hm hn hr ht hu id ie il im in io iq ir is it je jm jo jp ke kg kh ki km kn kp kr kw ky kz la lb lc li lk lr ls lt lu lv ly ma mc md me mg mh mk ml mm mn mo mp mq mr ms mt mu mv mw mx my mz na nc ne nf ng ni nl no np nr nu nz om pa pe pf pg ph pk pl pm pn pr ps pt pw py qa re ro rs ru rw sa sb sc sd se sg sh si sk sl sm sn so sr st su sv sx sy sz tc td tf tg th tj tk tl tm tn to tp tr tt tv tw tz ua ug uk us uy uz va vc ve vg vi vn vu wf ws ye yt za zm zw)
errors = {
"This `tld' has no whois server" => [],
"execution expired" => [],
"Connection reset by peer" => [],
"Connection refused - connect(2)" => [],
"getaddrinfo: nodename nor servname provided, or not known" => [],
"This TLD has no whois server, but you can access the whois database at" => {},
"UNKNOWN" => {},
}
exists = tlds.select do |tld|
begin
response = Whois.whois("#{name}.#{tld}")
rescue => e
if errors[e.to_s]
errors[e.to_s] << tld
elsif e.to_s[/This TLD has no whois server, but you can access the whois database at `(.+)'/, 1]
errors["This TLD has no whois server, but you can access the whois database at"][tld] = $1
else
errors["unknown"][tld] = e.to_s
end
end
response && response.registered?
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment