-
Notifications
You must be signed in to change notification settings - Fork 1
/
intro_to_dna_center.py
66 lines (48 loc) · 1.95 KB
/
intro_to_dna_center.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
#!/usr/bin/env python3
# developed by Gabi Zapodeanu, TSA, GPO, Cisco Systems
import json
import utils
import requests
import urllib3
from requests.auth import HTTPBasicAuth # for Basic Auth
from urllib3.exceptions import InsecureRequestWarning # for insecure https warnings
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 get_dnac_jwt_token(dnac_auth):
"""
Create the authorization token required to access DNA C
Call to DNA C - /api/system/v1/auth/login
:param dnac_auth - DNA C Basic Auth string
:return: DNA C JWT token
"""
url = DNAC_URL + '/api/system/v1/auth/login'
header = {'content-type': 'application/json'}
response = requests.get(url, auth=dnac_auth, headers=header, verify=False)
response_header = response.headers
dnac_jwt_token = response_header['Set-Cookie']
return dnac_jwt_token
def get_client_info(client_ip, dnac_jwt_token):
"""
This function will retrieve all the information from the client with the IP address
:param client_ip: client IPv4 address
:param dnac_jwt_token: DNA C token
:return: client info, or {None} if client does not found
"""
url = DNAC_URL + '/api/v1/host?hostIp=' + client_ip
header = {'content-type': 'application/json', 'Cookie': dnac_jwt_token}
response = requests.get(url, headers=header, verify=False)
client_json = response.json()
try:
client_info = client_json['response'][0]
return client_info
except:
return None
# get the DNA Center JWT auth
dnac_jwt_auth = get_dnac_jwt_token(DNAC_AUTH)
print('The DNA Center Auth JWT is: ', dnac_jwt_auth)
# get the client info
client_ip_add = '10.93.130.33'
client_detail = get_client_info(client_ip_add, dnac_jwt_auth)
print('The information for the client with the IP address: ', client_ip_add)
utils.pprint(client_detail)