-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdevice_report.py
executable file
·132 lines (100 loc) · 4.28 KB
/
device_report.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Cisco DNA Center Managed Devices Report
Copyright (c) 2019 Cisco and/or its affiliates.
This software is licensed to you under the terms of the Cisco Sample
Code License, Version 1.1 (the "License"). You may obtain a copy of the
License at
https://developer.cisco.com/docs/licenses
All use of the material herein must be in accordance with the terms of
the License. All rights not expressly granted by the License are
reserved. Unless required by applicable law or agreed to separately in
writing, software distributed under the License is distributed on an "AS
IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
or implied.
"""
__author__ = "Gabriel Zapodeanu TME, ENB"
__email__ = "[email protected]"
__version__ = "0.1.0"
__copyright__ = "Copyright (c) 2019 Cisco and/or its affiliates."
__license__ = "Cisco Sample Code License, Version 1.1"
import requests
import json
import urllib3
import csv
from urllib3.exceptions import InsecureRequestWarning # for insecure https warnings
from requests.auth import HTTPBasicAuth # for Basic Auth
from config import DNAC_URL, DNAC_PASS, DNAC_USER
urllib3.disable_warnings(InsecureRequestWarning) # disable insecure https warnings
DNAC_AUTH = HTTPBasicAuth(DNAC_USER, DNAC_PASS)
def pprint(json_data):
"""
Pretty print JSON formatted data
:param json_data: data to pretty print
:return None
"""
print(json.dumps(json_data, indent=4, separators=(' , ', ' : ')))
def get_dnac_jwt_token(dnac_auth):
"""
Create the authorization token required to access Cisco DNA Center
Call to Cisco DNA Center - /api/system/v1/auth/login
:param dnac_auth - Cisco DNA Center Basic Auth string
:return Cisco DNA Center Auth Token
"""
url = DNAC_URL + '/dna/system/api/v1/auth/token'
header = {'content-type': 'application/json'}
response = requests.post(url, auth=dnac_auth, headers=header, verify=False)
response_json = response.json()
dnac_jwt_token = response_json['Token']
return dnac_jwt_token
def get_all_device_info(dnac_jwt_token):
"""
The function will return all network devices info
:param dnac_jwt_token: Cisco DNA Center token
:return: Cisco DNA Center device inventory info
"""
url = DNAC_URL + '/dna/intent/api/v1/network-device'
header = {'content-type': 'application/json', 'x-auth-token': dnac_jwt_token}
all_device_response = requests.get(url, headers=header, verify=False)
all_device_info = all_device_response.json()
return all_device_info['response']
def main():
"""
This script will create a device_report.csv file with information about the Cisco DNA Center managed devices:
This report will include device:
- hostname
- device type
- software version
- management IP address
- serial number
"""
print('')
# obtain the Cisco DNA Center Auth Token
dnac_token = get_dnac_jwt_token(DNAC_AUTH)
# get the all device details from Cisco DNA Center
all_device_list = get_all_device_info(dnac_token)
# verify if Cisco DNA Center manages any devices and how many
device_count = len(all_device_list)
if device_count == [0]:
print('\nCisco DNA Center does not manage any devices')
else:
print('\nCisco DNA Center manages this number of devices: ', device_count)
# retrieve the information about the devices and write to file
# save information to file
output_file = open('device_report.csv', 'w', newline='')
output_writer = csv.writer(output_file)
# loop through all devices list to collect the information needed in the report
for device in all_device_list:
device_hostname = device['hostname']
device_type = device['type']
device_software_version = device['softwareVersion']
device_management_ip = device['managementIpAddress']
device_sn = device['serialNumber']
device_info = [device_hostname, device_type, device_software_version, device_management_ip, device_sn]
output_writer.writerow(device_info)
output_file.close()
print('\n\nFile "device_report.csv" saved')
print('\n\nEnd of application "device_report.py" run')
if __name__ == "__main__":
main()