Blog Viewer

Scripting How-To: Add descriptions and protocol filtering to the normal "show interfaces terse" command

By Erdem posted 08-14-2015 15:50

  

Overview

Show interfaces terse with descriptions. This applies to SLAX version 1.0 and higher.

 

Description

This script adds descriptions and protocol filtering to the normal "show interfaces terse" command. Two arguments (interface and protocol) provide additional filtering.

Source Code

 

GitHub Links

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

Example Configuration

1 system {
2     scripts {
3         op {
4             file show-interfaces.slax {
5                 description "show interfaces terse w/ descriptions";
6             }
7         }
8     }
9 }

Example Output

01 phil@dent> op show-interfaces? 
02 Possible completions:
03   <script>             Name of script to run
04   show-interfaces      show interfaces terse w/ descriptions
05 phil@dent> op show-interfaces ? 
06 Possible completions:
07   <[Enter]>            Execute this command
08   <name>               Argument name
09   detail               Display detailed output
10   interface            Name of interface to display
11   protocol             Protocol to display (inet, inet6)
12   |                    Pipe through a command
13 phil@dent> op show-interfaces protocol inet
14 Interface               Admin Link Proto    Local                 Remote
15 fe-0/0/0.0              RLab Mgmt interface
16                                    inet     10.0.0.7/24    
17 fe-0/0/1.0              Hub link to SJC (10.5.10.2)
18                                    inet     10.5.10.1/24   
19 fe-0/0/2.0              Hub link to LAX (10.5.13.2)
20                                    inet     10.5.13.1/24   
21 fe-0/0/3.0                         inet     10.5.1.2/24    
22 fe-1/3/0.0              Spoke link to IAD (10.5.14.2)
23                                    inet     10.5.14.1/24   
24 fe-1/3/1.0              Spoke link to AUS (10.5.12.2)
25                                    inet     10.5.12.1/24   
26 fe-1/3/2.0              Spoke link to SJC (10.5.11.2)
27                                    inet     10.5.11.1/24   
28 fxp0.0                             inet     152.14.12.56/28
29 fxp1.0                             inet     10.0.0.4/8     
30 lo0.0                              inet     10.254.254.7        --> 0/0
31                                             127.0.0.1           --> 0/0
32 lo0.16384                          inet     127.0.0.1           --> 0/0
33 lo0.16385                          inet   
34  
35 phil@dent>

SLAX Script Contents

 
001 /* Machine Crafted with Care (tm) by slaxWriter */ 

002 version 1.0; 

003   

004   

005 /* 

006 - $Id: show-interfaces.slax,v 1.2 2007/10/22 03:08:02 phil Exp $ 

007 - 

008 - Copyright (c) 2005, Juniper Networks, Inc. 

009 - All rights reserved. 

010 - 

011  */ 

012 ns junos = "http://xml.juniper.net/junos/*/junos"; 

013 ns xnm = "http://xml.juniper.net/xnm/1.1/xnm"; 

014 ns jcs = "http://xml.juniper.net/junos/commit-scripts/1.0"; 

015   

016 import "../import/junos.xsl"; 

017 var $arguments = { 

018     <argument> { 

019         <name> "interface"; 

020         <description> "Name of interface to display"; 

021     } 

022     <argument> { 

023         <name> "protocol"; 

024         <description> "Protocol to display (inet, inet6)"; 

025     } 

026 } 

027 param $interface; 

028 param $protocol; 

029   

030 match / { 

031     <op-script-output> { 

032         var $rpc = <get-interface-information> { 

033             <terse>; 

034             if ($interface) { 

035                 <interface-name> $interface; 

036             } 

037         } 

038         var $out = jcs:invoke($rpc); 

039         <interface-information junos:style = "terse"> { 

040             if ($protocol == "inet" || $protocol == "inet6" || $protocol == "mpls" || $protocol == "tnp") { 

041                   

042                 for-each ($out/physical-interface/logical-interface[address-family/address-family-name ==$protocol]) { 

043                     call intf(); 

044                 } 

045               

046             } else if ($protocol) { 

047                 <xnm:error> { 

048                     <message> { 

049                         expr "invalid protocol: "; 

050                         expr $protocol; 

051                     } 

052                 } 

053               

054             } else { 

055                   

056                 for-each ($out/physical-interface/logical-interface) { 

057                     call intf(); 

058                 } 

059             } 

060         } 

061     } 

062 } 

063   

064 template intf () { 

065     var $status = { 

066         if (admin-status == "up" && oper-status == "up") { 

067           

068         } else if (admin-status == "down") { 

069             expr "offline"; 

070           

071         } else if (oper-status == "down" && ../admin-status == "down") { 

072             expr "p-offline"; 

073           

074         } else if (oper-status == "down" && ../oper-status == "down") { 

075             expr "p-down"; 

076           

077         } else if (oper-status == "down") { 

078             expr "down"; 

079           

080         } else { 

081             expr oper-status _ "/" _ admin-status; 

082         } 

083     } 

084     var $desc = { 

085         if (description) { 

086             expr description; 

087           

088         } else if (../description) { 

089             expr ../description; 

090         } 

091     } 

092     <logical-interface> { 

093         <name> name; 

094         if (string-length($desc)) { 

095             <admin-status> $desc; 

096         } 

097         <admin-status> $status; 

098         if ($protocol) { 

099             copy-of address-family[address-family-name == $protocol]; 

100           

101         } else { 

102             copy-of address-family; 

103         } 

104     } 

105 } 

XML Script Contents

 
01 <?xml version="1.0"?>
02 <script>
03   <title>show-interfaces.slax</title>
04   <alternate>show-interfaces.xsl</alternate>
05   <author>phil</author>
06   <synopsis>
07     "show interfaces terse" with descriptions
08   </synopsis>
09   <coe>op</coe>
10   <type>interfaces</type>
11  
12   <description>
13     This script adds descriptions and protocol filtering to the
14     normal "show interfaces terse" command.
15     Two arguments (interface and protocol) provide additional filtering.
16   </description>
17  
18   <example>
19     <title>Sample output</title>
20     <config>show-interfaces.conf</config>
21     <output>show-interfaces.output</output>
22   </example>
23   <xhtml:script xmlns:xhtml="http://www.w3.org/1999/xhtml"
24                 src="../../../../../web/leaf.js" type="text/javascript"/>
25 </script>

#Slax
#ScriptingHow-To
#opscript
#How-To

Permalink