我目前正在处理一个脚本,该脚本从我们的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")
我得到一个包含所有数据的列表。
答案 0 :(得分:1)
您可以采取两种方法:
第一种方法:逐行如果要逐行运行代码,可以使用regex检查行是否符合要求:
for line in output:
if re.match(r'entity.*/entity', line):
print("It's a match")
这将与您拥有“任何文本(例如<)实体任何文本 / entity 任何文本(例如>)”的每一行匹配 但是,这只会捕获单行实体。另外,您可以查找带有开始标签和开始标签的行,并在以下两者之间捕获内容:
for line in output:
if re.match(r'entity', line):
if re.match(r'/entity', line):
print("End of block")
else:
print("Start of block")
第二种方法:XML
如果您想将其实际视为XML,不仅可以查找单行 entity 对象,还可以使用xml.etree.ElementTree并按照here的描述运行递归findall。查找每个实体元素。