1

我正在尝试为 Junos 界面创建自定义表/视图。是的,我知道它已经作为内置/默认表存在,但这是我计划稍后扩展的学习练习。

问题是我只想获取inet系列地址,因此只有在address-family节包含inet的 address-family/address-family-name时才是ifa-local

有没有办法从我的视图 YAML 中执行此操作,或者我是否必须用我的视图获取所有地址系列节并以编程方式仅将inet节保留在我的 python 脚本中?那将如何运作?

这是 XML 结构(为了清楚起见,删除了不重要的部分):

<rpc-reply xmlns:junos="http://xml.juniper.net/junos/15.1F6/junos">
    <interface-information xmlns="http://xml.juniper.net/junos/15.1F6/junos-interface" junos:style="normal">
        <physical-interface>
            <name>ge-0/0/9</name>
            <logical-interface>
                <name>ge-0/0/9.0</name>
                <address-family>
                    <address-family-name>inet</address-family-name>
                    <mtu>3986</mtu>
                    <route-table>0</route-table>
                    <interface-address>
                        <ifa-destination>10.0.2.88/30</ifa-destination>
                        <ifa-local>10.0.2.89</ifa-local>
                        <ifa-broadcast>10.0.2.91</ifa-broadcast>
                    </interface-address>
                </address-family>
                <address-family>
                    <address-family-name>iso</address-family-name>
                    <mtu>3983</mtu>
                    <route-table>0</route-table>
                </address-family>
            </logical-interface>
        </physical-interface>
    </interface-information>
</rpc-reply>

这是我的 YAML:

JunosDCDiscoveryInterfaces:
    rpc: get-interface-information
    item: physical-interface/logical-interface
    key: name
    view: JunosDCDiscoveryInterfacesView

JunosDCDiscoveryInterfacesView:
    fields:
        name: name
        description: description
        active: { if-config-flags/iff-up : flag }
        # ipv4_address only if address-family/address-family-name is 'inet'
        ipv4_address: {address-family/interface-address/ifa-local}

最后,这是我用来在路由器上显示每个接口的 python 代码:

logical_interfaces = JunosDCDiscoveryInterfaces(client.device).get()
for logical_interface in logical_interfaces:
    print(f'{logical_interface.name} {logical_interface.active} {logical_interface.description} {logical_interface.ipv4_address}')
4

0 回答 0