-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathparse-routes.py
More file actions
executable file
·57 lines (49 loc) · 1.21 KB
/
parse-routes.py
File metadata and controls
executable file
·57 lines (49 loc) · 1.21 KB
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
#!/usr/bin/python
import sys
import re
if len(sys.argv) > 1:
inputfile = sys.argv[1]
device = sys.argv[2]
else:
print "Please provide an input file:"
openfile = open(inputfile, 'r')
writefile = open(inputfile+"-parsed", 'w')
vrf = ""
rd = ""
route = ""
nexthop = ""
preroutelist = []
for each in openfile:
if "Route Distinguisher" in each:
rd = ""
vrf = ""
rdasn = each.split(':')[1]
rdnn = each.split(':')[2]
rd = (str(rdasn + ":" + rdnn)).split('\n')[0]
elif "vpn-instance" in each:
rd = ""
vrf = ""
replaced = each.replace('Total routes of vpn-instance ','')
vrf = replaced.split(':')[0]
elif "Network" in each:
continue
else:
route_regex = re.sub('[*^>i]','', each)
route_despace = re.sub('[ ]{2,}',' ', route_regex)
route_split = route_despace.split(' ')
if len(route_split) < 2:
pass
elif len(route_split) <= 3:
nexthop = route_split[1]
else:
if "NULL" in route_split[3]:
route = route_split[1]
nexthop = route_split[2]
else:
nexthop = route_split[1]
preroutelist.append(str(device + rd + vrf + " " + route + " " + nexthop))
routelist = set(preroutelist)
for elements in routelist:
writefile.write(elements + "\n")
openfile.close()
writefile.close()