Skip to content

Commit

Permalink
and here is the dns resolution library w/ examples + license
Browse files Browse the repository at this point in the history
  • Loading branch information
ryan-allen committed Jul 1, 2008
0 parents commit fe5d709
Show file tree
Hide file tree
Showing 3 changed files with 187 additions and 0 deletions.
19 changes: 19 additions & 0 deletions MIT-LICENSE
@@ -0,0 +1,19 @@
Copyright (c) 2008 Ryan Allen, FlashDen Pty Ltd

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.
102 changes: 102 additions & 0 deletions dns.rb
@@ -0,0 +1,102 @@
#
# automate using dig to check our DNS migration, coz liek, i am so seriously
# sick of doing this manually :)
#
# author: ryan allen (ryan@eden.cc)
#
# you can use this syntax to check against your local dns resolution:
#
# DNS.verify('flashden.net').resolves_to('72.32.2.225')
# DNS.verify('www.flashden.net').is_aliased_to('flashden.net')
# DNS.verify('blog.flashden.net').resolves_to('64.13.250.227')
#
# see example.rb for some real world usage.
#
module DNS

def self.delay=(seconds)
Verifier.delay = seconds
end

def self.verify(domain)
Verifier.new(domain)
end

class Verifier

@@delay = nil

def self.delay=(seconds)
@@delay = seconds
end

def initialize(domain)
@server = nil
@domain = domain
yield self if block_given?
self
end

def with_server(server)
@server = server
yield self if block_given?
self
end

def resolves_to(ip)
check! 'A', ip
end

def is_aliased_to(domain)
check! 'CNAME', domain
end

def maps_mail_to(domain, args = {})
check! 'MX', domain, args
end

# or it.answers_to ??
def has_authorative_nameserver_of(domain)
check! 'NS', domain
end

private

def check!(query, against, args = {})
if ['A', 'CNAME', 'NS'].include?(query)
pattern = /#{@domain}\.\s+\d+\s+IN\s+#{query}\s+#{against}/
elsif query == 'MX'
pattern = /#{@domain}\.\s+\d+\s+IN\s+MX\s+#{args[:priority]}\s+#{against}/
else
raise "Unsupported query (#{query.inspect}): only A, CNAME and MX."
end
send(result!(query) =~ pattern ? :pass! : :fail!, query, against, args)
end

def result!(query)
@last_dig_command = if @server
"dig @#{@server} #{@domain} #{query}"
else
"dig #{@domain} #{query}"
end
sleep(@@delay) if @@delay
@last_dig_output = `#{@last_dig_command}`
end

def pass!(*args)
report! 'PASS', *args
end

def fail!(*args)
report! 'FAIL', *args
puts @last_dig_command
puts @last_dig_output
end

def report!(result, query, against, args = {})
puts "#{result}: #{@domain} #{query} #{against} #{"@#{@server}" if @server} #{args.inspect if args.any?}"
end

end

end
66 changes: 66 additions & 0 deletions example.rb
@@ -0,0 +1,66 @@
#
# this will print results to $stdout, so to see it working, run this file.
#
require 'dns'

# the nameservers we want to check against
@local_dns = [nil]
@slicehost_dns = %w(ns1.slicehost.net ns2.slicehost.net)

# 1 second between dig requests so nameservers don't block us (this default to
# no delay, and it's super fast, good for local queries)
DNS.delay = 1

(@local_dns + @slicehost_dns).each do |server|

DNS.verify('yeahnah.org').with_server(server) do |it|
# checks A records
it.resolves_to('208.78.102.114')
# checks MX records
it.maps_mail_to('ASPMX.L.GOOGLE.COM', :priority => 10)
it.maps_mail_to('ALT1.ASPMX.L.GOOGLE.COM', :priority => 20)
it.maps_mail_to('ALT2.ASPMX.L.GOOGLE.COM', :priority => 30)
end

DNS.verify('www.yeahnah.org').with_server(server) do |it|
# checks CNAME records
it.is_aliased_to('yeahnah.org')
end

DNS.verify('tumble.yeahnah.org').with_server(server) do |it|
it.resolves_to('72.32.231.8') # tumblr :)
end

end

# shorthand that checks against your local dns
DNS.verify('yeahnah.org').resolves_to('208.78.102.114')
DNS.verify('tumble.yeahnah.org').resolves_to('72.32.231.8')

# oh, and check authorative ns :)
DNS.verify('yeahnah.org').has_authorative_nameserver_of('ns1.slicehost.net')
DNS.verify('yeahnah.org').has_authorative_nameserver_of('ns2.slicehost.net')

# and FYI this is the output of the script, will display dig output on failures
# PASS: yeahnah.org A 208.78.102.114
# PASS: yeahnah.org MX ASPMX.L.GOOGLE.COM {:priority=>10}
# PASS: yeahnah.org MX ALT1.ASPMX.L.GOOGLE.COM {:priority=>20}
# PASS: yeahnah.org MX ALT2.ASPMX.L.GOOGLE.COM {:priority=>30}
# PASS: www.yeahnah.org CNAME yeahnah.org
# PASS: tumble.yeahnah.org A 72.32.231.8
# PASS: yeahnah.org A 208.78.102.114 @ns1.slicehost.net
# PASS: yeahnah.org MX ASPMX.L.GOOGLE.COM @ns1.slicehost.net {:priority=>10}
# PASS: yeahnah.org MX ALT1.ASPMX.L.GOOGLE.COM @ns1.slicehost.net {:priority=>20}
# PASS: yeahnah.org MX ALT2.ASPMX.L.GOOGLE.COM @ns1.slicehost.net {:priority=>30}
# PASS: www.yeahnah.org CNAME yeahnah.org @ns1.slicehost.net
# PASS: tumble.yeahnah.org A 72.32.231.8 @ns1.slicehost.net
# PASS: yeahnah.org A 208.78.102.114 @ns2.slicehost.net
# PASS: yeahnah.org MX ASPMX.L.GOOGLE.COM @ns2.slicehost.net {:priority=>10}
# PASS: yeahnah.org MX ALT1.ASPMX.L.GOOGLE.COM @ns2.slicehost.net {:priority=>20}
# PASS: yeahnah.org MX ALT2.ASPMX.L.GOOGLE.COM @ns2.slicehost.net {:priority=>30}
# PASS: www.yeahnah.org CNAME yeahnah.org @ns2.slicehost.net
# PASS: tumble.yeahnah.org A 72.32.231.8 @ns2.slicehost.net
# PASS: yeahnah.org A 208.78.102.114
# PASS: tumble.yeahnah.org A 72.32.231.8
# PASS: yeahnah.org NS ns1.slicehost.net
# PASS: yeahnah.org NS ns2.slicehost.net

0 comments on commit fe5d709

Please sign in to comment.