Junos Automation (Scripting)
Junos Automation (Scripting)

Removing Duplicates from a list

‎11-25-2013 10:25 AM

Below is some sample output from my script..

 

The script takes interface names and using the jcs:split function separates the fpc and pic values.  I would like to use only the fpc and pic values in a resulting list with all duplicates removed.

 

For instance, the ge-2/0/0, ge-2/0/1, ge-2/0/2, etc  interfaces each belong to fpc 2 pic 0(ge-2   0), so my resulting list should have "ge-0   0" listed just once

 

My complete script file is attached.  I'm new to scripting and any insight would be great.

 

Thanks,

Chris 

 

admin@MX240-1> op get-interface   

xe-0/0/0        xe-0    0

xe-0/1/0        xe-0    1

xe-0/2/0        xe-0    2

xe-0/3/0        xe-0    3

ge-2/0/0        ge-2    0

ge-2/0/1        ge-2    0

ge-2/0/2        ge-2    0

ge-2/0/3        ge-2    0

ge-2/0/4        ge-2    0

ge-2/0/5        ge-2    0

ge-2/0/6        ge-2    0

ge-2/0/7        ge-2    0

ge-2/0/8        ge-2    0

ge-2/0/9        ge-2    0

ge-2/1/0        ge-2    1

ge-2/1/1        ge-2    1

ge-2/1/2        ge-2    1

ge-2/1/3        ge-2    1

ge-2/1/4        ge-2    1

ge-2/1/5        ge-2    1

ge-2/1/6        ge-2    1

ge-2/1/7        ge-2    1

ge-2/1/8        ge-2    1

ge-2/1/9        ge-2    1

ge-2/2/0        ge-2    2

ge-2/2/1        ge-2    2

ge-2/2/2        ge-2    2

ge-2/2/3        ge-2    2

ge-2/2/4        ge-2    2

ge-2/2/5        ge-2    2

ge-2/2/6        ge-2    2

ge-2/2/7        ge-2    2

ge-2/2/8        ge-2    2

Attachments

6 REPLIES 6
Junos Automation (Scripting)

Re: Removing Duplicates from a list

‎11-25-2013 12:16 PM
You might want to consider a slightly different approach.

1. Use get-interface-information rpc response to build a node set (e.g. "$if-list") that contains each physical-interface name and your "derived" values for fpc and pic. I.e. $if-list would contain a sequence of:
{
$name; (from physical-interface name)
$fpc; <-- derived using your regex
$pic; <-- ditto
}

2. Once you've built $if-list with your derived data, you can operate on that node-set with things like set:distinct() or count()
e.g.
$fpc-list = set:distinct($if-list//fpc);
copy-of $fpc-list;

e.g.
$ge-if-count = count($if-list/name[starts-with(."ge-"))


If you haven't already, download a copy of "This Week: JUNOS Automation Reference for SLAX 1.0".
It's indispensable.
HTH.
/doug
--
"There he goes. One of God's own prototypes. A high-powered mutant of some kind never even considered for mass production. Too weird to live, and too rare to die." --HST
Junos Automation (Scripting)

Re: Removing Duplicates from a list

‎11-26-2013 08:30 AM

How to build a node set with the reponse file though. 

 

I'm trying

 

var $results = jcs:invoke ( get-interface-information;

 

var $ethernets = jcs:regex ( "[xg]e.*", $results//name ) ;

 

for-each ( $ethernets) {

   <output> . ;

}

 

No output.  Problem is that the regex attempts a match on only the first name of the list of names.

 

I have the Day One guide, but SLAX is not easy to understand, especially the notion of node-sets versus RTF and how to populate and parse these.

Junos Automation (Scripting)

Re: Removing Duplicates from a list

‎12-03-2013 02:46 AM

Hello,

To build a node-set, You need this:

 

var $results := jcs:invoke ( get-interface-information ;

 Note := versus =.

 

And then You can walk the $results node-set and use whatever regex You fancy.

Or, post an ideal printout You want to get from box' interface information, and forum members will suggest the ways to achieve it with SLAX.

HTH

Thanks
Alex

 

_____________________________________________________________________

Please ask Your Juniper account team about Juniper Professional Services offerings.
Juniper PS can design, test & build the network/part of the network as per Your requirements

+++++++++++++++++++++++++++++++++++++++++++++

Accept as Solution = cool !
Accept as Solution+Kudo = You are a Star !
Junos Automation (Scripting)

Re: Removing Duplicates from a list

‎12-03-2013 07:30 AM

My goal:

My script is to perform an audit on all multicast enabled M/MX devices to calculate how many PFEs are included in the replication tree to forward multicast for a particular S,G, in a particular routing-instance to all downstream interested subscribers.

 

So, the get-multicast-information API is the starting point for my audit.

 

The downstream interfaces node, filtered for only ge- and xe- interfaces provides the list of interfaces per S,G. 

 

On the M/MX platfrom, the pic value, in xx-?/?/? represents the PFE value for an interface. 

 

For example ge-1/1/0, ge-1/1/1 and ge-1/1/2 all share the same PFE, so given a list of interfaces as follows.

 

xe-0/0/0

xe-0/1/0

ge-1/1/0

ge-1/1/1

ge-1/1/2

 

The unique PFEs would be

 

xe-0/0

xe-0/1

ge-1/1

 

and hence the PFE replication tree diameter for this S,G would be 3.

 

Since my lab does not have a router with more than one downstream interface, per S,G entry,  I am using the get-interface-information API to build the logic to find the unique PFE count.

 

Now, I'm a newbie to Junos scripting, so I am having difficulty manipulating the data, while trying to avoid loops and immutable variables.  I've read the "This Week - Applying JUNOS Automation" and also have access to the reference guide for functions, elements and templates, but am still stuck here a bit.

 

My first problem is getting only the list of ge- and xe- interfaces into a list.  I seem to get the whole name node and not just the name element.

 

var $command-set := {
   <get-interface-information> {
    <terse> ;
   }
  }

var $results := jcs:invoke ( $command-set );

var $interfaces :=  $results/physical-interface[ @name==jcs:regex("[xg]e.*", @name) ];

 

for-each ( $interfaces ) {
 <output> .;    
}

 

My output file using <xsl:document { copy-of $interfaces; }

 

<physical-interface xmlns="http://xml.juniper.net/junos/11.4R8/junos-interface" xmlns:junos="http://xml.juniper.net/junos/*/junos">
<name>xe-0/0/0</name>
<admin-status>up</admin-status>
<oper-status>up</oper-status>
<description>MX960-1 xe-10/2/0 - ae1</description>
<logical-interface>
<name>xe-0/0/0.0</name>
<admin-status>up</admin-status>
<oper-status>up</oper-status>
<filter-information>
</filter-information>
<address-family>
<address-family-name>aenet</address-family-name>
<ae-bundle-name>ae1.0</ae-bundle-name>
</address-family>
</logical-interface>
</physical-interface><physical-interface xmlns="http://xml.juniper.net/junos/11.4R8/junos-interface" xmlns:junos="http://xml.juniper.net/junos/*/junos">
<name>xe-0/1/0</name>
<admin-status>up</admin-status>
<oper-status>up</oper-status>
<description>MX960-1 xe-10/3/0 - ae1</description>
<logical-interface>
<name>xe-0/1/0.0</name>
<admin-status>up</admin-status>
<oper-status>up</oper-status>
<filter-information>
</filter-information>
<address-family>
<address-family-name>aenet</address-family-name>
<ae-bundle-name>ae1.0</ae-bundle-name>
</address-family>
</logical-interface>
</physical-interface><physical-interface xmlns="http://xml.juniper.net/junos/11.4R8/junos-interface" xmlns:junos="http://xml.juniper.net/junos/*/junos">
<name>xe-0/2/0</name>
<admin-status>up</admin-status>
<oper-status>up</oper-status>
<description>MX240-2 xe-0/2/0 - ae0</description>
<logical-interface>
<name>xe-0/2/0.0</name>
<admin-status>up</admin-status>
<oper-status>up</oper-status>
<filter-information>
</filter-information>

Junos Automation (Scripting)
Solution
Accepted by topic author challinger
‎08-26-2015 01:27 AM

Re: Removing Duplicates from a list

‎12-03-2013 08:30 AM

Chris,


Pls see attached example of how you can use set:distinct() to achieve what you're trying to do.

Basically, I just build up a nodeset of (in this case ge-*) interfaces that looks like this:

 

    <if>
        <name>
            ge-0/0/0
        </name>
        <fpc>
            ge-0
        </fpc>
        <pic>
            0
        </pic>
        <port>
            0
        </port>
    </if>
    <if>
        <name>
            ge-0/0/1
        </name>
        <fpc>
            ge-0
        </fpc>
        <pic>
            0
        </pic>
        <port>
            1
        </port>
    </if>
...

 

In general, you'll probably find it easier to build up your own nodesets so you can reference them and iterate through them in multiple places.

 

Hope that helps!
/doug

 

 

 

 

--
"There he goes. One of God's own prototypes. A high-powered mutant of some kind never even considered for mass production. Too weird to live, and too rare to die." --HST

Attachments

Junos Automation (Scripting)

Re: Removing Duplicates from a list

‎12-03-2013 09:36 AM

Awesome!  Need practice with this stuff for sure.