0

我有下面的 Junos 路由器配置部分。我正在尝试解析数据以显示下面的结果。我尝试了很多方法,但我遇到的问题是我似乎无法为每个接口(lo0.2 和 xe-1/0/2.1210)获得唯一的线路。我将非常感谢帮助理解实现所需输出的最佳方法。我正在尝试做的是解析这些路由器配置以在 excel 中的行中显示关键数据,这也将包括接口配置项。

我试图循环浏览配置并寻找匹配的行,但我永远无法同时获得这两个接口。只需要一些关于如何实现这一点的想法。

for line in config:
    if line.startswith('set routing-instances'):
        if 'options router-id' in line:
            self.router_id = line.split()[5]
        elif 'interface' in line:
            self.interface = line.split()[5]
        elif 'vrf-import' in line:
            self.vrf_import_policy = line.split()[4]
        elif 'vrf-export' in line:
            self.vrf_export_policy = line.split()[4]

    set routing-instances redwood-ca routing-options router-id 10.10.10.1
    set routing-instances redwood-ca routing-options autonomous-system 620
    set routing-instances redwood-ca routing-options autonomous-system independent-domain no-attrset
    set routing-instances redwood-ca protocols bgp path-selection always-compare-med
    set routing-instances redwood-ca protocols bgp path-selection external-router-id
    set routing-instances redwood-ca protocols bgp family inet unicast
    set routing-instances redwood-ca protocols bgp hold-time 30
    set routing-instances redwood-ca protocols bgp log-updown
    set routing-instances redwood-ca instance-type vrf
    set routing-instances redwood-ca interface lo0.2
    set routing-instances redwood-ca interface xe-1/0/2.1210
    set routing-instances redwood-ca route-distinguisher 10.10.10.1:100
    set routing-instances redwood-ca vrf-import redwood-ca-vrf-import
    set routing-instances redwood-ca vrf-export redwood-ca-vrf-export
    set routing-instances redwood-ca vrf-table-label
    
 
    Desired Output
    vrf         router-id       interface           vrf-import              vrf-export
    redwood-ca  10.10.10.1      lo0.2               redwood-ca-vrf-import   redwood-ca-vrf-export
    redwood-ca  10.10.10.1      xe-1/0/2.1210       redwood-ca-vrf-import   redwood-ca-vrf-export

感谢您提供的任何帮助。是否有将垂直类型数据转换为水平行的标准/最佳实践方法?

提前感谢大家的帮助。

麦克风

4

1 回答 1

0

您能否将界面设为列表并在每次找到时附加匹配项。

**interface = list()**
for line in config:
    if line.startswith('set routing-instances'):
        if 'options router-id' in line:
            router_id = line.split()[5]
        elif 'interface' in line:
            **interface.append(line.split()[4])**
        elif 'vrf-import' in line:
            vrf_import_policy = line.split()[4]
        elif 'vrf-export' in line:
            vrf_export_policy = line.split()[4]

print interface

输出 - ['lo0.2', 'xe-1/0/2.1210']

于 2021-07-06T19:55:37.380 回答