Skip to content

Instantly share code, notes, and snippets.

@curipha
Created May 23, 2017 12:17
Show Gist options
  • Save curipha/deedfef8c9e6be1b4fd8a914525d7f1f to your computer and use it in GitHub Desktop.
Save curipha/deedfef8c9e6be1b4fd8a914525d7f1f to your computer and use it in GitHub Desktop.
Check the FQDN is valid and resolvable
#!/usr/bin/env ruby
# Google Public DNS
#DNS = %w( 8.8.8.8 8.8.4.4 )
# (Recommended) Google Public DNS, Dnsadvantage, OpenDNS, NortonDNS, Level 3
DNS = %w( 8.8.8.8 8.8.4.4 156.154.70.1 156.154.71.1 208.67.222.222 208.67.220.220 198.153.192.1 198.153.194.1 4.2.2.1 4.2.2.2 4.2.2.3 4.2.2.4 4.2.2.5 4.2.2.6 )
WAITMAX = 5000 # ms
unless ARGV[0]
puts <<README
Usage:
$ ./#{File.basename(__FILE__)} filename
Check if the FQDN is valid and can be name resolution.
File contents must be separated by newlines.
README
exit
end
file = File.expand_path ARGV[0]
if ! File.exist? ARGV[0]
puts "File (#{file}) is not found."
exit
end
fqdn = []
begin
f = open file
f.each {|l| fqdn << l.strip }
f.close
rescue
puts "Failed to read file (#{file})."
exit
end
nxdomain = []
noerror = []
servfail = []
other = []
threads = []
fqdn.each {|h|
threads << Thread.new(h) {|h|
sleep(rand(WAITMAX).to_f / 1000)
s = DNS.at(rand(DNS.size))
r = `dig #{h} @#{s}`.strip
o = "#{h} [#{s}]"
if r.include? 'status: NOERROR'
noerror << o
elsif r.include? 'status: NXDOMAIN'
nxdomain << o
elsif r.include? 'status: SERVFAIL'
servfail << o
else
r = r.gsub(/[\n\r;<>]/, ' ')
r = r.gsub(/\s+/, ' ')
other << "#{o}\n(#{r})"
end
}
}
max = threads.size - 1
for i in 0..max
threads[i].join
end
noerror = [] # prevent display noerror (too verbose)
v = false
if ! noerror.empty?
puts if v
puts '## NOERROR ##'
puts noerror
v = true
end
if ! nxdomain.empty?
puts if v
puts '## NXDOMAIN ##'
puts nxdomain
v = true
end
if ! servfail.empty?
puts if v
puts '## SERVFAIL ##'
puts servfail
v = true
end
if ! other.empty?
puts if v
puts '## OTHERWISE ##'
puts other
v = true
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment