如何使用Ruby检查DNS记录是否存在?

wdebmtf2  于 2023-08-04  发布在  Ruby
关注(0)|答案(1)|浏览(103)

我正在检查给定的主机名是否已经在我们的IPAM系统中的A记录中使用。例如,如果https://this-internal-url.net已经在DNS系统中被“占用”,则它应该返回true。否则为假。
我试过一堆不同的解决方案,其中大部分来自S.O.
其中大多数只是在传入无效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记录和不存在的记录,而不会出错。

5kgi1eie

5kgi1eie1#

我解决了

name_taken = system("ping -c 1 -W 1 #{host_name}")

字符串
name_taken返回一个布尔值,我可以用它来确定给定主机名的DNS记录是否已经存在。

相关问题