-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_CSV.py
68 lines (51 loc) · 1.99 KB
/
generate_CSV.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
import os
from glob import glob
import pickle
from pprint import pprint
from sys import argv
# This script is used to generate CSV files for a ISP_ASN
# It takes complete path of a prefix file as input
#
# Steps ->
# Find all .pkl in cur dir recursively
# Sort them [imp]
# Now iterate over all prefixes :
# search their freq in sorted .pkl
# & append the CSV file accordingly
#
#
# Usage -> python3 generate_CSV.py AIRTEL-BHARTI_9498
# Output -> AIRTEL-BHARTI_9498_database.csv
#
# Note - Full path of AIRTEL file should be -> ISP_ASN/AIRTEL_9498
#
#
csv_name = argv[1] + '_database.csv'
with open(csv_name, 'a') as csv_file:
csv_file.write("DATE,TIME,ASN,PREFIX,FREQ\n") # add initial header
pkl_files = [y for x in os.walk('.') for y in glob(os.path.join(x[0], '*.pkl'))]
pkl_files.sort()
# sample pkl naming -> ./20210203/20210203.0200.pkl
# sample CSV naming -> 20191101,0200,AS31549,31.56.0.0/18,115
pprint(pkl_files)
with open(csv_name, 'a') as csv_file:
file = 'ISP_ASN/' + argv[1]
with open(file, 'r') as f:
for prefix in f.readlines(): # read file line by line, strip \n
prefix = prefix.strip()
# now search this prefix in sorted .pkl & append CSV
for pkl in pkl_files:
# tmp = 20191101,0200,AS31549,31.56.0.0/18,
tmp = pkl[11:19] + ',' + pkl[20:24] + ',' + 'AS' + argv[1].split('_')[1] + ',' + prefix + ','
with open(pkl, 'rb') as handle:
master_dict = pickle.load(handle)
if prefix in master_dict[argv[1].split('_')[1]]: # if 192.168.1.4 in '9894'
tmp += str(master_dict[argv[1].split('_')[1]][prefix]) + '\n' # add the freq for prefix
else:
tmp += '0\n' # else add 0 (not found)
csv_file.write(tmp)
print(tmp)
# add breaker after every prefix
tmp = '0,0,0,0,0\n'
csv_file.write(tmp)
print(tmp)