Skip to content

Instantly share code, notes, and snippets.

@sriedel
Last active December 18, 2020 08:33
Show Gist options
  • Save sriedel/bf51d7344477cf05b9763626ee1a2c19 to your computer and use it in GitHub Desktop.
Save sriedel/bf51d7344477cf05b9763626ee1a2c19 to your computer and use it in GitHub Desktop.
DNS compare
#!/usr/bin/env ruby
NEW_NS_SERVER="ns-561.awsdns-06.net"
OLD_NS_SERVER="ns.udag.net"
def clean_up_answer(ns_answer)
answer_section = nil
ns_answer.match(%r{^;; ANSWER SECTION:\s*(.*?\n)\n}m) do |match|
answer_section = match[1]
end
answer_section.lines.map do |line|
hostname, _ttl, _in, type, *content = line.strip.split(/\s+/)
next unless %w[A MX CNAME TXT].include?(type)
"#{hostname} #{type} #{content.join(' ')}"
end.compact.sort
end
ARGV.each do |hostname|
(+hostname).strip!
old_ns_answer = %x{host -a #{hostname} #{OLD_NS_SERVER}}
new_ns_answer = %x{host -a #{hostname} #{NEW_NS_SERVER}}
if clean_up_answer(old_ns_answer) == clean_up_answer(new_ns_answer)
puts "Matched: #{hostname}"
else
puts "Difference for host #{hostname}:"
puts "Old Raw:\n#{old_ns_answer}\n"
puts "New Raw:\n#{new_ns_answer}\n"
puts "Old Cleaned:\n#{clean_up_answer(old_ns_answer)}\n"
puts "New Cleaned:\n#{clean_up_answer(new_ns_answer)}\n"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment