Automation

last person joined: 4 days ago 

Ask questions and share experiences about Apstra, Paragon, and all things network automation.
  • 1.  for-each for 2 different nodes elements.

    Posted 05-14-2013 05:16

    I hope my query is simple to respond, but I haven't been able to figure it out. Based on the snippet SAMPLE OUTPUT below, as you can notice under interfaces I could have interfaces <interface-range> or simply <interface> nodes.  I want to write a for-each loop that goes down the result and prints the name under the <interface> or <interface-range> in one single run.

     

    I can do a for-each config that checks on one node name at the time (see CODE below), but I want to be able to go down and check and if the node is either <interface> or <interface-range> then to print the <name> node value. In other words I need an OR like checking as part of the for-each statement. Is this possible?  I have tried multiple combinations but none have worked.

     

    Can I do something like the CODE DESIRED below for the for-each statement? (this doesn't work, but that's the idea of what I am looking to do)

     

    CODE:

     

    var $config-rpc = <get-configuration>{
           <configuration> {
                    <interfaces> {
                     }
            }
    }

     

    var $config = jcs:invoke( $config-rpc );

     

    for-each($config//interfaces/interface) {
                 <output> "Interface or range: " _ ./name; 
    }

     

     

    CODE DESIRED:  (this doesn't work, but that's the idea of what I am looking to do)

     

    for-each( ($config//interfaces/interface) || ($config//interfaces/interface-range) ){
                 <output> "Interface or range: " _ ./name;
    }

     

     

    SAMPLE OUTPUT:

     

    > show configuration interfaces | display xml

    <rpc-reply xmlns:junos="http://xml.juniper.net/junos/11.2R1/junos">
    <configuration junos:commit-seconds="1363885658" junos:commit-localtime="2013-03-21 13:07:38 EDT" junos:commit-user="root">

    <interfaces>
           <interface-range>
                   <name>serverZ_manage_trunk</name>
                   <member-range>
                               <name>ge-4/0/35</name>

             .....

           <interface>
                  <name>ge-0/0/1</name>
                  <description>Connection XYZ->A</description>

             .....



  • 2.  RE: for-each for 2 different nodes elements.
    Best Answer

    Posted 05-14-2013 08:49

    The union operator should help here:

     

    for-each( $node-set-1 | $node-set-2 )

     

    http://www.juniper.net/techpubs/en_US/junos11.4/topics/reference/general/junos-script-automation-slax-operators.html

     

    But, do you care what order they are in? If not then the above should be fine, but if you want them to appear in document order, then something like this might work better:

     

    for-each( $config//interfaces/*[name() == "interface" || name() == "interface-range"] )



  • 3.  RE: for-each for 2 different nodes elements.

    Posted 05-14-2013 09:58

    you are a scripting genius, I recognize your name from the awesome books you wrote. Thank you the suggestions worked!