Automation
Automation

Scripting How-To: ex-interface script

by Cordelia on ‎08-10-2015 09:44 AM - edited on ‎09-11-2017 05:27 PM by Administrator Administrator (1,015 Views)

Overview

An op-script to combine the output of two different EX operational commands and present it "your" way.

 

Description

 

Include the description field from each interface and support an optional parameter to specify the fpc member.
 

Source Code


GitHub Links


The source code below is also available from GitHub at the following locations:

Example Output

 

01	Output from "show Ethernet-switching interface"
02	 
03	  
04	jnpr@VC2-SPP> show ethernet-switching interfaces
05	Interface   State  VLAN members         Blocking
06	ae0.0       up     vlan200              unblocked
07	ae1.0       up     vlan200              blocked - blocked by RTG
08	bme0.32770  down   mgmt                 unblocked
09	bme0.32771  down   mgmt                 unblocked
10	ge-0/0/10.0 down   default              unblocked
11	ge-0/0/11.0 down   default              unblocked
12	ge-0/0/12.0 down   default              unblocked
13	ge-0/0/13.0 down   default              unblocked
14	ge-0/0/14.0 down   default              unblocked
15	ge-0/0/15.0 down   default              unblocked
16	ge-0/0/16.0 down   default              unblocked
17	ge-0/0/17.0 down   default              unblocked
18	ge-0/0/18.0 down   default              unblocked
19	ge-0/0/19.0 down   default              unblocked
20	ge-1/0/0.0  down   vlan200              unblocked
21	ge-2/0/0.0  up     netman               unblocked
22	me0.0       up     mgmt                 unblocked
23	 
24	  
25	What the customer wanted was the output to include the "description" field from each interface.
26	  
27	Output from the operation script "ex-interface"
28	 
29	jnpr@VC2-SPP> op ex-interface
30	Interface  State  VLAN Member  Blocked      Speed  Duplex  Auto-Negotiation  Status     Description
31	ae0        up     vlan200      unblocked    Auto   Auto    enabled           complete
32	ae1        up     vlan200      RTG Blocked  Auto   Auto    enabled           complete
33	ge-0/0/10  down   default      unblocked    Auto   Auto    enabled           incomplete
34	ge-0/0/11  down   default      unblocked    Auto   Auto    enabled           incomplete
35	ge-0/0/12  down   default      unblocked    Auto   Auto    enabled           incomplete
36	ge-0/0/13  down   default      unblocked    Auto   Auto    enabled           incomplete
37	ge-0/0/14  down   default      unblocked    Auto   Auto    enabled           incomplete
38	ge-0/0/15  down   default      unblocked    Auto   Auto    enabled           incomplete
39	ge-0/0/16  down   default      unblocked    Auto   Auto    enabled           incomplete  Link to SD
40	ge-0/0/17  down   default      unblocked    Auto   Auto    enabled           incomplete
41	ge-0/0/18  down   default      unblocked    Auto   Auto    enabled           incomplete
42	ge-0/0/19  down   default      unblocked    Auto   Auto    enabled           incomplete
43	ge-1/0/0   down   vlan200      unblocked    Auto   Auto    enabled           incomplete  Link to NY
44	ge-2/0/0   up     netman       unblocked    Auto   Auto    enabled           complete
45	 
46	 
47	Output from the operation script limited to a member "interface fpc 1"
48	 
49	jnpr@VC2-SPP> op ex-interface fpc 1
50	Interface  State  VLAN Member  Blocked    Speed  Duplex  Auto-Negotiation  Status      Description
51	ge-1/0/0   down   vlan200      unblocked  Auto   Auto    enbaled           incomplete  Link to NY
52	 
53	The script only works on >=JUNOS 9.3 because the structure returned by the get-ethernet-switching-interface-information call changed between JUNOS9.2 and JUNOS9.3.  If you wish to run this on a earlier version of JUNOS, I have documented in the imbedded comments what needs to be changed to make it work.

 

SLAX Script Contents

  

001	/*
002	*
003	*    NAME: ex-interface.slax
004	*    PURPOSE: Enhance the output of the
005	*             "show ethernet-switching interfaces" command.
006	*
007	*    CREATED: 02/03/09
008	*    BY: Jay Wilson (Solutions Architect - Western Area)
009	*    LAST MOD: 07/19/11
010	*    BY: Brian Armstrong (Support Automation)
011	*    VERSION: 2.00
012	*
013	*    MODIFICATION HISTORY:
014	*        V1.00 = Initial release
015	*        V1.01 = Added a parm called "fpc".
016	*                Used to limit output to a specific member # on
017	*                EX3200/EX4200 or to a specific slot $ on the
018	*                EX8208/EX8216.
019	*                If not specified, all interfaces are returned.
020	*        V1.10 = Optimized performance
021	*                Changed the get-interface-information call
022	*                to be called only once.
023	*        V1.20 = Changes to Mash-up:
024	*                  1) Support for JUNOS9.3 structure
025	*                     returned from get-ethernet-switching-\
026	*                     interface-information.
027	*                  2) Check for blocking by RTG
028	*
029	*                Change to main routine to support the inclusion
030	*                "ae" type interfaces.
031	*
032	*        V2.00 = Updated to add speed/duplex/auto-negotiation status,
033	*                and VLAN tag ID. Tested on ex4200-48t with 11.2B2.4,
034	*                and EX4200-48T with 10.0R4.
035	*
036	*/
037	version 1.0;
038	ns junos = "http://xml.juniper.net/junos/*/junos";
039	ns xnm = "http://xml.juniper.net/xnm/1.1/xnm";
040	ns jcs = "http://xml.juniper.net/junos/commit-scripts/1.0";
041	import "../import/junos.xsl";
042	var $arguments = {
043	    <argument> {
044	        <name> "fpc";
045	        <description> "EX member or slot # to display";
046	    }
047	}
048	param $fpc;
049	 
050	 
051	match / {
052	    <op-script-results> {
053	/*
054	*
055	*  RPC that returns the "show ethernet-switching interface" output
056	*
057	*/
058	        var $rpc = <get-ethernet-switching-interface-information> {
059	        }
060	        var $switch-output = jcs:invoke($rpc);
061	/*
062	*
063	*  RPC that returns the "show interface media" output
064	*  (This includes the standard "show interface" output data as well.)
065	*
066	*/
067	    var $rpc2 = <get-interface-information> {
068	              <media>;
069	    }
070	    var $physical-output = jcs:invoke($rpc2);
071	/*
072	*
073	*   Print out the Header line
074	*   v2.00 - Removing Description for Line Length considerations
075	*
076	*/
077	        <output> jcs:printf("%-10s %-6s %-12s %-4s %-13s %-6s %-7s %-17s %-7s\n",
078	        "Interface","State","VLAN Member","Tag", "Blocked","Speed", "Duplex",
079	        "Auto-Negotiation", "Status");
080	 
081	  
082	/*
083	*
084	*   Get all the interfaces, but only look at "ge", "xe", and "ae"
085	*   At the same time, get the FPC #
086	*
087	*/
088	        for-each ($switch-output/interface) {
089	            var $triple = substring-after(interface-name,'-');
090	            var $ct1 = substring-before($triple,'/');
091	            if ((contains(interface-name,"ge") || contains(interface-name,"xe")
092	            || contains(interface-name,"ae")) && (not($fpc) || ($fpc == $ct1))) {
093	      
094	/*
095	*
096	*   Output has unit appended to the interface name.  Drop it because
097	*      it is always 0 for "family ethernet-switching", which are the
098	*      only interfaces returned from the RPC call above.
099	*
100	*/
101	                var $interface = substring-before(interface-name,".");
102	                call mash-up($interface = $interface,
103	                $physical-output = $physical-output,
104	                $switch-output = .);
105	            }
106	        }
107	    }
108	}
109	/*
110	*   NAME: MASH-UP
111	*   PURPOSE: To combine the ethernet-switching-interface-information output
112	*            with the interface-information output.
113	*   CALLED: Called for all "ge" or "xe" that are returned by the RPC
114	*           to ethernet-switching-interface-information and that match any
115	*           additional criteria, such fpc #
116	*
117	*   PARMS PASSED:
118	*        $interface = interface to mash-up
119	*        $physical-output = the structure that is returned with the
120	*                           RPC interface-information call.
121	*        $physical-output3 = Structure from 'show interfaces media'
122	*        $switch-output = the structure that goes with the interface
123	*                         that was returned with the RPC
124	*                         ethernet-switching-interface-information.
125	*
126	*/
127	template mash-up($interface, $physical-output, $switch-output) {
128	 
129	 
130	 
131	/*
132	*
133	*   Save the initial information into a formated string.
134	*   This is needed to prevent a newline from being inserted before
135	*   the initial string is complete.
136	*
137	*/
138	    var $phy-prnt = { if ($physical-output/physical-interface/name == $interface) {
139	                          expr jcs:printf("%-10s %-6s ",$interface,
140	                          $switch-output/interface-state);
141	                      }
142	                    }
143	    if ($phy-prnt) {
144	/*
145	*
146	*   Vlan membership for each interface comes back as a list, so
147	*   a for loop is needed to process it.
148	*
149	*   IF THE SCRIPT IS NOT OUTPUTTING ANY DATA,
150	*   NOTE THE VERSION OF JUNOS BEING USED AND
151	*   CHANGE THE NEXT FOR-EACH!!!!!!!!!!!!!!
152	*
153	*   interface-vlan-member was returned in JUNOS 9.1
154	*      as a leaf, so interface-vlan-name did not
155	*      exist as a leaf and block-status was 1 leaf up.
156	*   interface-vlan-member was returned in JUNOS 9.3
157	*      as a node set with interface-vlan-name and
158	*      bock-status as leafs.
159	*
160	*/
161	       for-each($switch-output/interface-vlan-member-list/interface-vlan-member) {
162	/*
163	*
164	*  Reduce the length of blocked status by using a shorter string.
165	*
166	*/
167	          var $status = { if (contains(blocking-status,"STP")) {
168	                             expr "STP Blocked";
169	                          }
170	                          else if (contains(blocking-status,"RTG")) {
171	                             expr "RTG Blocked";
172	                          }
173	                          else {
174	                             expr blocking-status;
175	                          }
176	                        }
177	/*
178	*
179	*  Reduce the length of Duplex by using a shorter string.
180	*
181	*/
182	          var $duplex = { if (contains($physical-output/physical-interface[name=$interface]/duplex,"Full")) {
183	                             expr "Full";
184	                          }
185	                          else if (contains($physical-output/physical-interface[name=$interface]/duplex,"Half")) {
186	                             expr "Half";
187	                          }
188	                          else {
189	                             expr $physical-output/physical-interface[name=$interface]/duplex;
190	                          }
191	                        }
192	/*
193	*
194	*    The first line for each interface is different than the other
195	*    output lines.   Check for the first time in the for loop and
196	*    add the saved initial string from above.
197	*    v2.00 - Removing Description for Line Length considerations
198	*/
199	          var $first = position();
200	          if ($first == 1) {
201	              <output> jcs:printf("%-18s%-12s %-4s %-12s  %-6s %-7s %-17s %-7s\n",
202	              $phy-prnt,
203	              interface-vlan-name,
204	              interface-vlan-member-tagid,
205	              $status,
206	              $physical-output/physical-interface[name=$interface]/speed,
207	              $duplex,
208	              $physical-output/physical-interface[name=$interface]/if-auto-negotiation,
209	              $physical-output/physical-interface[name=$interface]/ethernet-autonegotiation/autonegotiation-status);
210	 /*             $physical-output/physical-interface[name=$interface]/description); */
211	          }
212	          else {
213	/*
214	*
215	*   printf may look odd, but it is correct.  It must account for the
216	*   embedded spaces in the initial string.
217	*/
218	              <output> jcs:printf("%-18s%-12s %-4s %-12s\n", " ", interface-vlan-name,
219	              interface-vlan-member-tagid,
220	              $status);
221	          }
222	           
223	       }
224	    }
225	}

 

XML Script Contents

 

01	<?xml version="1.0"?>
02	<script>
03	<title>ex-interface.slax</title>
04	<author>jayw</author>
05	<synopsis>
06	An op-script to combine the output of two different EX operational commands and present it "your" way.
07	</synopsis>
08	<coe>op</coe>
09	<type>display</type>
10	 
11	<description>
12	Include the description field from each interface and support an optional parameter to specify the fpc member.
13	</description>
14	 
15	 <example>
16	 <title>Output</title>
17	 <output>example-1.output</output>
18	 </example>
19	 
20	<xhtml:script xmlns:xhtml="http://www.w3.org/1999/xhtml"
21	src="../../../../../web/leaf.js"
22	type="text/javascript"/>
23	</script>