我正在尝试检查给定的主机名是否已在我们的 IPAM 系统的 A 记录中使用。例如,如果https://this-internal-url.net已经在 DNS 系统中“采用”,它应该返回 true。否则为假。
我尝试了一堆不同的解决方案,其中大部分来自 SO
当传入一个无效的 url 时,其中的大多数都会出错:
require 'resolv'
ip_ad = Resolv.getaddress <url>
print ip_ad
如果 url 在 dns 中不存在,就会出错。
require 'infoblox'
# create a connection
connection = Infoblox::Connection.new(
username: <infoblox_user>,
password: <infoblox_passwd>,
host: <infoblox_grid_server>,
ssl_opts: {verify: false},
logger: Logger.new(STDOUT)
)
search = Infoblox::Arecord.find(connection, {"name~" => "#{url}"})
print search
# also tried: search = Infoblox::Search.find(connection, "search_string~" => "#{url}")
还:
require 'rubygems'
require 'dnsruby'
include Dnsruby
info = system("nslookup -q=txt #{url}")
print info
还:
require 'rubygems'
require 'dnsruby'
include Dnsruby
res = Dnsruby::Resolver.new
ret = res.query(url, Types.TXT)
print "#{ret.answer}"
还:
require 'resolv'
txt = Resolv::DNS.open do |dns|
records = dns.getresources(url, Resolv::DNS::Resource::IN::TXT)
records.empty? ? nil : records.map(&:data).join(" ")
end
print txt
还:
require "resolv"
Resolv::DNS.open do |dns|
ress = dns.getresources "infoir.com", Resolv::DNS::Resource::IN::A
log(ress.map { |r| [r.exchange.to_s, r.preference] })
end
我知道这些片段都不会评估为真/假。我只是还没有找到任何可以区分现有 DNS 记录和不存在记录而不会出错的解决方案。
任何指针将不胜感激!