Last active
August 29, 2015 14:23
-
-
Save KB1RMA/c8b2b53fd367508ed00f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 | |
# | |
# Example output: | |
# | |
# | TYPE | RECORD | CURRENT | @NINA.NS.CLOUDFLARE.COM | |
# --|-------|--------------------------------|--------------------------------|------------------------------- | |
# ✗ | A | www.matternow.com | 45.56.118.101 | matternow.com. | |
# ✓ | A | matternow.com | 45.56.118.101 | 45.56.118.101 | |
require 'csv' | |
require 'optparse' | |
require 'table_print' # For pretty output: http://tableprintgem.com/ | |
options = {} | |
OptionParser.new do |opt| | |
opt.on('--input_file tests.csv') { |o| options.fetch(:input_file) = o } | |
opt.on('--nameserver nina.ns.cloudflare.com') { |o| options.fetch(:nameserver) = o } | |
end.parse! | |
# Check to make sure the file is readable | |
abort("#{options.fetch(:input_file)} is unreadable!") unless File.exists?(options.fetch(:input_file)) | |
# 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.fetch(: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.fetch(:nameserver)} -t #{row["type"]} #{row["test"]} +short 2>&1 | head -n 1 ` | |
# Check if this has passed or failed the test | |
valid = current.eql?(tested) ? "✓".green : valid ="✗".red | |
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: {display_name: "Record"}}, | |
:current, | |
tested: {display_name: "@#{options[:nameserver]}"} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment