Retrieving IP addresses of interfaces using table/view

134 views
Skip to first unread message

ragsboss

unread,
Jul 23, 2018, 7:18:24 PM7/23/18
to Junos Python EZ
I'm trying to get IP addresses of all interfaces along with other per-interface details like speed etc. For this I'm using following table-view spec and unable to get the IP. I used the same XPATH on output of RPC and am able to get the IPs..

        {
            'InterfaceStatusTable': {
                'rpc': 'get-interface-information',
                'item': 'physical-interface',
                'view': 'InterfaceStatusView',
            },
            'InterfaceStatusView': {
                'fields': {
                    'description': 'description',
                    'speed': 'speed',
                    'ipv4': 'logical-interface[1]/address-family[address-family-name="\ninet\n"]/interface-address[1]/ifa-local',
                    'ipv4_broadcast': 'logical-interface[1]/address-family[address-family-name="\ninet\n"]/interface-address[1]/ifa-broadcast',
                }
            }
        }
gives me
[('gr-0/0/0', [('ipv4_broadcast', None), ('speed', '800mbps'), ('description', None), ('ipv4', None)]), ('pfe-0/0/0', [('ipv4_broadcast', None), ('speed', None), ('description', None), ('ipv4', None)]), ('pfh-0/0/0', [('ipv4_broadcast', None), ('speed', None), ('description', None), ('ipv4', None)]), ('xe-0/0/0', [('ipv4_broadcast', None), ('speed', '10Gbps'), ('description', 'intf-desc'), ('ipv4', None)]), ('xe-0/0/1', [('ipv4_broadcast', None)..]


The same XPATH done interactively in Python works
>>> print [etree.tostring(i) for i in ir.xpath('physical-interface/logical-interface[1]/address-family[address-family-name="\ninet\n"]/interface-address[1]/ifa-local')]
['<ifa-local>\n10.1.2.1\n</ifa-local>\n', '<ifa-local>\n128.0.0.1\n</ifa-local>\n', '<ifa-local>\n172.28.128.10\n</ifa-local>\n', '<ifa-local>\n169.254.0.2\n</ifa-local>\n', '<ifa-local>\n192.168.1.2\n</ifa-local>\n', '<ifa-local>\n192.0.2.2\n</ifa-local>\n', '<ifa-local>\n128.0.0.127\n</ifa-local>\n']

I'd also like to know if there is any better way to do this. Ex: avoid \n in search..

Appreciate any input

Thanks Rags

Stacy Smith

unread,
Jul 23, 2018, 7:34:59 PM7/23/18
to Junos Python EZ
On Monday, July 23, 2018 at 5:18:24 PM UTC-6, ragsboss wrote:
I'd also like to know if there is any better way to do this. Ex: avoid \n in search..

I believe that's exactly why your ipv4 and ipv4_broadcast fields are not matching. PyEZ automatically removes leading and trailing whitespace from XML values when using Op Tables/Views. Try removing the \n's from your XPaths.

PyEZ also allows you to enable this whitespace normalization for all RPC responses by passing the normalize=True parameter to the Device() instantiation. See the docs. I personally prefer to always enable this whitespace normalization. It only removes leading and trailing whitespace from XML values. It does not remove internal whitespace within the value.

Reply all
Reply to author
Forward
0 new messages