-
Notifications
You must be signed in to change notification settings - Fork 1
/
dns.rb
102 lines (83 loc) · 2.31 KB
/
dns.rb
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
#
# automate using dig to check our DNS migration, coz liek, i am so seriously
# sick of doing this manually :)
#
# author: ryan allen ([email protected])
#
# 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