如何使用Ruby检查域是否存在?

时间:2022-06-01 21:16:44

Something along the lines of:

有点像:

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

puts "valid!" if domain_exists?("example.com")

4 个解决方案

#1


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/

如果要检查域是否已注册,则需要执行Whois查询。 http://www.ruby-whois.org/

#2


7  

require 'socket'

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

  true
end

#3


1  

With ruby-whois is pretty easy:

ruby-whois非常简单:

Install gem and require.

安装gem和require。

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

a = Whois.whois(“google.com”)

a.available? => false

a.available? =>假

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

如果您通过ruby gems安装它,还会捆绑一个CLI:ruby-whois

web page at: ruby-whois.org

网页:ruby-whois.org

#4


0  

You could shell out to nslookup like this:

你可以像这样向nslookup发送shell:

`nslookup #{domain}`

and parse the results as text with regexes etc.

并使用正则表达式将结果解析为文本等。

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

或者您可以使用Socket类,特别是Socket.getaddrinfo。有关此问题,请参阅之前的*答案。

#1


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/

如果要检查域是否已注册,则需要执行Whois查询。 http://www.ruby-whois.org/

#2


7  

require 'socket'

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

  true
end

#3


1  

With ruby-whois is pretty easy:

ruby-whois非常简单:

Install gem and require.

安装gem和require。

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

a = Whois.whois(“google.com”)

a.available? => false

a.available? =>假

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

如果您通过ruby gems安装它,还会捆绑一个CLI:ruby-whois

web page at: ruby-whois.org

网页:ruby-whois.org

#4


0  

You could shell out to nslookup like this:

你可以像这样向nslookup发送shell:

`nslookup #{domain}`

and parse the results as text with regexes etc.

并使用正则表达式将结果解析为文本等。

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

或者您可以使用Socket类,特别是Socket.getaddrinfo。有关此问题,请参阅之前的*答案。