-
Notifications
You must be signed in to change notification settings - Fork 0
/
peering.py
executable file
·363 lines (319 loc) · 13.7 KB
/
peering.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
#! /usr/bin/env python3
###############################################################################
#
# Copyright 2019-present PEERING POLICY MANGER project (Frederic LOUI)
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed On an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
###############################################################################
from threading import Thread
import argparse, glob,os, sys,logging, re, linecache, json,ipaddress
from time import sleep
from ciscoconfparse import CiscoConfParse
# set our lib path
sys.path.append(
os.path.join(os.path.dirname(os.path.abspath(__file__)), "./")
)
PROGRAM_NAME = os.path.basename(sys.argv[0])
CONFIG_TYPE_JUNOS=0
CONFIG_TYPE_IOSXR=1
CONFIG_TYPE_IOSXE_=2
CONFIG_TYPE_IOS=3
CONFIG_TYPE_FOS=4
log_level = logging.WARNING
logger = logging.getLogger(PROGRAM_NAME)
if not len(logger.handlers):
logger.addHandler(logging.StreamHandler())
logger.setLevel(log_level)
def _Exception():
exc_type, exc_obj, tb = sys.exc_info()
f = tb.tb_frame
lineno = tb.tb_lineno
filename = f.f_code.co_filename
linecache.checkcache(filename)
line = linecache.getline(filename, lineno, f.f_globals)
return 'EXCEPTION IN ({}, LINE {} "{}"): {}'.format(filename, lineno, line.strip(), exc_obj)
def _getClassName(instance):
return instance.__class__.__name__
class PPM(Thread):
def __init__(self, threadID, cfg_dir, json_dir):
Thread.__init__(self)
self.threadID = threadID
self.name = _getClassName(self)
logger.warning("%s - _init_" % PROGRAM_NAME)
self.die=False
self.cfg = ""
self.cfg_dir = cfg_dir
self.json_dir = json_dir
self.cfg_type = CONFIG_TYPE_IOSXR
def run(self):
logger.warning("%s - Main" % (self.name))
logger.warning("%s - Entering message loop" % (self.name))
while not self.die:
self.getAsnPeerListFromAllRouters()
exit(0)
def getAsnPeerListFromAllRouters(self,):
os.chdir(self.cfg_dir)
logger.warning("CFG_DIR: %s" % self.cfg_dir)
cfg_iosxr_regexp = "00309[1-9]"
cfg_junos_regexp = "00313[1-9]"
for file in glob.glob("*.cfg"):
logger.warning("CFG_FILE: %s" % file)
if re.search(cfg_junos_regexp,file):
self.getAsnPeerList(file, CONFIG_TYPE_JUNOS)
elif re.search(cfg_iosxr_regexp,file):
self.getAsnPeerList(file, CONFIG_TYPE_IOSXR)
ACTIVE_PEERS=[]
def getAsnPeerList(self, cfg, cfg_type):
if cfg_type==CONFIG_TYPE_JUNOS:
self.getAsnPeerListFromJunos(cfg)
if cfg_type==CONFIG_TYPE_IOSXR:
self.getAsnPeerListFromIosXR(cfg)
def getAsnPeerListFromJunos(self, cfg):
parse = CiscoConfParse("%s" % cfg, syntax='junos', comment='#')
intf_data = self.getInterfaceListFromJunos(parse,cfg)
#logger.warning("INTF_DATA:%s" % intf_data)
peer_list_obj=parse.find_objects(" peer-as")
peer_list=[]
logger.warning("CFG_JUNOS: %s" % cfg)
for peer in peer_list_obj:
if not peer.text in peer_list:
peer_list.append(peer.text)
#logger.warning("PEER_LIST:%s" % peer_list)
for peer in peer_list:
PEER = {}
PEER["asn"] = ""
PEER["remote_neighbors"] = []
peer_ip=""
remote_neighbors = parse.find_parents_w_child(r" neighbor ", peer)
#intf_data = {}
for neighbor in remote_neighbors:
peer_ip=re.sub(r"\s+|neighbor", "", neighbor)
peer_info = self.getPeerInterfaceSubnetJunos(intf_data,peer_ip)
#logger.warning("PEER_INFO_1:%s" % peer_info)
if intf_data is not None:
PEER["remote_neighbors"].append(peer_info)
if len(PEER["remote_neighbors"]) == 0:
#if PEER["remote_neighbors"] is None:
peer_as_parents=parse.find_parents_w_child(r"group", peer)
for parent in peer_as_parents:
remote_neighbors = parse.find_children_w_parents(parent, r" neighbor ")
for neighbor in remote_neighbors:
peer_ip=re.sub(r"\s+|neighbor", "", neighbor)
peer_info = self.getPeerInterfaceSubnetJunos(intf_data,peer_ip)
#logger.warning("PEER_INFO_2:%s" % peer_info)
if peer_info is not None:
#PEER["remote_neighbors"].append(re.sub(r"\s+|neighbor", "", neighbor))
PEER["remote_neighbors"].append(peer_info)
#if not PEER["remote_neighbors"] is None :
if not len(PEER["remote_neighbors"]) == 0:
PEER["asn"] = re.sub(r"\s+|peer-as","",peer)
ACTIVE_PEERS.append(PEER)
logger.warning("%s" % (ACTIVE_PEERS))
with open("%s/%s.json" % (self.json_dir,re.sub(r".cfg$","",cfg)), 'w') as outjsonfile:
json.dump(ACTIVE_PEERS, outjsonfile)
ACTIVE_PEERS.clear()
def getInterfaceListFromIosXR(self,parse,cfg):
branchspec = (r'^interface', r'address')
branches = parse.find_object_branches(branchspec=branchspec)
out={}
for branch in branches:
intf =""
local_ip =""
for elt in branch:
if elt is not None:
if re.search(r"^interface", elt.text):
intf=re.sub(r"\s+|interface","",elt.text)
if re.search(r"address", elt.text):
local_ip=re.sub(r"^\s+|address|ipv4|ipv6","",elt.text).strip()
local_ip=re.sub(r"\s","/",local_ip)
ipaddress.ip_network(local_ip,strict=False)
out["%s" % local_ip]="%s" % intf
return out
def getAsnPeerListFromIosXR(self,cfg):
parse = CiscoConfParse("%s" % cfg, syntax='ios', comment='!',factory=True)
intf_data=self.getInterfaceListFromIosXR(parse ,cfg)
peer_list_obj=parse.find_objects("^ remote-as")
peer_list=[]
logger.warning("CFG_IOSXR: %s" % cfg)
for peer in peer_list_obj:
if not peer.text in peer_list:
peer_list.append(peer.text)
logger.warning("%s" % peer_list)
for peer in peer_list:
PEER = {}
PEER["asn"] = ""
PEER["remote_neighbors"] = []
peer_ip = ""
remote_neighbors = parse.find_parents_w_child(r"^ neighbor ", peer)
logger.warning("REMOTE_NEIGHBORS=%s" % remote_neighbors)
for neighbor in remote_neighbors:
peer_ip=re.sub(r"\s+|neighbor", "", neighbor)
peer_info = self.getPeerInterfaceSubnetIosXR(intf_data,peer_ip)
if peer_info is not None:
PEER["remote_neighbors"].append(peer_info)
if not len(PEER["remote_neighbors"])==0:
PEER["asn"] = re.sub("\s+|remote-as","",peer )
ACTIVE_PEERS.append(PEER)
logger.warning("%s" % (ACTIVE_PEERS))
with open("%s/%s.json" % (self.json_dir,re.sub(r".cfg$","",cfg)), 'w') as outjsonfile:
json.dump(ACTIVE_PEERS, outjsonfile)
ACTIVE_PEERS.clear()
def getPeerInterfaceSubnetIosXR(self,subnets_list,peer_ip):
out={}
for subnet in subnets_list.keys():
try:
intf_ip_subnet = ipaddress.ip_network(subnet,False)
local_ip = ipaddress.ip_address(peer_ip)
if local_ip in intf_ip_subnet:
#ip_local=re.sub(r"%s"%intf_ip,"",subnet)
ip_network=intf_ip_subnet.network_address
ip_netmask=intf_ip_subnet.netmask
ip_cidr=intf_ip_subnet.prefixlen
local_intf=subnets_list[subnet]
#logger.warning("LOCAL_IP:%s" % (ip_local))
#logger.warning("IP_NETWORK:%s" % (ip_network))
#logger.warning("IP_NETMASK:%s" % (ip_netmask))
#logger.warning("IP_CIDR:%s" % (ip_cidr))
#logger.warning("LOCAL_INTF: %s" % (local_intf) )
out["local_ip"]="%s" % local_ip
out["peer_ip"]="%s" % peer_ip
out["subnet"]="%s" % ip_network
out["cidr"]="%s" % ip_cidr
out["interface"]="%s" % local_intf
return out
except (ValueError, TypeError):
logger.warning("%s is not a valid IPv4/IPv6 network:" % (ipaddr))
def getInterfaceListFromJunos(self,parse,cfg):
parse_intf=CiscoConfParse(parse.find_all_children("^interface"))
branchspec = (r'^interface', r'^ [a-z]{2}', r'unit', r'family inet', r'address')
branches = parse_intf.find_object_branches(branchspec=branchspec)
subnets_list = {}
for branch in branches:
intf =""
unit =""
local_ip =""
intf_data =""
for elt in branch:
if elt is not None:
if not re.search(r"inactive",elt.text):
if re.search(r"^ [a-z]{2}",elt.text):
intf=re.sub(r"\s+|{","",elt.text)
if re.search(r"unit",elt.text):
unit=re.sub(r"\s+|unit|{","",elt.text)
if re.search(r"address",elt.text):
local_ip=re.sub(r"\s+|address|{","",elt.text)
intf_data = "{interface:%s.%s,local_ip:%s}" % (intf,unit,local_ip)
subnets_list[local_ip]="%s.%s" % (intf,unit)
return subnets_list
def getPeerInterfaceSubnetJunos(self,subnets_list,peer_ip):
#{local_ip:1.1.1.1,ip_network:1.1.1.0,ip_cidr:24,ip_intf:xe-0/0/0.0}
out={}
for subnet in subnets_list.keys():
try:
intf_ip_subnet = ipaddress.ip_network(subnet,False)
intf_ip = ipaddress.ip_address(peer_ip)
if intf_ip in intf_ip_subnet:
ip_local=re.sub(r"\s|/[0-9]+$","",subnet)
ip_network=intf_ip_subnet.network_address
ip_netmask=intf_ip_subnet.netmask
ip_cidr=intf_ip_subnet.prefixlen
local_intf=subnets_list[subnet]
#logger.warning("LOCAL_IP:%s" % (ip_local))
#logger.warning("IP_NETWORK:%s" % (ip_network))
#logger.warning("IP_NETMASK:%s" % (ip_netmask))
#logger.warning("IP_CIDR:%s" % (ip_cidr))
#logger.warning("LOCAL_INTF: %s" % (local_intf) )
out["local_ip"]="%s" % ip_local
out["peer_ip"]="%s" % peer_ip
out["subnet"]="%s" % ip_network
out["cidr"]="%s" % ip_cidr
out["interface"]="%s" % local_intf
return out
except (ValueError, TypeError):
logger.warning("%s is not a valid IPv4/IPv6 network:" % (ipaddr))
def str2bool(v):
if isinstance(v, bool):
return v
if v.lower() in ('yes', 'true', 't', 'y', '1'):
return True
elif v.lower() in ('no', 'false', 'f', 'n', '0'):
return False
else:
raise argparse.ArgumentTypeError('Boolean value expected.')
def is_any_thread_alive(threads):
return True in [t.isAlive() for t in threads]
def graceful_exit(cfg_fh):
#t.interface._tear_down_stream()
cfg_fh.close()
sys.exit(0)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Peering Policy Manager")
parser.add_argument(
"--oxidized-address",
help="oxidized address",
type=str,
action="store",
required=False,
default="127.0.0.1",
)
parser.add_argument(
"--oxidized-port",
help="oxidized port",
type=int,
action="store",
required=False,
default=9080,
)
parser.add_argument(
"--equipment-config-dir",
help="Path to the directory where the configs are stored",
type=str,
action="store",
required=False,
default="%s/ppm-run/cfg" % os.getenv("HOME"),
)
parser.add_argument(
"--equipment-json-dir",
help="Path to the directory where the configs are stored",
type=str,
action="store",
required=False,
default="%s/ppm-run/json" % os.getenv("HOME"),
)
parser.add_argument(
"--parsing-interval",
help="Interval in seconds between updates of the MIB objects",
type=int,
action="store",
required=False,
default=60,
)
args = parser.parse_args()
try:
ACTIVE_PEERS = []
#cfg = "tour-rtr-091"
ppm = PPM(1,args.equipment_config_dir,
args.equipment_json_dir,)
ppm.daemon=True
ppm.start()
ALL_THREADS = [ppm]
while is_any_thread_alive(ALL_THREADS):
[t.join(1) for t in ALL_THREADS
if t is not None and t.isAlive()]
except KeyboardInterrupt:
logger.warning("\n%s - Received signal 2 ..." % PROGRAM_NAME)
for t in ALL_THREADS:
t.die = True
logger.warning("%s - Quitting !" % PROGRAM_NAME)
graceful_exit()