Automation

 View Only
last person joined: 6 days ago 

Ask questions and share experiences about Apstra, Paragon, and all things network automation.
  • 1.  Netconf RPC reply format

     
    Posted 01-30-2022 07:26
    Hello;
    I'm learning Automation and trying to use NetConf to retrieve information from Junos vMX device. I established a Netconf session from my CentOS machine to vMX and pasted the following RPC request:
    <rpc message-id="103" xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
     <get-interface-information>
       <terse/>
     </get-interface-information>
    </rpc>
    ]]>]]>​


    I got RPC Reply but the format of the message was not quiet readable like this:
    <rpc-reply xmlns:junos="http://xml.juniper.net/junos/19.4R0/junos" message-id="103" xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
    <interface-information xmlns="http://xml.juniper.net/junos/19.4R0/junos-interface" junos:style="terse">
    <physical-interface>
    <name>
    ge-0/0/0
    </name>
    <admin-status>
    up
    </admin-status>
    <oper-status>
    up
    </oper-status>
    <logical-interface>
    <name>
    ge-0/0/0.32767
    </name>
    <admin-status>
    up
    </admin-status>
    <oper-status>
    up
    </oper-status>
    <filter-information>
    </filter-information>
    <address-family>
    <address-family-name>
    multiservice
    </address-family-name>
    </address-family>
    </logical-interface>
    </physical-interface>
    <physical-interface>
    <name>
    gr-0/0/0
    <---output omitted--->


    I copied the code to a Prettify XML - Online XML Tools  website to make it better but again it did not change it a lot.
    <rpc-reply
      xmlns:junos="http://xml.juniper.net/junos/19.4R0/junos" message-id="103"
      xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
      <interface-information
        xmlns="http://xml.juniper.net/junos/19.4R0/junos-interface" junos:style="terse">
        <physical-interface>
          <name>
    ge-0/0/0
    </name>
          <admin-status>
    up
    </admin-status>
          <oper-status>
    up
    </oper-status>
          <logical-interface>
            <name>
    ge-0/0/0.32767
    </name>
            <admin-status>
    up
    </admin-status>
            <oper-status>
    up
    </oper-status>
            <filter-information></filter-information>
            <address-family>
              <address-family-name>
    multiservice
    </address-family-name>
            </address-family>
          </logical-interface>
        </physical-interface>
        <physical-interface>
          <name>
    gr-0/0/0
    </name>
          <admin-status>
    up
    </admin-status>
          <oper-status>
    up
    </oper-status>
          <logical-interface>
            <name>
    gr-0/0/0.0
    </name>
            <admin-status>
    up
    </admin-status>
            <oper-status>
    up
    </oper-status>
            <filter-information></filter-information>
            <address-family>
              <address-family-name>
    inet
    </address-family-name>
              <interface-address>
                <ifa-local>
    100.64.0.1
    </ifa-local>
                <ifa-destination junos:emit="emit">
    0/0
    </ifa-destination>
    <---output omitted--->​

    Is there any way to get mush more better format of the output? 
    And another question; Is it possible to use XPATH on the NetConf session to filter output? I searched internet but all I thing I found was using XPATH with Python or Perl.

    Regards;

    ------------------------------
    ISA GOK
    ------------------------------



  • 2.  RE: Netconf RPC reply format

     
    Posted 03-09-2022 17:08
    One option would be to use PyEz.

    You could normalize the data returned in a manner such as the following:

    from jnpr.junos import Device
    from lxml import etree
    
    with Device(host='router', user='foo', password='bar') as dev:
        if_info_rsp = dev.rpc.get_interface_information(terse=True, normalize=True)
        print(etree.tostring(if_info_rsp, encoding='unicode', pretty_print=True))​

    Regards,
    Andy

    ------------------------------
    Andy Sharp
    ------------------------------



  • 3.  RE: Netconf RPC reply format

     
    Posted 03-10-2022 19:52
    Hello ISA GOK,

    The device will return the structured data, but it's up to your computer to determine how to present it. As you've seen, your script may just dump it out without any pretty formatting.

    It would help us if you could share the important snippets of your code.

    At Juniper, we really like the lxml library, as it gives us a lot of capabilities when working with XML. If you are taking advantage of our PyEZ library, you'll find that lxml is included, giving you all the tools needed.

    pretty printing XML:
    tree = lxml.etree.parse("example.xml")
    pretty = lxml.etree.tostring(tree, encoding="unicode", pretty_print=True)
    
    print(pretty)
    
    """
    <root>
       <a>1</a>
       <b>2</b>
    </root>
    """​


    using XPATH and pretty printing with lxml

    from jnpr.junos import Device
    from lxml import etree
    
    with Device(host='dallas-fw0', user='automation', password='juniper123') as firewall:
        filter = '<configuration><interfaces><interface></interface></interfaces></configuration>'
        data = firewall.rpc.get_config(filter_xml=filter)
        print(etree.tostring(data, encoding='unicode', pretty_print=True))  
    

    Hope this helps

    ------------------------------
    Calvin Remsburg | Global Enterprise Architect | Juniper Networks
    ------------------------------