How can I use Ruby to check for a domain?

Something along the lines of:

def domain_exists?(domain) # perform check # return true|false end puts "valid!" if domain_exists?("example.com") 
+4
source share
4 answers

If you want to check if a domain is registered or not, you need to fulfill the Whois request. http://www.ruby-whois.org/

+4
source
 require 'socket' def domain_exists?(domain) begin Socket.gethostbyname(domain) rescue SocketError return false end true end 
+7
source

With ruby-whois is pretty easy:

Set the gem and demand.

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

a.available? => false

CLI is also included if you install it using ruby ​​stones: ruby-whois

on page: ruby-whois.org

+1
source

You can execute the nslookup command as follows:

 `nslookup #{domain}` 

and analyze the results as regular expression text, etc.

Or you can use the Socket class, in particular Socket.getaddrinfo. See StackOverflow 's previous answer on this very question.

0
source

Source: https://habr.com/ru/post/1307740/


All Articles