I'm currently working on a script, that's getting a full list of network devices from our Cisco Prime Infrastructure and then adding these devices to our Cisco ISE installation.
The output data from our Cisco Prime Infrastructure is over 25000 lines as text.
Example output:
<entity dtoType="devicesDTO" type="Devices" url="https://X.X.X.X/webacs/api/v4/data/Devices/7860155308">
<devicesDTO displayName="7860155308" id="7860155308">
<collectionDetail><status><general code="SUCCESS"/></status></collectionDetail>
<collectionTime>2019-06-28T01:02:32.494+02:00</collectionTime>
<creationTime>2018-10-05T08:45:01.461+02:00</creationTime>
<deviceId>7849390591</deviceId>
<deviceName>008-HX.TEST.SWITCH-CORE-01</deviceName>
<deviceType>Cisco Catalyst38xx stack-able ethernet switch</deviceType>
<ipAddress>172.29.0.254</ipAddress>
<location>Test Location</location>
<managementStatus>MANAGED_AND_SYNCHRONIZED</managementStatus>
<productFamily>Switches and Hubs</productFamily>
<reachability>REACHABLE</reachability>
<softwareType>IOS-XE</softwareType>
<softwareVersion>16.6.5</softwareVersion>
<adminStatus>MANAGED</adminStatus>
<collectionStatus>COMPLETED</collectionStatus>
<manufacturerPartNrs>
<manufacturerPartNr>
<partNumber>WS-C3850-12XS-E</partNumber>
</manufacturerPartNr>
</manufacturerPartNrs>
</devicesDTO>
</entity>
<entity dtoType="devicesDTO" type="Devices" url="https://X.X.X.X/webacs/api/v4/data/Devices/7605631035">
<devicesDTO displayName="7605631035" id="7605631035">
<collectionDetail><status><general code="SUCCESS"/></status></collectionDetail>
<collectionTime>2019-06-28T00:45:33.177+02:00</collectionTime>
<creationTime>2018-09-04T01:48:15.790+02:00</creationTime>
<deviceId>7598542980</deviceId>
<deviceName>008-HX.TEST.SWITCH-UA-01.Vejle.dk</deviceName>
<deviceType>Cisco Catalyst 29xx Stack-able Ethernet Switch</deviceType>
<ipAddress>172.29.0.2</ipAddress>
<location>Test Location</location>
<managementStatus>MANAGED_AND_SYNCHRONIZED</managementStatus>
<productFamily>Switches and Hubs</productFamily>
<reachability>REACHABLE</reachability>
<softwareType>IOS</softwareType>
<softwareVersion>15.2(2)E9</softwareVersion>
<adminStatus>MANAGED</adminStatus>
<collectionStatus>COMPLETED</collectionStatus>
<manufacturerPartNrs>
<manufacturerPartNr>
<partNumber>WS-C2960X-48FPD-L</partNumber>
</manufacturerPartNr>
</manufacturerPartNrs>
</devicesDTO>
</entity>
<entity dtoType="devicesDTO" type="Devices" url="https://X.X.X.X/webacs/api/v4/data/Devices/7605631034">
<devicesDTO displayName="7605631034" id="7605631034">
<collectionDetail><status><general code="SUCCESS"/></status></collectionDetail>
<collectionTime>2019-06-28T00:46:35.393+02:00</collectionTime>
<creationTime>2018-09-04T01:48:00.776+02:00</creationTime>
<deviceId>7598542967</deviceId>
<deviceName>008-HX.TEST.SWITCH-UA-02.Vejle.dk</deviceName>
<deviceType>Cisco Catalyst 29xx Stack-able Ethernet Switch</deviceType>
<ipAddress>172.29.0.3</ipAddress>
<location>Test Location</location>
<managementStatus>MANAGED_AND_SYNCHRONIZED</managementStatus>
<productFamily>Switches and Hubs</productFamily>
<reachability>REACHABLE</reachability>
<softwareType>IOS</softwareType>
<softwareVersion>15.2(2)E9</softwareVersion>
<adminStatus>MANAGED</adminStatus>
<collectionStatus>COMPLETED</collectionStatus>
<manufacturerPartNrs>
<manufacturerPartNr>
<partNumber>WS-C2960X-48FPD-L</partNumber>
</manufacturerPartNr>
</manufacturerPartNrs>
</devicesDTO>
</entity>
<entity dtoType="devicesDTO" type="Devices" url="https://X.X.X.X/webacs/api/v4/data/Devices/7605631033">
<devicesDTO displayName="7605631033" id="7605631033">
<collectionDetail><status><general code="SUCCESS"/></status></collectionDetail>
<collectionTime>2019-06-28T00:47:01.412+02:00</collectionTime>
<creationTime>2018-09-04T01:47:45.817+02:00</creationTime>
<deviceId>7598542954</deviceId>
<deviceName>008-UX1.TEST.SWITCH-UA-01.Vejle.dk</deviceName>
<deviceType>Cisco Catalyst 29xx Stack-able Ethernet Switch</deviceType>
<ipAddress>172.29.0.4</ipAddress>
<location>Test Location</location>
<managementStatus>MANAGED_AND_SYNCHRONIZED</managementStatus>
<productFamily>Switches and Hubs</productFamily>
<reachability>REACHABLE</reachability>
<softwareType>IOS</softwareType>
<softwareVersion>15.2(2)E9</softwareVersion>
<adminStatus>MANAGED</adminStatus>
<collectionStatus>COMPLETED</collectionStatus>
<manufacturerPartNrs>
<manufacturerPartNr>
<partNumber>WS-C2960X-48FPD-L</partNumber>
</manufacturerPartNr>
</manufacturerPartNrs>
</devicesDTO>
</entity>
<entity dtoType="devicesDTO" type="Devices" url="https://X.X.X.X/webacs/api/v4/data/Devices/7605631038">
<devicesDTO displayName="7605631038" id="7605631038">
<collectionDetail><status><general code="SUCCESS"/></status></collectionDetail>
<collectionTime>2019-06-28T00:47:25.438+02:00</collectionTime>
<creationTime>2018-09-04T01:49:01.038+02:00</creationTime>
<deviceId>7598543019</deviceId>
<deviceName>008-UX2.TEST.SWITCH-UA-01.Vejle.dk</deviceName>
<deviceType>Cisco Catalyst 35xx Stack-able Ethernet Switch</deviceType>
<ipAddress>172.29.0.5</ipAddress>
<location>Test Location</location>
<managementStatus>MANAGED_AND_SYNCHRONIZED</managementStatus>
<productFamily>Switches and Hubs</productFamily>
<reachability>REACHABLE</reachability>
<softwareType>IOS</softwareType>
<softwareVersion>15.2(4)E7</softwareVersion>
<adminStatus>MANAGED</adminStatus>
<collectionStatus>COMPLETED</collectionStatus>
<manufacturerPartNrs>
<manufacturerPartNr>
<partNumber>WS-C3560CX-12PD-S</partNumber>
</manufacturerPartNr>
</manufacturerPartNrs>
</devicesDTO>
</entity>
<entity dtoType="devicesDTO" type="Devices" url="https://X.X.X.X/webacs/api/v4/data/Devices/7605631037">
<devicesDTO displayName="7605631037" id="7605631037">
<collectionDetail><status><general code="SUCCESS"/></status></collectionDetail>
<collectionTime>2019-06-28T00:45:32.775+02:00</collectionTime>
<creationTime>2018-09-04T01:48:45.760+02:00</creationTime>
<deviceId>7598543006</deviceId>
<deviceName>008-UX3.TEST.SWITCH-UA-01.Vejle.dk</deviceName>
<deviceType>Cisco Catalyst 29xx Stack-able Ethernet Switch</deviceType>
<ipAddress>172.29.0.6</ipAddress>
<location>Test Location</location>
<managementStatus>MANAGED_AND_SYNCHRONIZED</managementStatus>
<productFamily>Switches and Hubs</productFamily>
<reachability>REACHABLE</reachability>
<softwareType>IOS</softwareType>
<softwareVersion>15.2(2)E9</softwareVersion>
<adminStatus>MANAGED</adminStatus>
<collectionStatus>COMPLETED</collectionStatus>
<manufacturerPartNrs>
<manufacturerPartNr>
<partNumber>WS-C2960X-24PD-L</partNumber>
</manufacturerPartNr>
</manufacturerPartNrs>
</devicesDTO>
</entity>
Now i only want to save the lines starting with entity and ending with /entity into the list and each section should be a list of its own.
Example:
[['<entity dtoType="devicesDTO" type="Devices" url="https://X.X.X.X/webacs/api/v4/data/Devices/7860155308">', '...truncated...', '</entity>'], ['<entity dtoType="devicesDTO" type="Devices" url="https://X.X.X.X/webacs/api/v4/data/Devices/7605631035">', '...truncated...', '</entity>']]
My current code:
#!/usr/bin/python
# coding=utf-8
import sys
import requests
import xml.etree.ElementTree as ET
response = requests.get('https://X.X.X.X/webacs/api/v4/data/Devices?.full=true&.sort=deviceName', verify=False)
output = response.text
data = []
for line in output:
data.append(line)
But it's giving weird results. I cannot fathom how to solve this currently. Some help would be appreciated.
Edit:
I can see there's a \n at the end of every line. If i do this:
output = output.split("\n")
I get a single list containing all the data.