5

Something along the lines of:

def domain_exists?(domain)
  # perform check
  # return true|false
end

puts "valid!" if domain_exists?("example.com")
maček
  • 76,434
  • 37
  • 167
  • 198

4 Answers4

8
require 'socket'

def domain_exists?(domain)
  begin
    Socket.gethostbyname(domain)
  rescue SocketError
    return false
  end

  true
end
Jordon Bedwell
  • 3,189
  • 3
  • 23
  • 32
Santa
  • 11,381
  • 8
  • 51
  • 64
4

If you want to check whether a domain is registered or not, then you need to perform a Whois query. http://www.ruby-whois.org/

Simone Carletti
  • 173,507
  • 49
  • 363
  • 364
1

With ruby-whois is pretty easy:

Install gem and require.

a = Whois.whois("google.com")

a.available?
=> false

There is also a CLI bundled if you install it via ruby gems: ruby-whois

web page at: ruby-whois.org

nyi
  • 1,425
  • 1
  • 15
  • 17
cobi_z
  • 87
  • 4
0

You could shell out to nslookup like this:

`nslookup #{domain}`

and parse the results as text with regexes etc.

Or you can use the Socket class, specifically Socket.getaddrinfo. See previous StackOverflow answer on this very question.

Tom Morris
  • 3,979
  • 2
  • 25
  • 43