我正在使用以下代码从文件中提取的网址列表上运行whois命令:
File.read("urls.txt").each_line do |lookup|
begin
Timeout::timeout(5) do
info = `whois #{lookup}`
File.open("whois_log", "w") {|whois| whois.puts(info)}
end
rescue Timeout::Error
puts "Error looking up URL #{lookup}".red.bold
next
end
end
whois命令会生成以下信息:
Domain ID: 1250695815_DOMAIN_COM-VRSN
Registrar WHOIS Server: whois.corporatedomains.com
Registrar URL: www.cscprotectsbrands.com
Updated Date: 2015-09-29T06:01:55Z
Creation Date: 2007-10-02T10:47:56Z
Registrar Registration Expiration Date: 2017-10-02T10:47:56Z
Registrar: CSC CORPORATE DOMAINS, INC.
Sponsoring Registrar IANA ID: 299
Registrar Abuse Contact Email: domainabuse@cscglobal.com
Registrar Abuse Contact Phone: +1.8887802723
Domain Status: serverTransferProhibited http://www.icann.org/epp#serverTransferProhibited
Domain Status: serverDeleteProhibited http://www.icann.org/epp#serverDeleteProhibited
Domain Status: clientTransferProhibited http://www.icann.org/epp#clientTransferProhibited
Domain Status: serverUpdateProhibited http://www.icann.org/epp#serverUpdateProhibited
Registry Registrant ID:
Registrant Name: CSC Corporate Domains
Registrant Organization: CSC Corporate Domains
Registrant Street: 62 RCOM Drive
Registrant City: Yarmouth
Registrant State/Province: NS
Registrant Postal Code: B5A 4B1
Registrant Country: CA
Registrant Phone: +1.9027492792
Registrant Phone Ext:
Registrant Fax: +1.4137238334
Registrant Fax Ext:
Registrant Email: admin@internationaladmin.com
Registry Admin ID:
Admin Name: CSC Corporate Domains
Admin Organization: CSC Corporate Domains
Admin Street: 62 RCOM Drive
Admin City: Yarmouth
Admin State/Province: NS
Admin Postal Code: B5A 4B1
Admin Country: CA
Admin Phone: +1.9027492792
Admin Phone Ext:
Admin Fax: +1.4137238334
Admin Fax Ext:
Admin Email: admin@internationaladmin.com
Registry Tech ID:
Tech Name: CSC Corporate Domains
Tech Organization: CSC Corporate Domains
Tech Street: 62 RCOM Drive
Tech City: Yarmouth
Tech State/Province: NS
Tech Postal Code: B5A 4B1
Tech Country: CA
Tech Phone: +1.9027492792
Tech Phone Ext:
Tech Fax: +1.4137238334
Tech Fax Ext:
Tech Email: admin@internationaladmin.com
Name Server: sbdns3.cscdns.net
Name Server: dns.lucasfilm.com
DNSSEC: unsigned
URL of the ICANN WHOIS Data Problem Reporting System: http://wdprs.internic.net/
>>> Last update of WHOIS database: 2015-09-29T06:01:55Z <<<
我想做的是拒绝除电子邮件,地址,电话号码,网址和ID之外的所有内容,并将其保存到文件中。
我有办法做到这一点吗?
答案 0 :(得分:0)
此示例使用popen3调用该命令,但问题是当读取一行时,如果该行与正则表达式匹配,则只放入(到您的文件,或将其附加到字符串并稍后写入)。 p>
require 'open3'
#... the rest of your code
Open3.popen3("whois #{lookup}") do |stdin, stdout, stderr|
stdout.each_line do |line|
# make this #puts call in your file object
puts line if line =~ /(Domain ID)|(Registrar URL)|(etc...)/
end
end
正则表达式中的每个选项都是要在括号内括起来并用竖线分隔的行的子字符串