Getting device data from Prime with Python

Hello

I had to find out from Prime if a device is a stack or not, and how many devices there are if it is a stack, so I came up with this script:

First, the connection:

primeServer="<your_server.com"
primeCreds = HTTPBasicAuth('your_username','<your_password')     

url = "https://" + primeServer + "/webacs/api/v4/data/Devices?.full=true&.sort=ipAddress&deviceName=startsWith(%22" + device_name + "%22)"
response=requests.get(url, auth=primeCreds, verify=False)
obj = xmltodict.parse(response.text)
internal_obj = obj['queryResponse']
entity = internal_obj['entity']    

Now we need to account for the fact that there may be one or more results for ”startsWith”.

if isinstance(entity, list):          #if several results returned by Prime 
        print(f'Returning several objects for {device_name}')
        
        for item in entity:
           device = item['devicesDTO']
           local_name = device['deviceName']
           location = device['location']
           vc_dict = device['manufacturerPartNrs']
           internal_vc_dict = vc_dict['manufacturerPartNr']
           
           if isinstance(internal_vc_dict, list):        #if it is a stack
               vc_members = len(internal_vc_dict)
               print(f'stack size {local_name} in prime is {vc_members}')
 
           else:                                         # if it is not a stack
               vc_members = int(internal_vc_dict['name'])
              
else:                                   #if one result only
           device = entity['devicesDTO']
           vc_dict = device['manufacturerPartNrs']
           location = device['location']
           internal_vc_dict = vc_dict['manufacturerPartNr']
           if isinstance(internal_vc_dict, list):   #if it is a stack
               vc_members = len(internal_vc_dict)
               print(f'stack size {local_name} in prime is {vc_members}')

           else:                                            # if it is not a stack
               vc_members = int(internal_vc_dict['name'])
               print('not a stack')

     return vc_members

Skomentuj

Wprowadź swoje dane lub kliknij jedną z tych ikon, aby się zalogować:

Logo WordPress.com

Komentujesz korzystając z konta WordPress.com. Wyloguj /  Zmień )

Zdjęcie na Google

Komentujesz korzystając z konta Google. Wyloguj /  Zmień )

Zdjęcie z Twittera

Komentujesz korzystając z konta Twitter. Wyloguj /  Zmień )

Zdjęcie na Facebooku

Komentujesz korzystając z konta Facebook. Wyloguj /  Zmień )

Połączenie z %s