-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOSPF_error_detector.py
More file actions
172 lines (156 loc) · 6.1 KB
/
OSPF_error_detector.py
File metadata and controls
172 lines (156 loc) · 6.1 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
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
"""
Automated test to check OSPF links share the same network/mask, area and timers.
Be aware that as CDP is used you will see the mismatch from both traffic directions
ie: it will show a failure from R1 to R2, as well as R2 back to R1.
This is the same mismatch shown from both perspectives of each device.
This script uses CDP and was designed to operate between directly connected, OSPF-enabled devices.
Switched shared segments will break the CDP logic.
This script uses Nornir2, not Nornir3. Install with: pip3 install nornir"<3"
"""
import os
import ipaddress
from collections import defaultdict
import requests
from nornir import InitNornir
from nornir.plugins.tasks.apis import http_method
from rich import print as rprint
nr = InitNornir(config_file="config.yaml")
requests.packages.urllib3.disable_warnings()
headers = {
"Accept": "application/yang-data+json",
"Content-Type": "application/yang-data+json",
}
CLEAR = "clear"
os.system(CLEAR)
config_dict = defaultdict(dict)
facts_dict = defaultdict(dict)
network_error = []
area_error = []
timer_error = []
rprint("[cyan]SCANNING...[/cyan]\n\n")
def get_ospf(task):
"""
Use RESTCONF to retrieve data and create 2 dictionaries of OSPF info
"""
send = task.run(
http_method,
auth=("john", "cisco"),
verify=False,
method="get",
headers=headers,
url=f"https://{task.host.hostname}:443/restconf/data/ospf-oper-data",
)
sender = task.run(
http_method,
auth=("john", "cisco"),
verify=False,
method="get",
headers=headers,
url=f"https://{task.host.hostname}:443/restconf/data/native/interface",
)
task.host["ospf-facts"] = send.response.json()
task.host["ospf-config"] = sender.response.json()
config_interfaces = task.host["ospf-config"]["Cisco-IOS-XE-native:interface"]
for inter_types in config_interfaces:
interfaces = config_interfaces[inter_types]
for intf in interfaces:
try:
name = intf["name"]
intlink = inter_types + str(name)
ip = intf["ip"]["address"]["primary"]["address"]
mask = intf["ip"]["address"]["primary"]["mask"]
config_dict[f"{task.host}"][intlink] = [ip, mask]
except KeyError:
pass
instances = task.host["ospf-facts"]["Cisco-IOS-XE-ospf-oper:ospf-oper-data"][
"ospf-state"
]["ospf-instance"]
for instance in instances:
areas = instance["ospf-area"]
for area in areas:
try:
area_id = area["area-id"]
ospf_interfaces = area["ospf-interface"]
for ospf_interface in ospf_interfaces:
name = ospf_interface["name"]
dead = ospf_interface["dead-interval"]
hello = ospf_interface["hello-interval"]
facts_dict[f"{task.host}"][name] = [dead, hello, area_id]
except KeyError:
pass
def get_cdp(task):
"""
Use CDP to correlate connected links.
Pull link infomation from OSPF dictionaries and test
"""
send = task.run(
http_method,
auth=("john", "cisco"),
verify=False,
method="get",
headers=headers,
url=f"https://{task.host.hostname}:443/restconf/data/cdp-neighbor-details",
)
task.host["cdp-facts"] = send.response.json()
cdp_neighbors = task.host["cdp-facts"][
"Cisco-IOS-XE-cdp-oper:cdp-neighbor-details"
]["cdp-neighbor-detail"]
for neighbor in cdp_neighbors:
dev_name = neighbor["device-name"]
local_intf = neighbor["local-intf-name"]
port_id = neighbor["port-id"]
if local_intf.startswith("Gi"):
local_ip = config_dict[f"{task.host}"][local_intf][0]
local_mask = config_dict[f"{task.host}"][local_intf][1]
local_net = ipaddress.ip_network(local_ip + "/" + local_mask, strict=False)
remote_ip = config_dict[dev_name][port_id][0]
remote_mask = config_dict[dev_name][port_id][1]
remote_net = ipaddress.ip_network(
remote_ip + "/" + remote_mask, strict=False
)
local_area = facts_dict[f"{task.host}"][local_intf][2]
remote_area = facts_dict[dev_name][port_id][2]
local_hello = facts_dict[f"{task.host}"][local_intf][1]
remote_hello = facts_dict[dev_name][port_id][1]
local_dead = facts_dict[f"{task.host}"][local_intf][0]
remote_dead = facts_dict[dev_name][port_id][0]
if not local_net == remote_net:
network_error.append(
(
f"NETWORK MISMATCH: {task.host} {local_intf} {local_net}"
f" ~ ~ {dev_name} {port_id} {remote_net}"
)
)
if not local_area == remote_area:
area_error.append(
(
f"AREA MISMATCH: {task.host} {local_intf} area {local_area}"
f" ~ ~ {dev_name} {port_id} area {remote_area}"
)
)
if not local_hello == remote_hello:
timer_error.append(
(
f"TIMER MISMATCH: {task.host} {local_intf} hello interval {local_hello}"
f" ~ ~ {dev_name} {port_id} hello interval {remote_hello}"
)
)
if not local_dead == remote_dead:
timer_error.append(
(
f"TIMER MISMATCH: {task.host} {local_intf} dead interval {local_dead}"
f" ~ ~ {dev_name} {port_id} dead interval {remote_dead}"
)
)
ospf_result = nr.run(task=get_ospf)
cdp_result = nr.run(task=get_cdp)
if network_error:
network_fails = sorted(network_error)
rprint(network_fails)
if area_error:
area_fails = sorted(area_error)
rprint(area_fails)
if timer_error:
timer_fails = sorted(timer_error)
rprint(timer_fails)
rprint("\n[yellow]*** SCAN COMPLETED *************[/yellow]\n")