I recently got a request to resolve the domains bound to our company's service to their corresponding storage spaces. It mostly involved string processing, so I wrote a script with the little Ruby I'd learned.

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
# /usr/bin/ruby -w
# coding:utf-8
# dig the domain and bucket
# https://github.com/bluemonk/net-dns
require 'net/dns'
require 'json'
# Run dig on the given URL and take the first record in the answer section
def dig_dns(url)
packet = Net::DNS::Resolver.start("#{url}")
r = packet.answer[0].to_s.split(" ")
print "#{r[0].to_s.chop} " "#{r[4].to_s.split(".")[0]}\n"
end
# Replace * in wildcard domains with test so they follow DNS naming rules
def subt(str)
if str.include? '*'
str.sub('*','upyun')
else
str
end
end
# Pass in a file containing domains as an argument
# filename = ARGV[0]
filename = 'yuming.txt'
def afile(filename)
File.open(filename,'r') do |f|
f.each do |line|
line.split(' ').each {|s|
dig_dns(subt(s))
}
end
end
end
afile(filename)