Connecting the Dots.
最近接到一个需求,将绑定在我司的域名解析出对应的空间,主要设计到对字符串处理,于是用学到的一点点 Ruby 知识写了个脚本。
1234567891011121314151617181920212223242526272829303132333435363738
# /usr/bin/ruby -w# coding:utf-8# dig the domain and bucket# https://github.com/bluemonk/net-dnsrequire 'net/dns'require 'json'# 将给定的 url dig 出来 answer 里的第一条解析记录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# 将泛域名里面的 * 替换成 test,以符合域名解析规则def subt(str) if str.include? '*' str.sub('*','upyun') else str endend# 包含域名的文件作为参数输入# 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 endendafile(filename)