-
Notifications
You must be signed in to change notification settings - Fork 21
/
mktable
executable file
·101 lines (69 loc) · 2.66 KB
/
mktable
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
#!/usr/bin/env python3
import os
from ipaddress import ip_address, ip_network, IPv6Address
from argparse import ArgumentParser
import yaml
from prettytable import PrettyTable
def mknettable(family, communities):
table = PrettyTable(['Network', 'Community', 'AS'])
table.align = 'l'
nets = list()
for community, data in communities:
if 'networks' not in data:
continue
if family not in data['networks']:
continue
for net in data['networks'][family]:
nets.append((ip_network(net), community, data['asn'] if 'asn' in data else '(n/a)'))
for net in sorted(nets, key=lambda data: data[0]):
table.add_row(net)
return table
def mkasntable(data):
table = PrettyTable(['ASN', 'Community', 'Tech-C'])
table.sortby = 'ASN'
table.align = 'l'
table.align['ASN'] = 'r'
for community, cdata in data:
asn = cdata['asn'] if 'asn' in cdata else 0
techc = ", ".join(cdata['tech-c']) if 'tech-c' in cdata else 'n/a'
table.add_row([asn, community, techc])
return table
def mkbgptable(data):
table = PrettyTable(['IPv6', 'IPv4', 'Name', 'Community'])
table.align = 'l'
bgp = list()
for community, cdata in data:
if 'bgp' not in cdata:
continue
for name, host in cdata['bgp'].items():
ipv4 = ip_address(host['ipv4']) if 'ipv4' in host else 'n/a'
ipv6 = ip_address(host['ipv6']) if 'ipv6' in host else 'n/a'
bgp.append((ipv6, ipv4, name, community))
for row in sorted(bgp, key=lambda x: x[0] if isinstance(x[0], IPv6Address) else ip_address("::")):
table.add_row(row)
return table
def mktable(srcdir):
data = list()
for fname in sorted(list(set(os.listdir(srcdir)))):
if fname.startswith('.'):
continue
if fname.startswith('README'):
continue
fpath = os.path.join(srcdir, fname)
if os.path.isfile(fpath):
with open(fpath) as handle:
data.append((fname, yaml.safe_load(handle)))
# Table: ASN Allocations and Contact
print("{}\n".format(mkasntable(data)))
# Table: Network Alllocations
print("{}\n".format(mknettable('ipv6', data)))
print("{}\n".format(mknettable('ipv4', data)))
# Table: BGP Peers
print("{}\n".format(mkbgptable(data)))
if __name__ == '__main__':
PARSER = ArgumentParser()
PARSER.add_argument('-s', '--sourcedir', dest='src',
help='Use files in DIR as input files. Default: ../icvpn-meta/',
metavar='DIR', default='../icvpn-meta/')
ARGS = PARSER.parse_args()
mktable(ARGS.src)