Skip to content

Instantly share code, notes, and snippets.

@sstelfox
Created January 2, 2014 15:17
Show Gist options
  • Save sstelfox/8220702 to your computer and use it in GitHub Desktop.
Save sstelfox/8220702 to your computer and use it in GitHub Desktop.
A random script I wrote to look up short potentially available domain names that are 'real' words including the TLD. This was a random script I pulled out a laptop that I was cleaning up and I'm saving for posterity.
#!/usr/bin/env ruby
require 'socket'
# cc.cc, cu.cc, cz.cc, co.nr, dot.tk -> Free domain services
# Pity can't do .ie, .it as they're common word endings
# This is a list of the ASCII internationalized TLDs that allow second level
# domain registrations and do not require any form of physical presence of
# citizenship within their country as reported by Wikipedia.
tld_list = %w{
ac ae af ag ai am ao as at ax az ba bb be bf bg bh bi bj bs bw by bz cc cd cf
cg ch ci ck cl co cr cu cv cx cy cz dj dk dm do dz ec eg es fj fm fo ga gd de
df gg gh gi gl gm gp gr gs gt gw gy hk hm hn hr ht im in io iq ir is je jo kg
hi hm hn kr kz la bc li lk lt lu lv ly ma md me mg mh ml mn mp mr ms mu mv mw
mx na nc ne nf ng nl nr nu pe pf ph pk pl pn pr ps pt pw ro rs ru rw sc sd se
sg sh si sk sl sn so sr st su sy tc tg tj tk tl tm tn to tp tr tt tv tw ug us
uy uz vc vg vi vn vu ws
}
# The following were removed because it was found they were unable to be
# reasonably obtained or the sole provider of them added further restrictions
# (such as a minimum character length)
blacklist_tld = %w{
li
}
tld_list = tld_list - blacklist_tld
found_domains = []
def has_a_record(domain)
!!(Socket.getaddrinfo(domain, 0, Socket::AF_UNSPEC, Socket::SOCK_STREAM, nil, Socket::AI_CANONNAME))
rescue SocketError
false
end
nr = File.open('name_report.txt', 'w')
File.open('/usr/share/dict/words', 'r') do |f|
while !f.eof?
line = f.readline.strip.downcase
# We're looking for short domain names...
next if line.length > 7
# ... but is not just a TLD ...
next unless line.length > 2
# ... that end in a valid TLD ...
next unless tld_list.include?(line[-2..-1])
# ... which only include regular characters ...
next unless line.match(/\A[a-z0-9]+\Z/i)
# ... and isn't a duplicate of one we've already found
next if found_domains.include?(line)
domain = line.insert(-3, '.')
# We also only care about domains that might not be registered
next if has_a_record(domain)
nr.write("#{domain}\n")
found_domains << domain
end
end
# Order by the length of the domain (smallest first)
found_domains = found_domains.sort_by { |t| t.size }
nr.close
# Print out the first 100
puts found_domains.first(300).inspect
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment