NetworkAutomation / jaide

Junos Aide - A Tool for Network Administrators
GNU General Public License v2.0
21 stars 8 forks source link

change in XML emitted by Junos 14.2 breaks 'info' functionality #27

Open jeffbrl opened 8 years ago

jeffbrl commented 8 years ago

Using the 'info' command line parameter yields no information for devices running 14.2. I see that the XML has changed.

13.3

<rpc-reply xmlns:junos="http://xml.juniper.net/junos/13.3X8/junos">
    <software-information>
        <host-name>router1</host-name>
        <product-model>mx240</product-model>
        <product-name>mx240</product-name>
        <junos-version>13.3X8</junos-version>
        <package-information>
            <name>junos</name>
            <comment>JUNOS Base OS boot [13.3X8]</comment>
        </package-information>

14.2

<rpc-reply xmlns:junos="http://xml.juniper.net/junos/14.2R4/junos">
    <software-information>
        <host-name>router2</host-name>
        <product-model>mx240</product-model>
        <product-name>mx240</product-name>
        <package-information>
            <name>junos-version</name>
            <comment>Junos: 14.2R4-S4.2</comment>
        </package-information>
       <package-information>
            <name>junos</name>
            <comment>JUNOS Base OS boot [14.2R4-S4.2]</comment>
        </package-information>

I suspect that the break occurs in core.py:498. The first package-information does not have square brackets in the comment.

I was hoping to write the fix myself but I'm having problems debugging. When I add "import pdb; pdb.set_trace()" to the code, the pdb prompt is displayed before the program exits.

If anyone can provide debugging tips, I can probably fix this and submit a PR.

jeffbrl commented 8 years ago

I'll start by thanking @geoffrhodes for pointing out that debugging with pdb is simple using the Jaide class rather than the CLI.

Here's some test code I wrote that borrows the logic from py-junos-eznc.

   # try looking for 'junos-version' for >= 15.1
    for element in tree.xpath('//software-information'):
        software_version = element.findtext('junos-version')

    # try looking for version in "junos" package
    if not software_version:
        try:
            package_info = tree.xpath(
            '//software-information/package-information[normalize-space(name)="junos"]/comment'
             )[0].text
            software_version = re.findall(r'\[(.*)\]', package_info)[0]
        except IndexError:
            software_version = "unknown"

I'll integrate this code into core.py. I suspect other XML emission changes will break device_info for newer junos versions. I'll report back on what I find.

nprintz commented 8 years ago

Wasn't the issue present on code version >=14.2 as well? Might want to update the comment if this fixes it down to that version

jeffbrl commented 8 years ago

Yes, the comment should be >= 14.2. The pyez code comment is inaccurate based on what I'm observing. On Apr 30, 2016 2:58 PM, "Nathan Printz" notifications@github.com wrote:

Wasn't the issue present on code version >=14.2 as well? Might want to update the comment if this fixes it down to that version

— You are receiving this because you authored the thread. Reply to this email directly or view it on GitHub https://github.com/NetworkAutomation/jaide/issues/27#issuecomment-215987299

jeffbrl commented 8 years ago

I'm observing three different XML emissions for 'get-software-information()'. I converted to xpath as I trust its syntax. I could combine my 2 and your 3 at the danger of making the logic harder to follow.

Here is what I have now.

       version = 'Unknown'
        if resp.xpath('//junos-version'):
            """ case 1:
                <junos-version>15.1</junos-version>
            """
            try:
                version = resp.xpath('//junos-version')[0].text
            except IndexError:
                pass
        elif resp.xpath("//package-information[name = 'junos-version']"):
            """ case 2:
                <package-information>
                    <name>junos-version</name>
                    <comment>Junos: 14.2R4</comment>
                </package-information>
           """
            try:
                version =  (resp.xpath("//package-information[name = 'junos-version']/comment")[0].text).split()[1]
            except IndexError:
                pass
        else:
            """ case 3:
                <package-information>
                    <name>junos</name>
                    <comment>JUNOS Base OS boot [12.3R5]</comment>
                </package-information>
            """
            try:
                version = ((resp.xpath(
                '//software-information/package-information/comment')
                [0].text.split('[')[1].split(']')[0]))
            except IndexError:
                pass

I'll pep8-ify this and submit a PR unless anyone has comments or suggestions.