-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathip2asn.rb
54 lines (46 loc) · 1.08 KB
/
ip2asn.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
#!/usr/bin/env ruby
require 'curb'
require 'redis'
require 'optparse'
def ip2long(ip)
ipAry = ip.split(/\./)
long = ipAry[3].to_i | ipAry[2].to_i << 8 | ipAry[1].to_i << 16 | ipAry[0].to_i << 24
long
end
def getMask(ipLong)
redis = Redis.new
(0..31).each {|msk|
ipRef = (ipLong >> msk) << msk
if redis.get(ipRef).nil?
else
return ipRef
end
}
end
def redisImport
redis = Redis.new
redis.flushall
http = Curl.get("http://thyme.apnic.net/current/data-raw-table")
http.body_str.each_line {|s|
s.scan(/([0-9.]+)\/[0-9]+\s+([0-9]+)/) {|x,y|
z = ip2long(x)
redis.set(z, y)
}
}
http = Curl.get("http://thyme.apnic.net/current/data-used-autnums")
http.body_str.each_line {|s|
s.scan(/([0-9]+)\s+(.*)/) {|x,y|
redis.set(x, y)
}
}
end
userIp = ARGV[0]
redis = Redis.new
ARGV.options do |opts|
opts.on("--charge") { puts "Charging Redis..." ; redisImport ; exit}
opts.parse!
end
ipLong = ip2long(userIp)
zMask = getMask(ipLong)
zAsn = redis.get(zMask)
puts "(" + userIp + ") belongs to ASN " + zAsn + " - " + redis.get(zAsn)