/
cmd_runner.py
106 lines (88 loc) · 3.99 KB
/
cmd_runner.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
import requests
import json
import logging
from requests.auth import HTTPBasicAuth
import os
import sys
# 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
sys.path.insert(0, project_root)
import env_lab
DNAC_URL = env_lab.DNA_CENTER["host"]
DNAC_USER = env_lab.DNA_CENTER["username"]
DNAC_PASS = env_lab.DNA_CENTER["password"]
"""
This code snippet will run execute operational commands across your entire network using Cisco DNA Center
Command Runner APIs
"""
def get_auth_token():
"""
Building out Auth request. Using requests.post to make a call to the Auth Endpoint
"""
url = 'https://{}/dna/system/api/v1/auth/token'.format(DNAC_URL) # Endpoint URL
hdr = {'content-type' : 'application/json'} # Define request header
try:
logging.captureWarnings(True)
resp = requests.post(url, auth=HTTPBasicAuth(DNAC_USER, DNAC_PASS), headers=hdr, verify=False) # Make the POST Request
except Exception as e:
print("Error: ", e)
token = resp.json()['Token'] # Retrieve the Token
return token # Create a return statement to send the token back for later use
def get_device_list():
"""
Building out function to retrieve list of devices. Using requests.get to make a call to the network device Endpoint
"""
token = get_auth_token() # Get Token
url = "https://{}/api/v1/network-device/1/4".format(DNAC_URL)
hdr = {'x-auth-token': token, 'content-type' : 'application/json'}
logging.captureWarnings(True)
resp = requests.get(url, headers=hdr, verify=False) # Make the Get Request
device_list = resp.json()
print("{0:25}{1:25}".format("hostname", "id"))
for device in device_list['response']:
print("{0:25}{1:25}".format(device['hostname'], device['id']))
initiate_cmd_runner(token) # initiate command runner
def initiate_cmd_runner(token):
ios_cmd = "show ver | inc RELEASE"
device_id = str(input("Copy/Past a device ID here:"))
print("executing ios command -->", ios_cmd)
param = {
"name": "Show Command",
"commands": [ios_cmd],
"deviceUuids": [device_id]
}
url = "https://{}/api/v1/network-device-poller/cli/read-request".format(DNAC_URL)
header = {'content-type': 'application/json', 'x-auth-token': token}
logging.captureWarnings(True)
response = requests.post(url, data=json.dumps(param), headers=header, verify=False)
task_id = response.json()['response']['taskId']
print("Command runner Initiated! Task ID --> ", task_id)
print("Retrieving Path Trace Results.... ")
get_task_info(task_id, token)
def get_task_info(task_id, token):
url = "https://{}/api/v1/task/{}".format(DNAC_URL, task_id)
hdr = {'x-auth-token': token, 'content-type' : 'application/json'}
logging.captureWarnings(True)
task_result = requests.get(url, headers=hdr, verify=False)
file_id = task_result.json()['response']['progress']
if "fileId" in file_id:
unwanted_chars = '{"}'
for char in unwanted_chars:
file_id = file_id.replace(char, '')
file_id = file_id.split(':')
file_id = file_id[1]
print("File ID --> ", file_id)
else: # keep checking for task completion
get_task_info(task_id, token)
get_cmd_output(token, file_id)
def get_cmd_output(token,file_id):
url = "https://{}/api/v1/file/{}".format(DNAC_URL, file_id)
hdr = {'x-auth-token': token, 'content-type': 'application/json'}
logging.captureWarnings(True)
cmd_result = requests.get(url, headers=hdr, verify=False)
print(json.dumps(cmd_result.json(), indent=4, sort_keys=True))
if __name__ == "__main__":
get_device_list()