以下是类似的内容:
def domain_exists?(domain)
# perform check
# return true|false
end
puts "valid!" if domain_exists?("example.com")
以下是类似的内容:
def domain_exists?(domain)
# perform check
# return true|false
end
puts "valid!" if domain_exists?("example.com")
require 'socket'
def domain_exists?(domain)
begin
Socket.gethostbyname(domain)
rescue SocketError
return false
end
true
end
使用ruby-whois
非常简单:
安装gem并引入即可。
a = Whois.whois("google.com")
a.available?
=> false
如果您通过Ruby Gems安装它,还会捆绑一个CLI:ruby-whois
网页链接:ruby-whois.org
`nslookup #{domain}`
并使用正则表达式等将结果解析为文本。
或者您可以使用Socket类,特别是Socket.getaddrinfo。请参阅先前的StackOverflow答案,涉及此问题。