cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
759
Views
0
Helpful
1
Replies

how to to get access to command runner payload

Good morning.

i have been trying to utilize the command runner api with the dnacentersdk.  

i been following the tutorial here: https://blogs.cisco.com/developer/network-automation-cisco-dna-center-sdk-2

but the file does to download anywhere to my system, and i am not sure where to access it in DNA center.  

here is the code i am using:

 

 

import json
from rich.console import Console
from dnacentersdk import DNACenterAPI
from dnacentersdk.exceptions import ApiError



dnac = DNACenterAPI(username= DNAC_USER, password= DNAC_PASS, base_url= DNAC_URL, verify=False)
myConsole = Console()


def cmd_run(device_list
    for device in device_list:
        print("Executing Command on {}".format(device))
        run_cmd = dnac.command_runner.run_read_only_commands_on_devices(commands=["show run"], deviceUuids=[device])
        print("Task started! Task ID is {}".format(run_cmd.response.taskId))
        task_info = dnac.task.get_task_by_id(run_cmd.response.taskId)
        task_progress = task_info.response.progress
        print("Task Status : {}".format(task_progress))
        while task_progress == 'CLI Runner request creation':
            task_progress = dnac.task.get_task_by_id(run_cmd.response.taskId).response.progress

        task_progress= json.loads(task_progress)

        cmd_output = dnac.file.download_a_file_by_fileid(task_progress['fileId'], dirpath='C:/Users/bussardm/OneDrive - Oakland County/Documents/Python scripts/file.json')

        myConsole.print(cmd_output)

        print("Saving config for device ... \n")

devices = dnac.devices.get_device_list(management_ip_address='10.255.47.1')
devicesuid_list = []
for device in devices.response:
    if device.family == 'Switches and Hubs':
        devicesuid_list.append(device.id)
        print("Device ID: {} ".format(device.id))
    print("Device Management IP: {} ".format(device.managementIpAddress))
    cmd_run(devicesuid_list)

 

 

1 Reply 1

Alexander Stevenson
Cisco Employee
Cisco Employee

Hi @OaklandCounty_Mike,

 

 For me, to get it to download, I had to also use the save_file parameter and not specify a filename in the dirpath, like so:

 

cmd_output = dnac.file.download_a_file_by_fileid(task_progress['fileId'], save_file=True, dirpath="/Users/alexstev/Desktop/temp/")

When I ran the above code I can see the files created in the local folder

 

alexstev_0-1667327311415.png

 

Here's how the docs explain those parameters:

download_a_file_by_fileid(file_id, dirpath=None, save_file=None, filename=None, headers=None, **request_parameters)[source]¶
Downloads a file specified by fileId.

Parameters
file_id (basestring) – File Identification number.
dirpath (basestring) – Directory absolute path. Defaults to os.getcwd().
save_file (bool) – Enable or disable automatic file creation of raw response.
filename (basestring) – The filename used to save the download file.
headers (dict) – Dictionary of HTTP Headers to send with the Request .
**request_parameters – Additional request parameters (provides support for parameters that may be added in the future).

source: https://dnacentersdk.readthedocs.io/en/latest/api/api.html

 

I'm not sure how (or even if) we can access that local file in the DNA Center GUI. You can try out the command runner in the GUI here: https://sandboxdnac.cisco.com/dna/tools/command-runner

You can also use the DevNet sandboxes for this. Currently, we have two DNA Center sandboxes. That's what I used.

https://devnetsandbox.cisco.com/RM/Topology

 

alexstev_1-1667327878604.png