0

This SO post suggests checking domain availability through a combination of DNS lookups and WHOIS queries.

Unfortunately, it's not clear what the best way is to perform the DNS lookup in Ruby. The docs for the Dnsruby gem are sparse at best.

What's the most efficient way to use Dnsruby to see if a domain is taken already?

Thanks!

Community
  • 1
  • 1
Crashalot
  • 33,605
  • 61
  • 269
  • 439
  • A domain name can be registered but not be delegated for completely legit reasons. So using the DNS to check the availability of a domain name is not a good idea. – Patrick Mevzek Jan 03 '18 at 23:02

1 Answers1

4

This blog post titled Some examples of dnsruby in action has very detailed instructions for how to use dnsruby for common tasks.

Based on the steps mentioned there, I could verify the following behaviour in irb:

>> require 'dnsruby'
>> include DnsRuby    
>> res = Resolver.new
=> #<Dnsruby::Resolver:0x00000100f4d2c8 @resolver_ruby=nil, @src_address=nil, @single_res_mutex=#<Mutex:0x00000100f4d278>, @configured=false, @do_caching=true, @config=Config - nameservers : 192.168.1.1,  domain : empty, search : local,  ndots : 1, @do_validation=false, @query_timeout=0, @retry_delay=5, @retry_times=1, @packet_timeout=5, @port=53, @udp_size=4096, @dnssec=true, @use_tcp=false, @no_tcp=false, @tsig=nil, @ignore_truncation=false, @src_port=[0], @recurse=true, @single_resolvers=[]>

>> res.query( 'www.google.com' )
=> ;; Answer received from 192.168.1.1 (123 bytes).... lengthy answer with A record info.

>> res.query( 'www.nonexistantsite.com' )
=> Throws an Dnsruby::NXDomain: Dnsruby::NXDomain exception. 

Looks like wrapping this suitably in a ruby program is an easy way to perform DNS lookup with ruby.

Prakash Murthy
  • 12,923
  • 3
  • 46
  • 74
  • 1
    [The source code for Dnsruby/Recursor.rb](http://dnsruby.rubyforge.org/svn/tags/dnsruby-1.30/lib/Dnsruby/Recursor.rb) has a line mentioning it is thread-safe. Have to agree with it as the code for key functionality is wrapped in Mutex.syncrhronize blocks. – Prakash Murthy Sep 30 '12 at 21:41
  • Thank you very much! But if we use the query method and not the Recursor class, will it still be thread safe? – Crashalot Sep 30 '12 at 22:34
  • Sorry, my previous answer was off, as I mistook `Recursor` to `Resolver` class. Looking through [the source for the Resolver class](http://dnsruby.rubyforge.org/svn/tags/dnsruby-1.30/lib/Dnsruby/Resolver.rb), it does look like the `query` method on the `Resolver` object is implemented to be thread safe. Look for instances of `@parent.single_res_mutex.synchronize` in the code. – Prakash Murthy Oct 01 '12 at 02:04
  • Thanks, Prakash! We're seeing weird behavior with Dnsruby. On our production system, the query method returns something even if no domain exists whereas on the dev system, the method throws an exception as documented if the domain doesn't exist. Any clues? We ran "bundle install" on both systems after updating the Gemfile. – Crashalot Oct 01 '12 at 02:21
  • 1
    Actually, we figured out why. It's because our domain name queries all resolve using OpenDNS, which always returns something. We need to figure out how to route the domain name queries elsewhere, or else we'll never get correct responses. – Crashalot Oct 01 '12 at 02:42
  • 2
    Absence from the DNS is not a sufficient condition for declaring that a domain is free to register. You need whois for that. – tripleee Oct 01 '12 at 11:42
  • Yes, @tripleee. DNS is a first layer to determine if something is taken. If it's free in DNS, we still confirm with whois. Thanks for checking, though. – Crashalot Oct 03 '12 at 07:40
  • But then why do you need the DNS round-trip at all? – tripleee Oct 03 '12 at 08:10
  • @Crashalot this is not good enough, at least if you do not take care to specifically query the authoritative nameservers of the TLD of your domain name. Otherwise if you query any recursive nameserver you will both get a reply that may have been cached and/or modified. You could also get an error because of DNSSEC or other operational problems, which will not mean the domain does not exist. Also using DNS requests for A records on an hostname is wrong, you should query NS records on the domain name (at the registry authoritative nameservers as said previously). – Patrick Mevzek Jan 03 '18 at 23:05