1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
|
#!/usr/bin/env ruby
# Copyright, 2009, 2012, by Samuel G. D. Williams. <http://www.codeotaku.com>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
# Pulls down DNS data from old-dns
# rd-dns-check -s old-dns.mydomain.com -d mydomain.com. -f old-dns.yml
# Check data against old-dns
# rd-dns-check -s old-dns.mydomain.com -d mydomain.com. -c old-dns.yml
# Check data against new DNS server
# rd-dns-check -s 10.0.0.36 -d mydomain.com. -c old-dns.yml
require 'yaml'
require 'optparse'
require 'set'
class DNSRecord
def initialize(arr)
@record = arr
normalize
end
def normalize
@record[0] = @record[0].downcase
@record[1] = @record[1].upcase
@record[2] = @record[2].upcase
@record[3] = @record[3].downcase
end
def hostname
@record[0]
end
def klass
@record[1]
end
def type
@record[2]
end
def value
@record[3]
end
def is_address?
["A", "AAAA"].include?(type)
end
def is_cname?
return type == "CNAME"
end
def to_s
"#{hostname.ljust(50)} #{klass.rjust(4)} #{type.rjust(5)} #{value}"
end
def key
"#{hostname}:#{klass}:#{type}".downcase
end
def to_a
@record
end
def == other
return @record == other.to_a
end
end
def dig(dns_server, cmd, exclude = ["TXT", "HINFO", "SOA", "NS"])
records = []
IO.popen("dig @#{dns_server} +nottlid +nocmd +noall +answer " + cmd) do |p|
p.each do |line|
r = line.chomp.split(/\s/, 4)
next if exclude.include?(r[2])
records << DNSRecord.new(r)
end
end
return records
end
def retrieve_records(dns_server, dns_root)
return dig(dns_server, "#{dns_root} AXFR")
end
def resolve_hostname(dns_server, hostname)
return dig(dns_server, "#{hostname} A").first
end
def resolve_address(dns_server, address)
return dig(dns_server, "-x #{address}").first
end
def print_summary(records, errors, okay, &block)
puts "[ Summary ]".center(72, "=")
puts "Checked #{records.size} record(s). #{errors} errors."
if errors == 0
puts "Everything seemed okay."
else
puts "The following records are okay:"
okay.each do |r|
if block_given?
yield r
else
puts "".rjust(12) + r.to_s
end
end
end
end
# Resolve hostnames to IP address "A" or "AAAA" records.
# Works through CNAME records in order to find out the final
# address if possible. Checks for loops in CNAME records.
def resolve_addresses(records)
addresses = {}
cnames = {}
# Extract all hostname -> ip address mappings
records.each do |r|
if r.is_address?
addresses[r.hostname] = r
elsif r.is_cname?
cnames[r.hostname] = r
end
end
cnames.each do |hostname, r|
q = r
trail = []
failed = false
# Keep track of CNAME records to avoid loops
while q.is_cname?
trail << q
q = cnames[q.value] || addresses[q.value]
# Q could be nil at this point, which means there was no address record
# Q could be already part of the trail, which means there was a loop
if q == nil || trail.include?(q)
failed = true
break
end
end
if failed
q = trail.last
puts "*** Warning: CNAME record #{hostname} does not point to actual address!"
trail.each_with_index do |r, idx|
puts idx.to_s.rjust(10) + ": " + r.to_s
end
end
addresses[r.hostname] = q
end
return addresses, cnames
end
def check_reverse(records, dns_server)
errors = 0
okay = []
puts "[ Checking Reverse Lookups ]".center(72, "=")
records.each do |r|
next unless r.is_address?
sr = resolve_address(dns_server, r.value)
if sr == nil
puts "*** Could not resolve host"
puts "".rjust(12) + r.to_s
errors += 1
elsif r.hostname != sr.value
puts "*** Hostname does not match"
puts "Primary: ".rjust(12) + r.to_s
puts "Secondary: ".rjust(12) + sr.to_s
errors += 1
else
okay << [r, sr]
end
end
print_summary(records, errors, okay) do |r|
puts "Primary:".rjust(12) + r[0].to_s
puts "Secondary:".rjust(12) + r[1].to_s
end
end
def ping_records(records)
addresses, cnames = resolve_addresses(records)
errors = 0
okay = []
puts "[ Pinging Records ]".center(72, "=")
addresses.each do |hostname, r|
ping = "ping -c 5 -t 5 -i 1 -o #{r.value} > /dev/null"
system(ping)
if $?.exitstatus == 0
okay << r
else
puts "*** Could not ping host #{hostname.dump}: #{ping.dump}"
puts "".rjust(12) + r.to_s
errors += 1
end
end
print_summary(records, errors, okay)
end
def query_records(primary, secondary_server)
addresses, cnames = resolve_addresses(primary)
okay = []
errors = 0
primary.each do |r|
sr = resolve_hostname(secondary_server, r.hostname)
if sr == nil
puts "*** Could not resolve hostname #{r.hostname.dump}"
puts "Primary: ".rjust(12) + r.to_s
rsr = resolve_address(secondary_server, (addresses[r.value] || r).value)
puts "Address: ".rjust(12) + rsr.to_s if rsr
errors += 1
elsif sr.value != r.value
ra = addresses[r.value] if r.is_cname?
sra = addresses[sr.value] if sr.is_cname?
if (sra || sr).value != (ra || r).value
puts "*** IP Address does not match"
puts "Primary: ".rjust(12) + r.to_s
puts "Resolved: ".rjust(12) + ra.to_s if ra
puts "Secondary: ".rjust(12) + sr.to_s
puts "Resolved: ".rjust(12) + sra.to_s if sra
errors += 1
end
else
okay << r
end
end
print_summary(primary, errors, okay)
end
def check_records(primary, secondary)
s = {}
okay = []
errors = 0
secondary.each do |r|
s[r.key] = r
end
puts "[ Checking Records ]".center(72, "=")
primary.each do |r|
sr = s[r.key]
if sr == nil
puts "*** Could not find record"
puts "Primary: ".rjust(12) + r.to_s
errors += 1
elsif sr != r
puts "*** Records are different"
puts "Primary: ".rjust(12) + r.to_s
puts "Secondary: ".rjust(12) + sr.to_s
errors += 1
else
okay << r
end
end
print_summary(primary, errors, okay)
end
OPTIONS = {
:DNSServer => nil,
:DNSRoot => ".",
}
ARGV.options do |o|
script_name = File.basename($0)
o.set_summary_indent(' ')
o.banner = "Usage: #{script_name} [options]"
o.define_head "This script is designed to test and check DNS servers."
o.on("-s ns.my.domain.", "--server ns.my.domain.", String, "The DNS server to query.") { |host| OPTIONS[:DNSServer] = host }
o.on("-d my.domain.", "--domain my.domain.", String, "The DNS zone to transfer/test.") { |host| OPTIONS[:DNSRoot] = host }
o.on("-f output.yml", "--fetch output.yml", String, "Pull down a list of hosts. Filters TXT and HINFO records. DNS transfers must be enabled.") { |f|
records = retrieve_records(OPTIONS[:DNSServer], OPTIONS[:DNSRoot])
output = (f ? File.open(f, "w") : STDOUT)
output.write(YAML::dump(records))
puts "#{records.size} record(s) retrieved."
}
o.on("-c input.yml", "--check input.yml", String, "Check that the DNS server returns results as specified by the file.") { |f|
input = (f ? File.open(f) : STDIN)
master_records = YAML::load(input.read)
secondary_records = retrieve_records(OPTIONS[:DNSServer], OPTIONS[:DNSRoot])
check_records(master_records, secondary_records)
}
o.on("-q input.yml", "--query input.yml", String, "Query the remote DNS server with all hostnames in the given file, and checks the IP addresses are consistent.") { |f|
input = (f ? File.open(f) : STDIN)
master_records = YAML::load(input.read)
query_records(master_records, OPTIONS[:DNSServer])
}
o.on("-p input.yml", "--ping input.yml", String, "Ping all hosts to check if they are available or not.") { |f|
input = (f ? File.open(f) : STDIN)
master_records = YAML::load(input.read)
ping_records(master_records)
}
o.on("-r input.yml", "--reverse input.yml", String, "Check that all address records have appropriate reverse entries.") { |f|
input = (f ? File.open(f) : STDIN)
master_records = YAML::load(input.read)
check_reverse(master_records, OPTIONS[:DNSServer])
}
o.separator ""
o.separator "Help and Copyright information"
o.on_tail("--copy", "Display copyright information") {
puts "#{script_name}. Copyright (c) 2009, 2011 Samuel Williams. Released under the MIT license."
puts "See http://www.oriontransfer.co.nz/ for more information."
exit
}
o.on_tail("-h", "--help", "Show this help message.") { puts o; exit }
end.parse!
|