-
Notifications
You must be signed in to change notification settings - Fork 0
/
xe_routing.py
214 lines (172 loc) · 8.55 KB
/
xe_routing.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
"""Class and function that get and parse Cisco XE routing table"""
import ipaddress
def get_vrfs(netmiko_connection: object) -> list:
"""Using the connection object created from the netmiko_login(), get routes from device"""
vrfs = []
send_vrfs = netmiko_connection.send_command(command_string="show vrf")
cli_line = send_vrfs.splitlines()
if cli_line:
for line in cli_line:
if "Name" in line:
pass
else:
vrfs.append(line.split()[0])
return vrfs
def get_routing_table(netmiko_connection:object, vrfs:str=None) -> list:
"""Using the connection object created from the netmiko_login(), get routes from device"""
routes = None
if not vrfs:
netmiko_connection.send_command(command_string="terminal length 0")
routes = netmiko_connection.send_command(command_string="show ip route")
elif vrfs:
netmiko_connection.send_command(command_string="terminal length 0")
routes = netmiko_connection.send_command(command_string=f"show ip route vrf {vrfs}")
routes = routes.split("\n")
return routes
def is_subneted(prefix:str) -> str:
mask = None
if prefix.rfind("subnetted") != -1:
mask = "/" + prefix.split()[0].split("/")[1]
return mask
class RoutingIos():
"""Begin route enty breakdown with various methods"""
route_protocols = ("L", "C", "S", "R", "M", "B", "D", "D EX", "O", "O IA", "O N1", "O N2", "O E1", "O E2", "i",
"i su", "i L1", "i l2", "*", "U", "o", "P", "H", "l", "a", "+", "%", "p", "S*")
def __init__(self, connection_obj):
self.netmiko_connection = connection_obj
self.mask = None
self.prefix = None
self.protocol = None
self.vrf = "global"
self.admin_dis = []
self.metric = []
self.next_hop = []
self.route_age = []
self.interface = []
self._route_table = []
self._parse_global_routing_entries()
self._parse_vrf_routing_entries()
def _parse_vrf_routing_entries(self)-> None:
"""Parses entries for vrf table"""
vrfs = get_vrfs(self.netmiko_connection)
for vrf in vrfs:
self.vrf = vrf
route_entries = get_routing_table(self.netmiko_connection, vrfs=vrf)
list(map(self._route_breakdown, route_entries))
def _parse_global_routing_entries(self)-> None:
"""Parses entries with no vrfs"""
route_entries = get_routing_table(self.netmiko_connection)
list(map(self._route_breakdown, route_entries))
# Writes last entry to list
self.database()
# Clear list for next method
self.clear_lists()
def _find_prefix(self, prefix:str) -> None:
"""Find prefix in current line"""
if prefix.rfind("via") == -1:
try:
if str(ipaddress.IPv4Network(prefix.split()[2])) != self.prefix:
self.database()
self.clear_lists()
self.prefix = str(ipaddress.IPv4Network(prefix.split()[2]))
except (ipaddress.AddressValueError, IndexError, ValueError):
pass
try:
if str(ipaddress.IPv4Network(prefix.split()[1])) != self.prefix:
self.database()
self.clear_lists()
self.prefix = str(ipaddress.IPv4Network(prefix.split()[1]))
except (ipaddress.AddressValueError, IndexError, ValueError):
pass
elif prefix.rfind("via") != -1:
try:
if str(ipaddress.IPv4Network(prefix.split()[1])) != self.prefix:
self.database()
self.clear_lists()
self.prefix = str(ipaddress.IPv4Network(prefix.split()[1]))
except (ipaddress.AddressValueError, IndexError, ValueError):
pass
try:
if str(ipaddress.IPv4Network(prefix.split()[2])) != self.prefix:
self.database()
self.clear_lists()
self.prefix = str(ipaddress.IPv4Network(prefix.split()[1]))
except (ipaddress.AddressValueError, IndexError, ValueError):
pass
# Combines self.mask with variablly subnetted prefixes
if prefix.rfind("via") != -1 and prefix.split()[1].rfind("/") == -1:
try:
if str(ipaddress.IPv4Network(prefix.split()[1] + self.mask)) != self.prefix:
self.database()
self.clear_lists()
self.prefix = str(ipaddress.IPv4Network(prefix.split()[1] + self.mask))
except (ipaddress.AddressValueError, IndexError, ValueError, TypeError):
pass
def _get_protocol(self, routing_entry:str) -> None:
"""Gets routing protocol from routing entry"""
find_protocol = [protocol for protocol in RoutingIos.route_protocols if protocol in routing_entry[0:5]]
if len(find_protocol) == 2:
self.protocol = find_protocol[1]
elif len(find_protocol) == 1:
self.protocol = find_protocol[0]
else:
pass
def _route_breakdown(self, routing_entry:str) -> None:
"""Breaks down each routing entry for routing attributes"""
self.mask = is_subneted(routing_entry)
self._find_prefix(routing_entry)
self._get_protocol(routing_entry)
if routing_entry.rfind("directly connected") != -1:
self.admin_dis.append("0")
self.metric.append("0")
self.next_hop.append(routing_entry.split()[4].strip(","))
self.route_age.append("permanent")
self.interface.append(routing_entry.split()[5].strip(","))
elif routing_entry.rfind("via") != -1:
if routing_entry.split()[0].rfind("[") == -1:
if len(routing_entry.split()) == 6:
self.admin_dis.append(routing_entry.split()[2].split("/")[0].strip("["))
self.metric.append(routing_entry.split()[2].split("/")[1].strip("]"))
self.next_hop.append(routing_entry.split()[4].strip(","))
self.route_age.append(routing_entry.split()[5].strip(","))
elif len(routing_entry.split()) == 7:
self.admin_dis.append(routing_entry.split()[2].split("/")[0].strip("["))
self.metric.append(routing_entry.split()[2].split("/")[1].strip("]"))
self.next_hop.append(routing_entry.split()[4].strip(","))
self.route_age.append(routing_entry.split()[5].strip(","))
self.interface.append(routing_entry.split()[6].strip(","))
elif len(routing_entry.split()) == 8:
self.admin_dis.append(routing_entry.split()[3].split("/")[0].strip("["))
self.metric.append(routing_entry.split()[3].split("/")[1].strip("]"))
self.next_hop.append(routing_entry.split()[5].strip(","))
self.route_age.append(routing_entry.split()[6].strip(","))
self.interface.append(routing_entry.split()[7].strip(","))
else:
# Static routes
self.admin_dis.append(routing_entry.split()[2].split("/")[0].strip("["))
self.metric.append(routing_entry.split()[2].split("/")[1].strip("]"))
self.next_hop.append(routing_entry.split()[4].strip(","))
self.route_age.append("permanent")
self.protocol = routing_entry.split()[0]
else:
self.admin_dis.append(routing_entry.split()[0].split("/")[0].strip("["))
self.metric.append(routing_entry.split()[0].split("/")[1].strip("]"))
self.next_hop.append(routing_entry.split()[2].strip(","))
self.route_age.append(routing_entry.split()[3].strip(","))
self.interface.append(routing_entry.split()[4].strip(","))
def clear_lists(self) -> None:
self.admin_dis = []
self.metric = []
self.next_hop = []
self.route_age = []
self.interface = []
def database(self) -> list:
if self.route_age:
try:
self._route_table.append([self.vrf, self.prefix, self.protocol, self.admin_dis[0], "--".join(self.metric),
"--".join(self.next_hop), "--".join(self.interface), "--".join(self.route_age)])
except IndexError:
pass
@property
def route_table(self):
return self._route_table