1
1
#! /usr/bin/env python
2
+ import ConfigParser
2
3
import csv
4
+ import dnsdb_query
3
5
import json
4
6
import pygeoip
5
7
import sys
6
8
7
9
from netaddr import IPAddress , IPRange , IPSet
8
10
9
- org_data = []
10
- geo_data = pygeoip .GeoIP ('data/GeoIP.dat' , pygeoip .MEMORY_CACHE )
11
+
12
+ def setup_dnsdb ():
13
+ config = ConfigParser .ConfigParser ()
14
+ config .read ('combine.cfg' )
15
+ server = config .get ('Winnower' , 'dnsdb_server' )
16
+ api = config .get ('Winnower' , 'dnsdb_api' )
17
+ sys .stderr .write ('Setting up DNSDB client\n ' )
18
+ return dnsdb_query .DnsdbClient (server , api )
19
+
11
20
12
21
def load_gi_org (filename ):
13
22
gi_org = {}
@@ -18,7 +27,7 @@ def load_gi_org(filename):
18
27
return gi_org
19
28
20
29
21
- def org_by_addr (address ):
30
+ def org_by_addr (address , org_data ):
22
31
as_num = None
23
32
as_name = None
24
33
for org in org_data :
@@ -29,23 +38,26 @@ def org_by_addr(address):
29
38
30
39
31
40
def maxhits (dns_records ):
32
- pass
41
+ max = 0
42
+ hostname = None
43
+ for record in dns_records :
44
+ if record ['count' ] > max :
45
+ max = record ['count' ]
46
+ hostname = record ['rrname' ].rstrip ('.' )
47
+ return hostname
33
48
34
49
35
- def dnsdb (address , record_type ):
36
- pass
50
+ def enrich_IPv4 (address , org_data , geo_data , dnsdb ):
51
+ as_num , as_name = org_by_addr (address , org_data )
52
+ country = geo_data .country_code_by_addr ('%s' % address )
53
+ hostname = maxhits (dnsdb .query_rdata_ip ('%s' % address ))
37
54
38
-
39
- def enrich_IPv4 (address ):
40
- as_num , as_name = org_by_addr (address )
41
- country = geo_data .country_code_by_addr (address )
42
- hostname = maxhits (dnsdb (address , "PTR" ))
43
55
return (address , as_num , as_name , country , hostname )
44
56
45
57
46
58
def reserved (address ):
47
59
# from http://en.wikipedia.org/wiki/Reserved_IP_addresses:
48
- ranges = IPSet (['0.0.0.0/8' , '100.64.0.0/10' , '127.0.0.0/8' , '192.88.99.0/24' ,
60
+ ranges = IPSet (['0.0.0.0/8' , '100.64.0.0/10' , '127.0.0.0/8' , '192.88.99.0/24' ,
49
61
'198.18.0.0/15' , '198.51.100.0/24' , '203.0.113.0/24' , '233.252.0.0/24' ])
50
62
a_reserved = address .is_reserved ()
51
63
a_private = address .is_private ()
@@ -61,7 +73,8 @@ def winnow(in_file, out_file, enr_file):
61
73
crop = json .load (f )
62
74
63
75
org_data = load_gi_org ('data/GeoIPASNum2.csv' )
64
- #country_data = load_gi_country('data/')
76
+ geo_data = pygeoip .GeoIP ('data/GeoIP.dat' )
77
+ dnsdb = setup_dnsdb ()
65
78
66
79
wheat = []
67
80
enriched = []
@@ -72,16 +85,15 @@ def winnow(in_file, out_file, enr_file):
72
85
ipaddr = IPAddress (addr )
73
86
if not reserved (ipaddr ):
74
87
wheat .append (each )
75
- enriched .append (enrich_IPv4 (ipaddr ))
88
+ enriched .append (enrich_IPv4 (ipaddr , org_data , geo_data , dnsdb ))
76
89
else :
77
- sys .stderr .write ("%s is reserved, sorry" % addr )
78
-
90
+ sys .stderr .write ("%s is reserved, sorry\n " % addr )
79
91
80
92
with open (out_file , 'wb' ) as f :
81
93
json .dump (wheat , f , indent = 2 )
82
94
83
- # with open(enr_file, 'wb') as f:
84
- # json.dump(enriched, f, indent=2)
95
+ with open (enr_file , 'wb' ) as f :
96
+ json .dump (enriched , f , indent = 2 )
85
97
86
98
87
99
if __name__ == "__main__" :
0 commit comments