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

DNAC API network-device

Netmart
Level 1
Level 1

Hello, 

Instead of printing the whole network device table, I only wanted to search/filter for certain devices containing a certain string like  "rpvg" which are part of the hostname. Below you will find an extract of my code. The if statement is intended to filter for a certain string like: if item["hostname"] == <'containing string 'rpvg'>:

Note sure if could utilize the "re" module and set up a filter with regular expression 

Thanks,

 

 

def get_network_device(token
 #my_access_token=my_tkn_req.json()["Token"]
 my_access_token=token
 my_url="https://"+my_dna_ctr+"/dna/intent/api/v1/network-device"
 my_headers={"x-auth-token":my_access_token,
            "Content-Type": "application/json"}
 resp = requests.get(my_url, headers=my_headers,verify=False)
 network_device = resp.json()
 dnac_devices = PrettyTable(["Hostname", "Management Ip Address", "Serial Number",
 "platform Id", "Software Type", "Software Version","Role", "Up Time","Id"])
 dnac_devices.padding_width = 1
 for item in network_device['response']:
    if item["hostname"] == <'containing string 'rpvg'>:
        dnac_devices.add_row([item["hostname"], item["managementIpAddress"],
        item["serialNumber"], item["platformId"],item["softwareType"],
        item["softwareVersion"], item["role"], item["upTime"],item["id"]])
 print('\nList of Devices in DNA-\n', dnac_device
1 Reply 1

Torbjørn
Spotlight
Spotlight

The easiest way to achieve this is to use the "in" operator:

 

....
if "rpgv" in item["hostname"]:
....

 

 

Happy to help! Please mark as helpful/solution if applicable.
Get in touch: https://torbjorn.dev