r/Juniper•
basondole

Help with xpath

I'm trying to create an xpath that I can use to iterate through all units of an interface. I have tried uusing path = "./interfaces/interface[starts-with(name,'ae0')]" but this only seems to catch the first unit and doesn't loop through the others remaining units.

for element in root.xpath("./protocols/isis/interface"):
    if_name = element.find('name').text
    path = "./interfaces/interface[starts-with(name,'ae0')]"
    for element in root.xpath(path):
        jcs.emit_warning("Adding debug: " + if_name + " " + element.find('name').text +"."+element.find('unit/name').text)

My ae0 has multiple units

# run show interfaces terse | match ^ae0 
ae0                     up    up   multiservice
ae0.50                  up    up   multiservice
ae0.73                  up    up   multiservice
ae0.666                 up    up   multiservice

When I tried to debug by running a loop, I only see ae0.50 (the last column) where I was expecting to see the other units as well

warning: Adding debug: ae0.50 ae0.50
warning: Adding debug: ae0.73 ae0.50
warning: Adding debug: ae0.666 ae0.50

I was expecting

warning: Adding debug: ae0.50 ae0.50
warning: Adding debug: ae0.50 ae0.73
warning: Adding debug: ae0.50 ae0.666
warning: Adding debug: ae0.73 ae0.50
warning: Adding debug: ae0.73 ae0.73
warning: Adding debug: ae0.73 ae0.666
warning: Adding debug: ae0.666 ae0.50
warning: Adding debug: ae0.666 ae0.73
warning: Adding debug: ae0.666 ae0.666

Posted May 25, 2021, 4:56 PM UTC

19
thejheadJNCIE•

I can't say for sure without seeing other details, but I think your issue is that you're not parsing what you think you are.

for element in root.xpath("./protocols/isis/interface"):

The XML you'd get back from looking at the IS-IS configuration using a path of './protocols/isis/interface' would be all interfaces configured for IS-IS. However, this won't get you a list of elements, but a single item.

Then you say:

if_name = element.find('name').text

.find will always get the first entry of that single item.

What you could try is something like this:

is_is_interfaces = is_is_xml_config.xpath("//interface")
for element in is_is_interfaces:
    print(element)

The "//" says find all interfaces. Then iterate over those interfaces to do whatever you wanted.

2
basondoleOP•

Thanks for the input. The isis part seems to be working fine as I get all the interfaces from the isis hierarchy, what is not working is when looping those interfaces against a list of interfaces from interface ae0, this is where only the ae0.50 is seen and the rest of the units are not seen. What xpath can I use if I want to iterate through all units of an interface ae0?

1
thejheadJNCIE•

Similar logic with the "//". Something like:

path = "//interfaces/interface[starts-with(name,'ae0')]"

i.e. "Find all interfaces that start with 'ae0'"

1
basondoleOP•

That's exactly what I have and it's failing as explained

path = "./interfaces/interface[starts-with(name,'ae0')]"

2
thejheadJNCIE•

I saw in your other comment that you got it working.

I just want to point out that what I mentioned isn't the same, notice the initial "//" rather than "./".

1
basondoleOP•

This path worked as expected

./interfaces/interface[name='ae0']/unit

Thank you

1
thejheadJNCIE•

Awesome, glad it was simpler than I what I was getting at!

1
basondoleOP•

How can specify inactive unit attribute in the xpath. For example the xpath ./interfaces/interface[name='ae2']/unit[name='52'] the interface ae2.52 is inactive as in deactivated in configuration. How can I specify the inactive attribute in the xpath. Refer the xml output below

#run show configuration interfaces ae2.52 | display xml 
<rpc-reply xmlns:junos="http://xml.juniper.net/junos/20.1R0/junos">
    <configuration junos:commit-seconds="1622024643" junos:commit-localtime="2021-05-26 10:24:03 UTC" junos:commit-user="paul">
            <interfaces>
                <interface>
                    <name>ae2</name>
                    <unit inactive="inactive">
                        <name>52</name>
1