使用PyEZ表获取条件数据

时间:2018-12-26 18:30:33

标签: python pyez

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

问题是我只想获取 inet 家庭地址,所以只有地址家庭,这才是 ifa-local >节包含 inet address-family / address-family-name

是否可以从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}')

0 个答案:

没有答案