-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_ixp_participants.py
64 lines (55 loc) · 1.83 KB
/
get_ixp_participants.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
import argparse
import logging
import sys
from file_handlers.pickle import PickleFileHandler
DEFAULT_INPUT = 'raw/peeringdb/latest-peeringdb-netixlan.pickle.bz2'
OUTPUT_SUFFIX = '-ixp-participants'
def make_data_lines(netixlan_data: dict) -> None:
lines = list()
if len(netixlan_data) == 0:
return lines
for entry in netixlan_data:
ix_id = entry['ix_id']
ixlan_id = entry['ixlan_id']
asn = entry['asn']
if not asn:
continue
if 'ipaddr4' in entry and entry['ipaddr4']:
line = (ix_id,
ixlan_id,
asn,
entry['ipaddr4'])
lines.append(line)
if 'ipaddr6' in entry and entry['ipaddr6']:
line = (ix_id,
ixlan_id,
asn,
entry['ipaddr6'])
lines.append(line)
lines.sort()
headers = ('ix_id', 'ixlan_id', 'asn', 'ip')
lines.insert(0, headers)
return lines
def main() -> None:
log_format = '%(asctime)s %(levelname)s %(message)s'
logging.basicConfig(
format=log_format,
level=logging.INFO, datefmt='%Y-%m-%d %H:%M:%S')
parser = argparse.ArgumentParser()
parser.add_argument('-i', '--input',
help='Use this raw dump instead of latest',
default=DEFAULT_INPUT)
parser.add_argument('-o', '--output',
help='Manually specify output file')
args = parser.parse_args()
file = PickleFileHandler(input_=args.input, output=args.output,
output_name_suffix=OUTPUT_SUFFIX)
data = file.read()
lines = make_data_lines(data)
if len(lines) <= 1:
logging.error(f'No data written.')
return
file.write(lines)
if __name__ == '__main__':
main()
sys.exit(0)