-
Notifications
You must be signed in to change notification settings - Fork 291
Expand file tree
/
Copy pathdnac.py
More file actions
executable file
·126 lines (101 loc) · 4.7 KB
/
Copy pathdnac.py
File metadata and controls
executable file
·126 lines (101 loc) · 4.7 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
#!/usr/bin/env python
"""Verify the Cisco DNA Center APIs are accessible and responding.
Verify that DNA Center Sanbox Northbound APIs are accessible and
responding with data.
Copyright (c) 2018 Cisco and/or its affiliates.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""
import os
import sys
import requests
# Disable Certificate warning
try:
requests.packages.urllib3.disable_warnings()
except:
pass
from requests.auth import HTTPBasicAuth
# Get the absolute path for the directory where this file is located "here"
here = os.path.abspath(os.path.dirname(__file__))
# Get the absolute path for the project / repository root
project_root = os.path.abspath(os.path.join(here, "../.."))
# Extend the system path to include the project root and import the env files
if project_root not in sys.path:
sys.path.insert(0, project_root)
import env_lab # noqa
def dnac_get_device_count(dnac_session, dnac_host, dnac_headers):
"""DNAC Network Device Count"""
tmp_url = 'https://%s/api/v1/network-device/count' % dnac_host
r = dnac_session.get(tmp_url, verify=False, headers=dnac_headers)
r.raise_for_status()
return r.json()['response']
def dnac_get_host_count(dnac_session, dnac_host, dnac_headers):
"""DNAC Host Count"""
tmp_url = 'https://%s/api/v1/host/count' % dnac_host
r = dnac_session.get(tmp_url, verify=False, headers=dnac_headers)
r.raise_for_status()
# print('DNAC Response Body: ' + r.text)
return r.json()['response']
def verify() -> bool:
"""Verify access to the Cisco DNAC APIs."""
print('\n\n==> Verifying access to the Cisco DNA Center Northbound APIs')
# Verify env_lab is accessible and complete for DNAC
if not env_lab.DNA_CENTER['host']:
print('FAILED: You must provide DNAC hostname in the env_lab.py file.')
return False
if not env_lab.DNA_CENTER['username']:
print('FAILED: You must provide DNAC username in the env_lab.py file.')
return False
if not env_lab.DNA_CENTER['password']:
print('FAILED: You must provide DNAC password in the env_lab.py file.')
return False
dnac_host = env_lab.DNA_CENTER['host']
dnac_user = env_lab.DNA_CENTER['username']
dnac_pass = env_lab.DNA_CENTER['password']
dnac_headers = {'content-type': 'application/json'}
# Verify DNA Center login
with requests.Session() as dnac_session:
try:
dnac_auth_api='https://%s/api/system/v1/auth/login' % dnac_host
r = dnac_session.get(dnac_auth_api,
verify=False,
headers=dnac_headers,
auth=HTTPBasicAuth(dnac_user, dnac_pass))
r.raise_for_status()
session_token_val = ((r.headers['Set-Cookie']).split('=')[1]).split(';')[0]
cookies = {"X-JWT-ACCESS-TOKEN" : session_token_val}
dnac_session.cookies.update(cookies)
except:
print('FAILED: Not able to login to DNA Center at ' + dnac_host)
return False
else:
print('You are connected to to DNA Center at ' + dnac_host)
# Verify DNA Center inventory has some network devices and hosts
try:
c = dnac_get_device_count(dnac_session, dnac_host, dnac_headers)
h = dnac_get_host_count(dnac_session, dnac_host, dnac_headers)
except:
print('FAILED: No host and device invetory in DNA Center')
return False
else:
print('DNA Center Network Device Count: ' + str(c))
print('DNA Center Host Count: ' + str(h))
if c <= 0 or h <=0:
print('FAILED: No host and device invetory in DNA Center')
return False
return True
if __name__ == '__main__':
verify()