我目前正在编写一个脚本,从我们的Cisco Prime基础设施中获取网络设备的完整列表,然后将这些设备添加到Cisco ISE安装中。你知道吗
我们的Cisco Prime基础设施的输出数据超过25000行作为文本。你知道吗
输出示例:
<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>
现在我只想将以entity开始并以/entity结束的行保存到列表中,每个部分都应该是自己的列表。你知道吗
示例:
[['<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>']]
我的当前代码:
#!/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)
但结果很奇怪。我现在不知道怎么解决这个问题。如果能帮上忙,我们将不胜感激。你知道吗
编辑:
我可以看到每行的末尾都有一个\n。如果我这样做:
output = output.split("\n")
我得到一个包含所有数据的列表。你知道吗
您可以采取两种方法:
第一次进近:逐行如果您想让代码逐行运行,可以使用regex检查代码行是否符合您的要求:
这将匹配“any text(like<;)entityany text/entityany text(like>;)”所在的每一行 但是,这将只捕获单行实体。或者,您可以查找带有起始标记和开始标记的行,并捕获中间的内容:
第二种方法:XML如果您真的想把它当作一个XML来对待,那么不仅可以查找单行的实体对象,还可以使用xml.etree.ElementTree运行递归findall,如here所述来查找每个实体元素。你知道吗
相关问题 更多 >
编程相关推荐