Skip to content

Instantly share code, notes, and snippets.

@spheromak
Created January 31, 2013 19:06
Show Gist options
  • Select an option

  • Save spheromak/4685448 to your computer and use it in GitHub Desktop.

Select an option

Save spheromak/4685448 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
#
# Parse a zone file and dump output suitable for a cucumber test data set
#
class DnsZoneParser
require 'socket'
attr_accessor :records
def initialize(filename)
@records = Array.new
File.open(filename, 'r').each_line do |line|
next if (line =~ /^\s*;/)
next if (line =~ /^\s*@/)
next if (line =~ /^\s*\*/)
parts = line.split
next if parts.empty?
next unless parts[2] =~ /A|CNAME/
ap = {
:type => parts[2],
:name => parts[0],
:dest => parts[3]
}
@records << ap
end
end
def sort_type(type)
(@records.find_all {|r| r[:type] == type }).sort{|a,b| a[:name] <=> b[:name]}
end
def print
failed = []
sort_type("A").each do |r|
puts "|#{r[:name]}|#{r[:dest]}|"
end
sort_type("CNAME").each do |r|
# determine if its an internal redir to an ip.
# if not just print as-s
follow = @records.find{|i| i[:name] == r[:dest] }
dest = follow ? follow[:dest] : r[:dest]
# if we end in a . try looking it up from here
if dest =~ /.*\.$/
begin
s = ::Socket.getaddrinfo(dest.chop!,nil)
dest = s[0][2]
rescue
failed << {:name => r[:name], :dest => dest.chop!}
next
end
end
puts "|#{r[:name]}|#{dest}| "
end
out = (failed.map {|k| "#{k[:name]}->#{k[:dest]}\n"}).join(" ")
puts "-"*80
puts "\nThese CNAMES failed:\n #{out}"
end
end
z = DnsZoneParser.new(ARGV[0])
z.print
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment