-
Notifications
You must be signed in to change notification settings - Fork 0
/
s2_enrich_with_orgid.py
executable file
·87 lines (74 loc) · 2.58 KB
/
s2_enrich_with_orgid.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
#!/usr/bin/env python
#
# v1: 20190720
from minirdapc import rdap_client
import csv
import ipaddr
# if __name__ == "__main__":
# rc = rdap_client.rdap_client("https://rdap.lacnic.net/rdap")
# r = rc.rdap_query("ip", "200.40.20.1")
# # print(r)
if __name__ == "__main__":
print("Enriching CSV file")
csvfile = open("var/s1_invalid_prefixes.csv", "r")
outfile = open("var/s2_enrich_with_orgid.csv", "w")
csvin = csv.DictReader(csvfile, dialect="excel", delimiter="|")
rdapc = rdap_client.rdap_client("https://rdap.lacnic.net/rdapt2/", \
w_apikey="1d72e332-ed44-4234-bc45-6bde980d2705-a09732ff-0746-4d95-986a-a5111890c6ba"
)
header = next(csvin, None)
newheader = []
ouput_array = []
# csvout.writerow(header)
for line in csvin:
# print(line)
newline = {}
newheader = []
# print(line)
cprefixes = 0
for key in line:
k = key # this is needed because the type of line being OrderedDict
v = line[key]
# print("\tk={}, v={}".format(k,v))
try:
n4 = ipaddr.IPv4Network(v)
orgid = rdapc.prefixToOrgid(v)
newline[k] = v
newheader.append(k)
# newline.append(orgid)
newline['orgid'+str(cprefixes)] = orgid
newheader.append('orgid'+str(cprefixes))
cprefixes = cprefixes + 1
except ipaddr.AddressValueError:
# raise
newline[k] = v
newheader.append(k)
next
except Exception as err:
print("Unexpected error {0}".format(err))
print("Current RDAP JSON: {0}".format(rdapc.last_response))
raise
## end for
# print(newline)
ouput_array.append(newline)
## end for
## write output to file
print("Now writing CSV output to disk. Headers are {}".format(newheader))
csvout = csv.DictWriter(outfile, dialect='excel', delimiter='|', fieldnames=newheader)
csvout.writeheader()
for x in ouput_array:
try:
csvout.writerow(x)
except ValueError:
print("Wrong fields in row.")
print("\tLine contains: {}".format(x))
print("\tField names: {}".format(newheader))
csvfile.close()
raise
except:
csvfile.close()
raise
print("END!")
csvfile.close()
outfile.close()
# END SCRIPT ######################################################################