Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use Ruby to check if a domain exists?

Something along the lines of:

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

puts "valid!" if domain_exists?("example.com")
like image 964
maček Avatar asked Apr 22 '10 22:04

maček


3 Answers

require 'socket'

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

  true
end
like image 115
Santa Avatar answered Oct 29 '22 00:10

Santa


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/

like image 35
Simone Carletti Avatar answered Oct 29 '22 02:10

Simone Carletti


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

like image 41
cobi_z Avatar answered Oct 29 '22 00:10

cobi_z