-
Notifications
You must be signed in to change notification settings - Fork 181
/
Copy pathreport-topology.py
114 lines (88 loc) · 4.66 KB
/
report-topology.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
# Example of building and reporting topology via REST API calls from Python
# * THIS SAMPLE APPLICATION AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
# * OF ANY KIND BY CISCO, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED
# * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY FITNESS FOR A PARTICULAR
# * PURPOSE, NONINFRINGEMENT, SATISFACTORY QUALITY OR ARISING FROM A COURSE OF
# * DEALING, LAW, USAGE, OR TRADE PRACTICE. CISCO TAKES NO RESPONSIBILITY
# * REGARDING ITS USAGE IN AN APPLICATION, AND IT IS PRESENTED ONLY AS AN
# * EXAMPLE. THE SAMPLE CODE HAS NOT BEEN THOROUGHLY TESTED AND IS PROVIDED AS AN
# * EXAMPLE ONLY, THEREFORE CISCO DOES NOT GUARANTEE OR MAKE ANY REPRESENTATIONS
# * REGARDING ITS RELIABILITY, SERVICEABILITY, OR FUNCTION. IN NO EVENT DOES
# * CISCO WARRANT THAT THE SOFTWARE IS ERROR FREE OR THAT CUSTOMER WILL BE ABLE
# * TO OPERATE THE SOFTWARE WITHOUT PROBLEMS OR INTERRUPTIONS. NOR DOES CISCO
# * WARRANT THAT THE SOFTWARE OR ANY EQUIPMENT ON WHICH THE SOFTWARE IS USED WILL
# * BE FREE OF VULNERABILITY TO INTRUSION OR ATTACK. THIS SAMPLE APPLICATION IS
# * NOT SUPPORTED BY CISCO IN ANY MANNER. CISCO DOES NOT ASSUME ANY LIABILITY
# * ARISING FROM THE USE OF THE APPLICATION. FURTHERMORE, IN NO EVENT SHALL CISCO
# * OR ITS SUPPLIERS BE LIABLE FOR ANY INCIDENTAL OR CONSEQUENTIAL DAMAGES, LOST
# * PROFITS, OR LOST DATA, OR ANY OTHER INDIRECT DAMAGES EVEN IF CISCO OR ITS
# * SUPPLIERS HAVE BEEN INFORMED OF THE POSSIBILITY THEREOF.-->
from spark import *
# import requests library
import requests
#import json library
import json
# Disable warnings
requests.packages.urllib3.disable_warnings()
controller='198.18.129.100'
def getTicket():
# put the ip address or dns of your apic-em controller in this url
url = "https://" + controller + "/api/v1/ticket"
#the username and password to access the APIC-EM Controller
#payload = {"username":"devnetuser","password":"Cisco123!"}
payload = {"username":"admin","password":"C1sco12345"}
#Content type must be included in the header
header = {"content-type": "application/json"}
#Performs a POST on the specified url to get the service ticket
response= requests.post(url,data=json.dumps(payload), headers=header, verify=False)
print (response)
#convert response to json format
r_json=response.json()
#parse the json to get the service ticket
ticket = r_json["response"]["serviceTicket"]
return ticket
def getTopology(ticket):
# URL for topology REST API call to get list of existing devices on the network, and build topology
url = "https://" + controller + "/api/v1/topology/physical-topology"
#Content type as well as the ticket must be included in the header
header = {"content-type": "application/json", "X-Auth-Token":ticket}
# this statement performs a GET on the specified network device url
response = requests.get(url, headers=header, verify=False)
# json.dumps serializes the json into a string and allows us to
# print the response in a 'pretty' format with indentation etc.
print ("Topology = ")
print (json.dumps(response.json(), indent=4, separators=(',', ': ')))
#convert data to json format.
r_json=response.json()
net_nodes=[]
#Iterate through network device data and list the nodes, their interfaces, status and to what they connect
for n in r_json["response"]["nodes"]:
if "platformId" in n:
print()
print()
print('{:30}'.format("Node") + '{:25}'.format("Family") + '{:20}'.format("Label")+ "Management IP")
print('{:30}'.format(n["platformId"]) + '{:25}'.format(n["family"]) + '{:20}'.format(n["label"]) + n["ip"])
net_nodes.append(n["label"])
found=0 #print header flag
for i in r_json["response"]["links"]:
if "startPortName" in i:
#check that the source device id for the interface matches the node id. Means interface originated from this device.
if i["source"] == n["id"]:
if found==0:
print('{:>20}'.format("Source Interface") + '{:>15}'.format("Target") +'{:>28}'.format("Target Interface") + '{:>15}'.format("Status") )
found=1
for n1 in r_json["response"]["nodes"]:
#find name of node to which this one connects
if i["target"] == n1["id"]:
print(" " + '{:<25}'.format(i["startPortName"]) + '{:<18}'.format(n1["platformId"]) + '{:<25}'.format(i["endPortName"]) + '{:<9}'.format(i["linkStatus"]) )
break
return net_nodes
theTicket=getTicket()
theNodes=getTopology(theTicket)
nodestr=""
for node in theNodes:
nodestr=nodestr + node + " "
setHeaders()
room_id=createRoom("Brett's Room")
addMembers(room_id) # Passing roomId to members function here to Post Message.
postMsg(room_id,"Network nodes are " + nodestr) # Passing roomId to message function here to Post Message.