我想实现一个简单的小程序,读取文件中的域名,打印出来第一条解析记录 (CNAME 或者 A 记录)。 例如,有一个 test.txt 文件,里面有如下域名 baidu.com google.com
希望输出 => baidu.com. 30 IN A 220.181.57.217 => google.com. 199 IN A 64.233.189.101
我的代码如下:
#!/usr/bin/ruby
# -*- coding: UTF-8 -*-
require 'net/dns'
filename = ARGV[0]
file = File.open(filename)
file.each_line do |line|
dig_dns("#{line}")[0]
end
file.close
def dig_dns(url)
packet = Net::DNS::Resolver.start(url)
packet.answer
end
执行之后,总是提示方法未定义,求解~
chongfu.rb:11:in `block in <main>': undefined method `dig_dns' for main:Object (NoMethodError)
from chongfu.rb:10:in `each_line'
from chongfu.rb:10:in `<main>'