Skip to content

Instantly share code, notes, and snippets.

@mwilliams
Forked from KB1RMA/record_checker.rb
Last active August 29, 2015 14:23
Show Gist options
  • Save mwilliams/f56f53ecc161d21a1853 to your computer and use it in GitHub Desktop.
Save mwilliams/f56f53ecc161d21a1853 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
# Check DNS records against two authoritative nameservers
# usage: $ ruby record-checker.rb --input_file record-checker.csv --nameserver nina.ns.cloudflare.com
require 'csv'
require 'optparse'
require 'table_print'
options = {}
OptionParser.new do |opt|
opt.on('--input_file tests.csv') { |o| options[:input_file] = o }
opt.on('--nameserver nina.ns.cloudflare.com') { |o| options[:nameserver] = o }
end.parse!
# Check to make sure the file is readable
if !File.exists?(options[:input_file])
abort("#{options[:input_file]} is unreadable!")
end
# Adds two methods to the string class so we can output red and green to the console
class String
def red; "\033[31m#{self}\033[0m" end
def green; "\033[32m#{self}\033[0m" end
end
# Build each record's test values for output
test_list = []
# Loops through values in specified CSV
CSV.foreach(options[:input_file], headers:true, encoding:'windows-1251:utf-8') do |row|
# Find current value returned by DNS
current = `dig -t #{row["type"]} #{row["test"]} +short 2>&1 | head -n 1`
# Find value returned by speicfied DNS server
tested = `dig @#{options[:nameserver]} -t #{row["type"]} #{row["test"]} +short 2>&1 | head -n 1 `
# Check if this has passed or failed the test
if current.eql? tested
valid = "✓".green
else
valid ="✗".red
end
test_list << [
type:row["type"],
test:row["test"],
response:row["value"],
current:current,
tested:tested,
valid:valid,
]
end
# Print table of responses
tp test_list, {:valid => {:display_name => "?".green}}, :type, :test, :current, :tested
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment