Class: Dert::ARIN

Inherits:
Object
  • Object
show all
Defined in:
lib/dert/methods/arin.rb

Class Method Summary collapse

Class Method Details

.arin_results(company) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/dert/methods/arin.rb', line 49

def self.arin_results(company)
  client = TCPSocket.new('whois.arin.net', 43)
  client.puts(company.to_s)
  output = []

  ret = client.gets
  while ret != nil
    output.push(ret)
    ret = client.gets
  end
  client.close

  output
end

.parse(data) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/dert/methods/arin.rb', line 5

def self.parse(data)
  full_results = []
  data.each do |line|
    net_handle = line.match(/\(\s*(NET-\d{1,3}\-\d{1,3}\-\d{1,3}\-\d{1,3}\-\d{1,3})\s*\)/)
    range = line.match(/(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\s+\-\s+\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/)
    if net_handle and range
      customer_info = self.parse_net_handle(net_handle.to_s)
      full_results.push(customer_info)
    end
  end

  return full_results
end

.parse_net_handle(net_handle) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/dert/methods/arin.rb', line 19

def self.parse_net_handle(net_handle)
  customer_record = {}
  net_handle = net_handle.gsub('(', '')
  net_handle = net_handle.gsub(')', '')
  results = self.arin_results(net_handle)
  results.each do |line|
    ret = line.match(/(\w+):\s+(.*)\n/)
    if ret
      split = ret.to_s.split(':')
      key = split[0].strip
      value = split[1].strip
      if key == 'CustName' or key == 'NetHandle' or key == 'CIDR' or key == 'PostalCode'
        case key
          when 'CustName'
            key = :customer
          when 'NetHandle'
            key = :handle
          when 'CIDR'
            key = :cidr
          when 'PostalCode'
            key = :zip
        end
        customer_record[key] = value.to_s
      end
    end
  end

  return customer_record
end

.query(domain) ⇒ Object



64
65
66
67
68
# File 'lib/dert/methods/arin.rb', line 64

def self.query(domain)
  company = domain.gsub(/\.\w+$/, '')
  response = self.arin_results(company)
  self.parse(response)
end