-
Notifications
You must be signed in to change notification settings - Fork 0
/
build_networks_config_from_arouteserver.py
executable file
·210 lines (181 loc) · 6.75 KB
/
build_networks_config_from_arouteserver.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
#!/usr/bin/env python
# Copyright (C) 2017 Pier Carlo Chiodi
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import argparse
import base64
import json
import sys
try:
from urllib.request import Request, urlopen
except ImportError: # Python 2
from urllib2 import Request, urlopen
def run(args):
try:
import yaml
except:
print("\n")
print("In order to use this script, the 'PyYAML' package is needed.")
print("Please consider installing it using the 'pip install PyYAML'.")
print("\n")
return False
with open(args.clients, "r") as f:
cfg = yaml.safe_load(f)
networks = {}
email = {
"type": "email",
"host": "smtp_server_address",
"from_addr": "[email protected]",
"template_file": "/etc/exabgp/template",
"recipients": {
}
}
clients = cfg["clients"]
for client in clients:
asn = "AS{}".format(client["asn"])
neighbors = client["ip"]
if not isinstance(neighbors, list):
neighbors = [neighbors]
if asn not in networks:
networks[asn] = {
"neighbors": neighbors
}
else:
networks[asn]["neighbors"].extend(neighbors)
networks[asn]["neighbors"] = list(set(networks[asn]["neighbors"]))
if args.email:
if asn not in email["recipients"]:
email["recipients"][asn] = {
"info": {
"email": []
}
}
if args.email and args.fetch_email_from_peeringdb:
for asn in email["recipients"]:
print("Fetching contacts from PeeringDB: {}...".format(asn))
url = "https://www.peeringdb.com/api/net?asn={asn}".format(
asn=asn[2:]
)
try:
response = urlopen(url)
except Exception as e:
print("Error while fetching network info "
"from PeeringDB: {}".format(str(e)))
continue
try:
json_obj = json.load(response.fp)
except Exception as e:
print("Error while decoding PeeringDB "
"network object: {}".format(str(e)))
continue
finally:
response.close()
try:
net_id = int(json_obj["data"][0]["id"])
except Exception as e:
print("Error while getting network ID from "
"PeeringDB network object: {}".format(str(e)))
continue
url = "https://www.peeringdb.com/api/poc?net_id={}".format(net_id)
try:
if args.peeringdb_username and args.peeringdb_password:
credentials = '{}:{}'.format(
args.peeringdb_username, args.peeringdb_password
)
response = urlopen(Request(url, headers={
"Authorization": "Basic " + base64.b64encode(credentials)
}))
else:
response = urlopen(url)
except Exception as e:
print("Error while fetching contacts info "
"from PeeringDB: {}".format(str(e)))
continue
try:
json_obj = json.load(response.fp)
except Exception as e:
print("Error while decoding PeeringDB "
"contacts object: {}".format(str(e)))
continue
finally:
response.close()
for contact in json_obj["data"]:
if not contact["email"]:
continue
if contact["email"] in email["recipients"][asn]["info"]["email"]:
continue
if contact["status"] == "ok" and \
contact["role"] in ["Technical", "NOC"]:
email["recipients"][asn]["info"]["email"].append(
contact["email"]
)
json.dump(networks, args.networks, indent=2)
if args.email:
json.dump(email, args.email, indent=2)
return True
def main():
parser = argparse.ArgumentParser(
description="Build config files for invalidroutesreporter.py "
"starting from ARouteServer 'clients.yml' file.",
epilog="Copyright (c) {} - Pier Carlo Chiodi - "
"https://pierky.com".format(2017)
)
parser.add_argument(
"-n", "--networks",
type=argparse.FileType('w'),
help="The output file where the networks configuration will be "
"written. Default: stdout.",
default=sys.stdout
)
group = parser.add_argument_group(
title="EMail alerter configuration",
description="This section can be used to also configure an "
"email alerter using information from ARouteServer "
"'clients.yml' configuration file and - optionally - "
"from PeeringDB."
)
group.add_argument(
"--email",
type=argparse.FileType('w'),
help="The output file where the email alerter configuration will be "
"written."
)
group.add_argument(
"--fetch-email-from-peeringdb",
action="store_true",
help="If set, the email addresses used to send alerts are fetched "
"from PeeringDB. Used only if --email is given.",
)
group.add_argument(
"--peeringdb-username",
help="PeeringDB username. Used only if --fetch-email-from-peeringdb "
"is given. Some networks' contacts on PeeringDB are "
"hidden because they are only visible to authenticated users. "
"Providing valid username and password here allows to access "
"those contacts."
)
group.add_argument(
"--peeringdb-password",
help="PeeringDB password. See also --peeringdb-username."
)
parser.add_argument(
"clients",
help="ARouteServer 'clients.yml' file."
)
args = parser.parse_args()
if run(args):
sys.exit(0)
else:
sys.exit(1)
main()