Skip to content

Instantly share code, notes, and snippets.

@d-tux
Created February 5, 2009 16:07
Show Gist options
  • Save d-tux/58781 to your computer and use it in GitHub Desktop.
Save d-tux/58781 to your computer and use it in GitHub Desktop.
Reads Bind zone files
#!/usr/bin/ruby
def resolve_node(node)
return node if node[-1] == '.'[0]
return node unless $context.has_key? "ORIGIN"
return "#{node}.#{$context["ORIGIN"]}"
end
ARGS.each do |filename|
File.open filename, 'r' do |file|
$context = {}
file.each_line do |line|
if line.match /^\$([A-Z]+)\s+(.*)/ then
key, value = $1.strip, $2.sub(/\s*;.*$/, '').strip
$context[key] = value
elsif line.match /(\S+)\s+(A|CNAME)\s+(\S+)/ then
node = resolve_node $1.strip
type = $2.strip
pointer = $3.sub(/\s*;.*$/, '').strip
action = case type
when "A" then
"is homed at"
when "CNAME" then
pointer = resolve_node pointer
"is an alias for"
else
nil
end
unless action.nil?
puts "#{node} #{action} #{pointer}"
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment