-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathgen_whois_route.py
executable file
·369 lines (286 loc) · 12.5 KB
/
gen_whois_route.py
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
#!/usr/bin/env python3
"""
Copyright (c) 2021-2022 Cisco Systems, Inc. and others. All rights reserved.
.. moduleauthor:: Tim Evens <[email protected]>
"""
import getopt
import gzip
import os
import sys
from collections import OrderedDict, deque
from ftplib import FTP
import traceback
import dbHandler
# ----------------------------------------------------------------
# RR Database download sites
# ----------------------------------------------------------------
RR_DB_FTP = OrderedDict()
RR_DB_FTP['radb'] = {'site': 'ftp.radb.net', 'path': '/radb/dbase/', 'filename': 'radb.db.gz'}
RR_DB_FTP['nttcom'] = {'site': 'rr1.ntt.net', 'path': '/nttcomRR/', 'filename': 'nttcom.db.gz'}
RR_DB_FTP['level3'] = {'site': 'ftp.radb.net', 'path': '/radb/dbase/', 'filename': 'level3.db.gz'}
RR_DB_FTP['arin'] = {'site': 'ftp.arin.net', 'path': '/pub/rr/', 'filename': 'arin.db.gz'}
RR_DB_FTP['afrinic'] = {'site': 'ftp.afrinic.net', 'path': '/pub/dbase/', 'filename': 'afrinic.db.gz'}
RR_DB_FTP['apnic'] = {'site': 'ftp.apnic.net', 'path': '/pub/apnic/whois/', 'filename': 'apnic.db.route.gz'}
RR_DB_FTP['jpirr'] = {'site': 'ftp.radb.net', 'path': '/radb/dbase/', 'filename': 'jpirr.db.gz'}
RR_DB_FTP['apnic_v6'] = {'site': 'ftp.apnic.net', 'path': '/pub/apnic/whois/', 'filename': 'apnic.db.route6.gz'}
RR_DB_FTP['ripe'] = {'site': 'ftp.ripe.net', 'path': '/ripe/dbase/split/', 'filename': 'ripe.db.route.gz'}
RR_DB_FTP['ripe_v6'] = {'site': 'ftp.ripe.net', 'path': '/ripe/dbase/split/', 'filename': 'ripe.db.route6.gz'}
RR_DB_FTP['altdb'] = {'site': 'ftp.altdb.net', 'path': '/pub/altdb/', 'filename': 'altdb.db.gz'}
RR_DB_FTP['lacnic'] = {'site': 'irr.lacnic.net', 'path': '/', 'filename': 'lacnic.db.gz'}
RR_DB_FTP['bell'] = {'site': 'whois.in.bell.ca', 'path': '/', 'filename': 'bell.db.gz'}
RR_DB_FTP['bboi'] = {'site': 'irr.bboi.net', 'path': '/', 'filename': 'bboi.db.gz'}
RR_DB_FTP['tc'] = {'site': 'ftp.radb.net', 'path': '/radb/dbase/', 'filename': 'tc.db.gz'}
RR_DB_FTP['idnic'] = {'site': 'irr-mirror.idnic.net', 'path': '/', 'filename': 'idnic.db.gz'}
RR_DB_FILES = OrderedDict()
RR_DB_FILES['radb'] = {'filename': 'radb.db.gz'}
RR_DB_FILES['nttcom'] = {'filename': 'nttcom.db.gz'}
RR_DB_FILES['level3'] = {'filename': 'level3.db.gz'}
RR_DB_FILES['arin'] = {'filename': 'arin.db.gz'}
RR_DB_FILES['afrinic'] = {'filename': 'afrinic.db.gz'}
RR_DB_FILES['apnic'] = {'filename': 'apnic.db.route.gz'}
RR_DB_FILES['jpirr'] = {'filename': 'jpirr.db.gz'}
RR_DB_FILES['apnic_v6'] = {'filename': 'apnic.db.route6.gz'}
RR_DB_FILES['ripe'] = {'filename': 'ripe.db.route.gz'}
RR_DB_FILES['ripe_v6'] = {'filename': 'ripe.db.route6.gz'}
RR_DB_FILES['altdb'] = {'filename': 'altdb.db.gz'}
RR_DB_FILES['lacnic'] = {'filename': 'lacnic.db.gz'}
RR_DB_FILES['bell'] = {'filename': 'bell.db.gz'}
RR_DB_FILES['bboi'] = {'filename': 'bboi.db.gz'}
RR_DB_FILES['tc'] = {'filename': 'tc.db.gz'}
RR_DB_FILES['idnic'] = {'filename': 'idnic.db.gz'}
# ----------------------------------------------------------------
# Whois mapping
# ----------------------------------------------------------------
WHOIS_ATTR_MAP = {
# RADB
'route': 'prefix',
'route6': 'prefix',
'descr': 'descr',
'origin': 'origin_as',
}
#: Bulk insert queue
bulk_insert_queue = deque()
MAX_BULK_INSERT_QUEUE_SIZE = 2000
#: Temp directory
TMP_DIR = '/tmp/rr_dbase'
# ----------------------------------------------------------------
def import_rr_db_file(db, source, db_filename):
""" Reads RR DB file and imports into database
..see: http://irr.net/docs/list.html for details of RR FTP/DB files
:param db: DbAccess reference
:param source: Source of the data (i.e. key value of RR_DB_FTP dict)
:param db_filename: Filename of DB file to import
"""
record = {'source': source}
inf = None
print("Parsing %s" % db_filename)
if (db_filename.endswith(".gz")):
inf = gzip.open(db_filename, 'rb')
else:
inf = open(db_filename, 'r')
if (inf != None):
recordComplete = False
prev_attr = ""
for line in inf:
line = line.decode("utf-8", "ignore")
line = line.rstrip('\n')
line = line.replace("\t", " ")
# empty line means record is complete
if len(line) == 0:
recordComplete = True
# Skip lines with a comment
elif line[0] == '#' or line[0] == '%':
continue
elif line[0] == ' ':
if prev_attr == 'descr':
#print "continuation: (%s) '%s'\n\t\t%r" % (prev_attr, line, record)
# Line is a continuation of previous attribute
try:
value = line.strip()
value = value.replace("'", "")
value = value.replace("\\", "")
record[WHOIS_ATTR_MAP[prev_attr]] += "\n" + value
except:
print("problem with continuation: (%s) '%s'\n\t\t%r" % (prev_attr, line, record))
pass
elif ': ' in line:
# Parse the attributes and build record
(attr, value) = line.split(': ', 1)
attr = attr.strip()
value = value.strip()
value = value.replace("'", "")
value = value.replace("\\", "")
if (attr == 'origin'):
# Strip off characters 'AS'
value = value[2:]
if ' ' in value:
value = value.split(' ', 1)[0]
# Convert from dot notation back to numeric
if "." in value:
a = value.split('.', 1)
record[WHOIS_ATTR_MAP[attr]] = (int(a[0]) << 16) + int(a[1])
else:
record[WHOIS_ATTR_MAP[attr]] = int(value)
elif (attr == 'route' or attr == 'route6'):
# Extract out the prefix_len
a = value.split('/')
record['prefix_len'] = int(a[1])
record[WHOIS_ATTR_MAP[attr]] = a[0]
# allow appending duplicate attributes
elif (attr == 'descr' and WHOIS_ATTR_MAP[attr] in record):
record[WHOIS_ATTR_MAP[attr]] += ' \n' + value
elif (attr in WHOIS_ATTR_MAP):
record[WHOIS_ATTR_MAP[attr]] = value
prev_attr = attr
if recordComplete:
recordComplete = False
if 'prefix' in record:
add_route_to_db(db, record)
record = {'source': source}
# Commit any pending items
add_route_to_db(db, {}, commit=True)
# Close the file
inf.close()
def add_route_to_db(db, record, commit=False):
""" Adds/updates route in DB
:param db: DbAccess reference
:param record: Dictionary of column names and values
:param commit: True to flush/commit the queue and this record, False to queue
and perform bulk insert.
:return: True if updated, False if error
"""
# Add entry to queue
if (len(record) > 4):
bulk_insert_queue.append("('%s/%d'::inet,%d,%u,'%s', '%s')" % (record['prefix'], record['prefix_len'],
record['prefix_len'],
record['origin_as'],
record['descr'][:254],
record['source']))
# Insert/commit the queue if commit is True or if reached max queue size
if ((commit == True or len(bulk_insert_queue) > MAX_BULK_INSERT_QUEUE_SIZE) and
len(bulk_insert_queue)):
query = "INSERT INTO info_route (prefix,prefix_len,origin_as,descr,source) "
query += " SELECT DISTINCT ON (prefix,origin_as) * FROM ( VALUES "
# try:
while bulk_insert_queue:
query += "%s," % bulk_insert_queue.popleft()
# except IndexError:
# # No more entries
# pass
# Remove the last comma if present
if (query.endswith(',')):
query = query[:-1]
query += " ) t(prefix,prefix_len,origin_as,descr,source) " # add order by if needed
query += " ON CONFLICT (prefix,prefix_len,origin_as) DO UPDATE SET "
query += " descr=excluded.descr, source=excluded.source, timestamp=now()"
# print "QUERY = %s" % query
# print "----------------------------------------------------------------"
db.queryNoResults(query)
def download_data_file():
""" Download the RR data files
"""
if (not os.path.exists(TMP_DIR)):
os.makedirs(TMP_DIR)
for source in RR_DB_FTP:
try:
print ("Downloading %s..." % source)
ftp = FTP(RR_DB_FTP[source]['site'])
ftp.login()
ftp.cwd(RR_DB_FTP[source]['path'])
ftp.retrbinary("RETR %s" % RR_DB_FTP[source]['filename'],
open("%s/%s" % (TMP_DIR, RR_DB_FTP[source]['filename']), 'wb').write)
ftp.quit()
print (" Done downloading %s" % source)
except:
print ("Error processing %s, skipping" % source)
traceback.print_exc()
def script_exit(status=0):
""" Simple wrapper to exit the script cleanly """
exit(status)
def parseCmdArgs(argv):
""" Parse commandline arguments
Usage is printed and program is terminated if there is an error.
:param argv: ARGV as provided by sys.argv. Arg 0 is the program name
:returns: dictionary defined as::
{
user: <username>,
password: <password>,
db_host: <database host>
}
"""
REQUIRED_ARGS = 3
found_req_args = 0
cmd_args = {'user': None,
'password': None,
'db_host': None,
'db_name': "openbmp"}
if (len(argv) < 3):
usage(argv[0])
sys.exit(1)
try:
(opts, args) = getopt.getopt(argv[1:], "hu:p:d:",
["help", "user=", "password=", "dbName="])
for o, a in opts:
if o in ("-h", "--help"):
usage(argv[0])
sys.exit(0)
elif o in ("-u", "--user"):
found_req_args += 1
cmd_args['user'] = a
elif o in ("-p", "--password"):
found_req_args += 1
cmd_args['password'] = a
elif o in ("-d", "--dbName"):
found_req_args += 1
cmd_args['db_name'] = a
else:
usage(argv[0])
sys.exit(1)
# The last arg should be the command
if (len(args) <= 0):
print ("ERROR: Missing the database host/IP")
usage(argv[0])
sys.exit(1)
else:
found_req_args += 1
cmd_args['db_host'] = args[0]
# The last arg should be the command
if (found_req_args < REQUIRED_ARGS):
print ("ERROR: Missing required args, found %d required %d" % (found_req_args, REQUIRED_ARGS))
usage(argv[0])
sys.exit(1)
return cmd_args
except (getopt.GetoptError, TypeError) as err:
print (str(err)) # will print something like "option -a not recognized"
usage(argv[0])
sys.exit(2)
def usage(prog):
""" Usage - Prints the usage for this program.
:param prog: Program name
"""
print ("")
print ("Usage: %s [OPTIONS] <database host/ip address>" % prog)
print ("")
print (" -u, --user".ljust(30) + "Database username")
print (" -p, --password".ljust(30) + "Database password")
print ("")
print ("OPTIONAL OPTIONS:")
print (" -h, --help".ljust(30) + "Print this help menu")
print (" -d, --dbName".ljust(30) + "Database name, default is 'openbmp'")
def main():
"""
"""
cfg = parseCmdArgs(sys.argv)
# Download the RR data files
download_data_file()
db = dbHandler.dbHandler()
db.connectDb(cfg['user'], cfg['password'], cfg['db_host'], cfg['db_name'])
for source in RR_DB_FILES:
try:
import_rr_db_file(db, source, "%s/%s" % (TMP_DIR, RR_DB_FILES[source]['filename']))
except:
traceback.print_exc()
#rmtree(TMP_DIR)
db.close()
if __name__ == '__main__':
main()