python查找两行之间的字符串,cisco routemap形式

2024-03-29 08:04:54 发布

您现在位置:Python中文网/ 问答频道 /正文

route-map other1 permit 10
route-map abc permit 10
match ip address prefix-list test
route-map abc deny 20
match ip address prefix-list test2
route-map abc permit 30
route-map other2 permit 10

如何找到路线图abc及其相关配置,如:

route-map abc permit 10
match ip address prefix-list test
route-map abc deny 20
match ip address prefix-list test2
route-map abc permit 30

基本上,它总是以“路线图abc”开始,以“路线图abc”结束,但是线路长度和“许可证30”是可变的,我如何找到两者之间的一切


Tags: testipmapprefixaddressmatchroutelist
0条回答
网友
1楼 · 发布于 2024-03-29 08:04:54

关于这一点:我们的想法是在路线图上触发abc并在任何其他id上关闭它。只要它被触发,读取行就会被记录

with open("route-map.txt", 'r') as f:
    routelog = f.readlines()

searchEntry = 'abc'
selected = False
outlog = []
for line in routelog:
    words = line.split()
    # probably there are other ways to find the trigger
    if len(words) > 1 and 'route-map' in words[0]:
        if searchEntry in words[1]:
            selected = True
        else:
            selected = False
    if selected:
        outlog.append(line)

print(outlog)

相关问题 更多 >