Automation

 View Only

IMPORTANT MODERATION NOTICE

This community is currently under full moderation, meaning  all posts will be reviewed before appearing in the community. Please expect a brief delay—there is no need to post multiple times. If your post is rejected, you'll receive an email outlining the reason(s). We've implemented full moderation to control spam. Thank you for your patience and participation.



  • 1.  PyEZ table with duplicate fields

    Posted 09-16-2015 23:33

    Hi all,

     

    I'm trying to figure out how to create a YAML table for the output of "show multicast route" which can have multiple duplicate fields for the downstream interface. For e.g.

     

    <rpc-reply xmlns:junos="http://xml.juniper.net/junos/13.3R3/junos">
        <multicast-route-information xmlns="http://xml.juniper.net/junos/13.3R3/junos-routing">
            <route-family>
                <multicast-instance>master</multicast-instance>
                <address-family>INET</address-family>
                <multicast-route junos:style="brief">
                    <multicast-group-address>239.232.0.2</multicast-group-address>
                    <multicast-source-address>198.18.0.22</multicast-source-address>
                    <multicast-source-prefix-length>32</multicast-source-prefix-length>
                    <upstream-interface-name>ae5.5</upstream-interface-name>
                    <downstream-interface-names>
                        <interface-name>xe-4/0/0.3</interface-name>
                        <interface-name>ae6.5</interface-name>
                    </downstream-interface-names>
                </multicast-route>

    The field <downstream-interface-names> has multiple <interface-name>. Just wondering how I can create the YAML table for this.

     

    Thanks.

     


    #yaml
    #JUNOS
    #Python
    #pyez


  • 2.  RE: PyEZ table with duplicate fields

     
    Posted 09-17-2015 01:21

    Hi,

     

    The documentation is difficult to understand how to do this, I did however do exactly what you are trying to achieve.

     

    The old techwiki looks to have been migrated to the forum here;

     

    http://forums.juniper.net/t5/Automation-Scripting/Junos-PyEZ/ta-p/280496

     

    The wiki is the documentation that I used to figure it out.

     

    My exmaple on an SRX is for the sessions table;

     

    get-flow-session-information <- RPC

    Within <flow-session-information> there are multiple <flow-session> records with two <flow-information> records

     

    ie

     

        <flow-session-information xmlns="http://xml.juniper.net/junos/12.1X46/junos-flow">
            <flow-session junos:style="brief">
                <session-identifier>821</session-identifier>
                <flow-information junos:style="brief">
                    <direction>In</direction>
                </flow-information>
                <flow-information junos:style="brief">
                    <direction>Out</direction>
                </flow-information>
            </flow-session>

    The YAML file for this is

     

    ---
    FlowSessTable:
      rpc: get-flow-session-information
      item: flow-session
      key: session-identifier
      view: FlowSessView
    
    FlowSessView:
      fields:
        id: session-identifier
        app: application-name
        policy: policy
        state: sess-state
        flow: _rspTable
    
    _rspTable:
      item: flow-information
      key: direction
      view: _rspView
    
    _rspView:
      fields:
        dir: direction
        src: source-address
        src_port: source-port
        dst: destination-address
        dst_port: destination-port
        proto: protocol
        int: interface-name
        pkt: pkt-cnt
        byte: byte-cnt

    The code to access the data

     

    catalog = loadyaml('flowsess.yml')
    flows = catalog['FlowSessTable'](dev).get()

    for flow in flows: print "+ id:{:<10} Policy:{:<30}".format(flow.id, flow.app) for item in flow.flow: print "+ Direction:{:<4} - src:{:<16} -> dst:{:<16} protocol:{:<5} +" .format(item.dir, item.src, item.dst, item.proto)

    You can see that I have accessed the FlowSessTable and then create the first iterator to view the <flow-session> records and another iterator to view both <flow-information> records per session.

     

    Tim



  • 3.  RE: PyEZ table with duplicate fields

    Posted 09-17-2015 03:13

    Hi Tim,

     

    Thanks for your reply. I've not tried it out yet, but I see that my case is slightly different from yours. For my case, the <interface-name> which are duplicates and these are the "fields" and not an "item" (like <downstream-interface-names>) :-

     

    ..
    ...
                    <downstream-interface-names>
                        <interface-name>xe-4/0/0.3</interface-name>
                        <interface-name>ae6.5</interface-name>
                    </downstream-interface-names>
    ...
    ..

    Thanks.

     



  • 4.  RE: PyEZ table with duplicate fields

     
    Posted 09-17-2015 03:47

    Hi,

     

    I see what you are referring too now. I have found another xml structure with duplicates.

     

    ....
    
                <source-address-range-entry>
                    <rule-source-address-low-range>192.168.1.0</rule-source-address-low-range>
                    <rule-source-address-high-range>192.168.1.255</rule-source-address-high-range>
                    <rule-source-address-low-range>172.16.12.0</rule-source-address-low-range>
                    <rule-source-address-high-range>172.16.12.255</rule-source-address-high-range>
                </source-address-range-entry>
    
    ....

    I will try on my system.

     

    Tim



  • 5.  RE: PyEZ table with duplicate fields
    Best Answer

     
    Posted 09-17-2015 16:32

    Hi,

     

    I tested this and confirmed that the duplicates can be model in YAML as a single entry. When you access the field in python it will return a list of values.

     

    fields:
      ds-if: interface-name 

    When you access ds-if it would contain

     

    ['xe-4/0/0.3', 'ae6.5']

    Then you just need to iterate through the list to access each text field 🙂

     

    Let me know if you need anymore help.

     

    Tim

     

     



  • 6.  RE: PyEZ table with duplicate fields

    Posted 09-17-2015 20:56

    Hi Tim,

     

    Thanks for your help with this. I've not coded it yet but since you have tested it, I'm sure it'll work.

     

    Cheers.



  • 7.  RE: PyEZ table with duplicate fields

    Posted 09-17-2015 23:11

    For those interested, here's the YAML table I ended up with :-

     

    ---
    MulticastTable:
        rpc: get-multicast-route-information
        key:
            - multicast-group-address
            - multicast-source-address
        item: route-family/multicast-route
        view: MulticastView
    
    MulticastView:
        fields:
           group_add: multicast-group-address
           source_add: multicast-source-address
           upstream_int: upstream-interface-name
           downstream_int: downstream-interface-names/interface-name

     

    As Tim mentioned above, the "downstream_int" is a string when there is no duplicate and will be a list when there are multiple entries.

     

    Cheers.



  • 8.  RE: PyEZ table with duplicate fields

    Posted 09-18-2015 06:39

    Thanks for sharing your working results.  

    More examples is always better.

    /doug