-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwhoare.rb
executable file
·87 lines (75 loc) · 1.5 KB
/
whoare.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#!/usr/bin/env ruby
# -*- encoding: utf-8 -*-
# TODO
# - APNIC の出力をパーズする
# - 出力のフォーマットを決める
# - RIPE
# - ARIN
require 'csv'
require 'ipaddr'
require 'socket'
class RIRtable
def initialize
@tab = []
CSV.foreach("ipv6-unicast-address-assignments.csv") { |row|
next if row[0] == "Prefix" # first line
@tab << row.values_at(0, 1, 3)
}
end
def lookup(addrstr)
ip = IPAddr.new(addrstr)
@tab.each { |a|
prefix = IPAddr.new a[0]
return a if prefix.include?(ip)
}
[ "::/0", "NOTALLOCATED", nil ]
end
end
module Whois
class Generic
def query(str)
sock = TCPSocket.open @host, 43
sock.write "#{str}\r\n"
result = sock.read
sock.close
result
end
end
class APNIC < Generic
def initialize
@host = "whois.apnic.net"
end
end
class RIPE < Generic
def initialize
@host = "whois.ripe.net"
end
end
end
class RSPL
def initialize(text)
@text = text
end
end
def usage
puts "usage: whoare <ipaddr>"
exit
end
usage if ARGV.size == 0
ARGV.each { |host|
a = Addrinfo.getaddrinfo(host, nil, :INET6)
if a.size == 0
puts "#{ipaddr} has no ipv6 address"
next
end
ipaddr = a[0].ip_address
a = RIRtable.new.lookup ipaddr
if a[2] == "whois.apnic.net"
whois = Whois::APNIC.new
puts whois.query(ipaddr)
elsif a[2] == "whois.ripe.net"
puts Whois::RIPE.new.query(ipaddr)
else
puts "#{ipaddr} is allocated to #{a[1]}"
end
}