Automation

Expand all | Collapse all

Python PyEZ script Extract of ISIS database

Jump to Best Answer
  • 1.  Python PyEZ script Extract of ISIS database

    Posted 03-11-2019 01:42

    Hi Junos Experts,

     

    I'm testing a script to extract ISIS database from Junos as follows

    dev.open()
    dev.timeout = 60
    result = dev.rpc.get_isis_database_information (normalize=True, detail=True)
    
    for entry in result.xpath("isis-database[level='2']/isis- database-entry"):
        lspid = entry.findtext("lsp-id")
        node=lspid[:-3]
        if not node.endswith(".00"):
           node="_"+node;
        if not node in graph['nodes']:
           graph['nodes'].append(node)
        for neighbor in entry.xpath("isis-neighbor"):
           neighborid = neighbor.findtext("is-neighbor-id")
           metric = neighbor.findtext("metric")
           topology = neighbor.findtext("isis-topology-id")
           if topology=="" or topology=="IPV4 Unicast":
              if not neighborid.endswith(".00"):
                 neighborid="_"+neighborid;
              if not neighborid in graph['nodes']:
                 graph['nodes'].append(neighborid)
              graph['links'].append([node, neighborid, metric])
    
    dev.close()

    When I run the script it return me error as below

    Traceback (most recent call last):
      File "pyezjunipertest.py", line 39, in <module>
        for entry in result.xpath("isis-database[level='2']/isis- database-entry"):
      File "lxml.etree.pyx", line 1509, in lxml.etree._Element.xpath (src/lxml/lxml.etree.c:50717)
      File "xpath.pxi", line 318, in lxml.etree.XPathElementEvaluator.__call__ (src/lxml/lxml.etree.c:145969)
      File "xpath.pxi", line 238, in lxml.etree._XPathEvaluatorBase._handle_result (src/lxml/lxml.etree.c:144977)
      File "xpath.pxi", line 224, in lxml.etree._XPathEvaluatorBase._raise_eval_error (src/lxml/lxml.etree.c:144832)
    lxml.etree.XPathEvalError: Invalid expression

    and this is the snippet of xml ouput from router terminal console

    <rpc-reply xmlns:junos="http://xml.juniper.net/junos/15.1R7/junos">
        <isis-database-information xmlns="http://xml.juniper.net/junos/15.1R7/junos-routing" junos:style="brief">
            <isis-database>
                <level>1</level>
                <lsp-count>0</lsp-count>
            </isis-database>
            <isis-database>
                <level>2</level>
                <isis-database-entry>
                    <lsp-id>core1-re0.00-00</lsp-id>
                    <sequence-number>0x35022</sequence-number>
                    <checksum>0x925c</checksum>
                    <remaining-lifetime>736</remaining-lifetime>
                    <lsp-attributes>L1 L2</lsp-attributes>
                </isis-database-entry>
                <isis-database-entry>
                    <lsp-id>core2-re0.00-00</lsp-id>
                    <sequence-number>0x347e2</sequence-number>
                    <checksum>0x322e</checksum>
                    <remaining-lifetime>1175</remaining-lifetime>
                    <lsp-attributes>L1 L2</lsp-attributes>
                </isis-database-entry>

     

    May I know would could be the issue here? I really appreciate some one could assist me further 

    Thank you.

     



  • 2.  RE: Python PyEZ script Extract of ISIS database
    Best Answer

     
    Posted 03-11-2019 02:39

    Hi jar,

     

    It seems you have a typo in isis-database-entry (an extra whitespace):

    for entry in result.xpath("isis-database[level='2']/isis- database-entry"): 

    Please remove it and check again.

     

    Best regards,

    Sergii

    -------------------------------------------------------------------

    Please accept the solution if your problem is resolved Smiley Wink

    -------------------------------------------------------------------



  • 3.  RE: Python PyEZ script Extract of ISIS database

    Posted 03-11-2019 05:28

    Hi sir,

    Yup..u spot it sir...thank you.

    I have run the script again and return the ouput but...it just list the nodes but no links listed

    '{"nodes": ["core1.re0.00", "core2.re0.00", "ms1.RE0.00", "ms2.RE0.00", "ms3.RE0.00", "ms4.RE0.00", "ipx1.RE0.00", "ipx2.RE0.00", "ib01.cx.00"], "links": []}'

    Do i miss anything here?



  • 4.  RE: Python PyEZ script Extract of ISIS database

     
    Posted 03-11-2019 06:09

    You can try removing this line:

    if topology=="" or topology=="IPV4 Unicast":

    Then, if your graph['links'] gets populated, check why this condition wasn't True.

     

    Best regards,

    Sergii

    -------------------------------------------------------------------

    Please accept the solution if your problem is resolved Smiley Wink

    -------------------------------------------------------------------



  • 5.  RE: Python PyEZ script Extract of ISIS database

    Posted 03-11-2019 06:37

    Yup....the links listed..but i still not sure why it populated after remove below line...

    #if topology=="" or topology=="IPV4 Unicast":

    Thank you sir...



  • 6.  RE: Python PyEZ script Extract of ISIS database

     
    Posted 03-11-2019 07:02

    Can you please add a condition "if topology is None" and re-test? If there are no elements that match "topology", findtext will return None by default.

    Reference - http://infohost.nmt.edu/~shipman/soft/pylxml/web/Element-findtext.html

     

    Best regards,

    Sergii

    ----------------------------------------------------------------------------------------

    Please mark this post as "Accepted solution" if your problem is resolved Smiley Wink

    ----------------------------------------------------------------------------------------



  • 7.  RE: Python PyEZ script Extract of ISIS database

    Posted 03-12-2019 22:59

    Hi sir..it return values of link...thus...i add None in the code line..Thanks