-
Notifications
You must be signed in to change notification settings - Fork 130
Expand file tree
/
Copy pathmodules_before.py
More file actions
320 lines (267 loc) · 10.5 KB
/
Copy pathmodules_before.py
File metadata and controls
320 lines (267 loc) · 10.5 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
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
#! /usr/bin/env python
"""
"""
from time import sleep
import json
import requests
import sys
# Diable InsecureRequestWarning
requests.packages.urllib3.disable_warnings(
requests.packages.urllib3.exceptions.InsecureRequestWarning
)
# DevNet Always-On Sandbox DNA Center
# https://devnetsandbox.cisco.com/RM/Diagram/Index/471eb739-323e-4805-b2a6-d0ec813dc8fc?diagramType=Topology
dnac = {
"host": "sandboxdnac2.cisco.com",
"username": "devnetuser",
"password": "Cisco123!",
"port": 443,
}
headers = {"content-type": "application/json", "x-auth-token": ""}
def dnac_login(dnac, port, username, password):
"""
Use the REST API to Log into an DNA Center and retrieve ticket
"""
url = "https://{}:{}/dna/system/api/v1/auth/token".format(dnac, port)
# Make Login request and return the response body
response = requests.request(
"POST", url, auth=(username, password), headers=headers, verify=False
)
return response.json()["Token"]
def host_list(dnac, ticket, ip=None, mac=None, name=None):
"""
Use the REST API to retrieve the list of hosts.
Optional parameters to filter by:
IP address
MAC address
Hostname
"""
url = "https://{}/api/v1/host".format(dnac)
headers["x-auth-token"] = ticket
filters = []
# Add filters if provided
if ip:
filters.append("hostIp={}".format(ip))
if mac:
filters.append("hostMac={}".format(mac))
if name:
filters.append("hostName={}".format(name))
if len(filters) > 0:
url += "?" + "&".join(filters)
# Make API request and return the response body
response = requests.request("GET", url, headers=headers, verify=False)
return response.json()["response"]
def verify_single_host(host, ip):
"""
Simple function to verify only a single host returned from query.
If no hosts, or multiple hosts are returned, an error message is printed
and the program exits.
"""
if len(host) == 0:
print("Error: No host with IP address {} was found".format(ip))
sys.exit(1)
if len(host) > 1:
print("Error: Multiple hosts with IP address {} were found".format(ip))
print(json.dumps(host, indent=2))
sys.exit(1)
def print_host_details(host):
"""
Print to screen interesting details about a given host.
Input Paramters are:
host_desc: string to describe this host. Example "Source"
host: dictionary object of a host returned from dnac
Standard Output Details:
Host Name (hostName) - If available
Host IP (hostIp)
Host MAC (hostMac)
Network Type (hostType) - wired/wireless
Host Sub Type (subType)
VLAN (vlanId)
Connected Network Device (connectedNetworkDeviceIpAddress)
Wired Host Details:
Connected Interface Name (connectedInterfaceName)
Wireless Host Details:
Connected AP Name (connectedAPName)
"""
# If optional host details missing, add as "Unavailable"
if "hostName" not in host.keys():
host["hostName"] = "Unavailable"
# Print Standard Details
print("Host Name: {}".format(host["hostName"]))
print("Network Type: {}".format(host["hostType"]))
print(
"Connected Network Device: {}".format(
host["connectedNetworkDeviceIpAddress"]
)
) # noqa: E501
# Print Wired/Wireless Details
if host["hostType"] == "wired":
print(
"Connected Interface Name: {}".format(
host["connectedInterfaceName"]
)
) # noqa: E501
if host["hostType"] == "wireless":
print("Connected AP Name: {}".format(host["connectedAPName"]))
# Print More Standard Details
print("VLAN: {}".format(host["vlanId"]))
print("Host IP: {}".format(host["hostIp"]))
print("Host MAC: {}".format(host["hostMac"]))
print("Host Sub Type: {}".format(host["subType"]))
# Blank line at the end
print("")
def network_device_list(dnac, ticket, id=None):
"""
Use the REST API to retrieve the list of network devices.
If a device id is provided, return only that device
"""
url = "https://{}/dna/intent/api/v1/network-device".format(dnac)
headers["x-auth-token"] = ticket
# Change URL to single device given an id
if id:
url += "/{}".format(id)
# Make API request and return the response body
response = requests.request("GET", url, headers=headers, verify=False)
# Always return a list object, even if single device for consistency
if id:
return [response.json()["response"]]
return response.json()["response"]
def interface_details(dnac, ticket, id):
"""
Use the REST API to retrieve details about an interface based on id.
"""
url = "https://{}/dna/intent/api/v1/interface/{}".format(dnac, id)
headers["x-auth-token"] = ticket
response = requests.request("GET", url, headers=headers, verify=False)
return response.json()["response"]
def print_network_device_details(network_device):
"""
Print to screen interesting details about a network device.
Input Paramters are:
network_device: dict object of a network device returned from dnac
Standard Output Details:
Device Hostname (hostname)
Management IP (managementIpAddress)
Device Location (locationName)
Device Type (type)
Platform Id (platformId)
Device Role (role)
Serial Number (serialNumber)
Software Version (softwareVersion)
Up Time (upTime)
Reachability Status (reachabilityStatus)
Error Code (errorCode)
Error Description (errorDescription)
"""
# Print Standard Details
print("Device Hostname: {}".format(network_device["hostname"]))
print("Management IP: {}".format(network_device["managementIpAddress"]))
print("Device Location: {}".format(network_device["locationName"]))
print("Device Type: {}".format(network_device["type"]))
print("Platform Id: {}".format(network_device["platformId"]))
print("Device Role: {}".format(network_device["role"]))
print("Serial Number: {}".format(network_device["serialNumber"]))
print("Software Version: {}".format(network_device["softwareVersion"]))
print("Up Time: {}".format(network_device["upTime"]))
print(
"Reachability Status: {}".format(network_device["reachabilityStatus"])
) # noqa: E501
print("Error Code: {}".format(network_device["errorCode"]))
print("Error Description: {}".format(network_device["errorDescription"]))
# Blank line at the end
print("")
def print_interface_details(interface):
"""
Print to screen interesting details about an interface.
Input Paramters are:
interface: dictionary object of an interface returned from dnac
Standard Output Details:
Port Name - (portName)
Interface Type (interfaceType) - Physical/Virtual
Admin Status - (adminStatus)
Operational Status (status)
Media Type - (mediaType)
Speed - (speed)
Duplex Setting (duplex)
Port Mode (portMode) - access/trunk/routed
Interface VLAN - (vlanId)
Voice VLAN - (voiceVlan)
"""
# Print Standard Details
print("Port Name: {}".format(interface["portName"]))
print("Interface Type: {}".format(interface["interfaceType"]))
print("Admin Status: {}".format(interface["adminStatus"]))
print("Operational Status: {}".format(interface["status"]))
print("Media Type: {}".format(interface["mediaType"]))
print("Speed: {}".format(interface["speed"]))
print("Duplex Setting: {}".format(interface["duplex"]))
print("Port Mode: {}".format(interface["portMode"]))
print("Interface VLAN: {}".format(interface["vlanId"]))
print("Voice VLAN: {}".format(interface["voiceVlan"]))
# Blank line at the end
print("")
# Entry point for program
if __name__ == "__main__":
# Setup Arg Parse for Command Line parameters
import argparse
parser = argparse.ArgumentParser()
# Command Line Parameters for Source and Destination IP
parser.add_argument("source_ip", help="Source IP Address")
parser.add_argument("destination_ip", help="Destination IP Address")
args = parser.parse_args()
# Get Source and Destination IPs from Command Line
source_ip = args.source_ip
destination_ip = args.destination_ip
# Print Starting message
print("Running Troubleshooting Script for ")
print(" Source IP: {} ".format(source_ip))
print(" Destination IP: {}".format(destination_ip))
print("")
# Log into the dnac Controller to get Ticket
token = dnac_login(
dnac["host"], dnac["port"], dnac["username"], dnac["password"]
)
# Step 1: Identify involved hosts
# Retrieve Host Details from dnac
source_host = host_list(dnac["host"], token, ip=source_ip)
destination_host = host_list(dnac["host"], token, ip=destination_ip)
# Verify single host found for each IP
verify_single_host(source_host, source_ip)
verify_single_host(destination_host, destination_ip)
# Print Out Host details
print("Source Host Details:")
print("-" * 25)
print_host_details(source_host[0])
print("Destination Host Details:")
print("-" * 25)
print_host_details(destination_host[0])
# Step 2: Where are they in the network?
# Retrieve and Print Source Device Details from dnac
source_host_net_device = network_device_list(
dnac["host"], token, id=source_host[0]["connectedNetworkDeviceId"]
) # noqa: E501
print("Source Host Network Connection Details:")
print("-" * 45)
print_network_device_details(source_host_net_device[0])
# If Host is wired, collect interface details
if source_host[0]["hostType"] == "wired":
source_host_interface = interface_details(
dnac["host"], token, id=source_host[0]["connectedInterfaceId"]
) # noqa: E501
print("Attached Interface:")
print("-" * 20)
print_interface_details(source_host_interface)
destination_host_net_device = network_device_list(
dnac["host"], token, id=destination_host[0]["connectedNetworkDeviceId"]
) # noqa: E501
print("Destination Host Network Connection Details:")
print("-" * 45)
print_network_device_details(destination_host_net_device[0])
# If Host is wired, collect interface details
if destination_host[0]["hostType"] == "wired":
destination_host_interface = interface_details(
dnac["host"], token, id=destination_host[0]["connectedInterfaceId"]
) # noqa: E501
print("Attached Interface:")
print("-" * 20)
print_interface_details(destination_host_interface)