0

I am running a whois command on a list of urls pulled from a file with the following code:

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

A whois command produces the following information:

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 <<<

What I want to do is reject everything but the email, addresses, phone numbers, url, and IDs and save them to a file.

Is there a way I can do this?

Jordan Running
  • 102,619
  • 17
  • 182
  • 182
13aal
  • 1,634
  • 1
  • 21
  • 47

1 Answers1

0

This example calls the command with popen3, but the thing is that when reading a line, you only puts (to your file, or append it to a string and write it later) if that line matches the regular expression.

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

Each option in the regex is a substring of the line to be selected enclosed within parenthesis and separated with a vertical bar

rccursach
  • 305
  • 3
  • 14