Automation
Automation

Scripting How-To: Add descriptions and protocol filtering

by Cordelia on ‎11-19-2015 12:16 PM - edited on ‎09-19-2017 08:05 AM by Administrator Administrator (1,260 Views)
This applies to SLAX version 1.0 and higher.
 

Overview

"Show interfaces terse" with descriptions.

 

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

 

system {
    scripts {
        op {
            file show-interfaces.slax {
                description "show interfaces terse w/ descriptions";
            }
        }
    }
}

 

Example Output

 

phil@dent> op show-interfaces?  
Possible completions:
  <script>             Name of script to run
  show-interfaces      show interfaces terse w/ descriptions
phil@dent> op show-interfaces ?  
Possible completions:
  <[Enter]>            Execute this command
  <name>               Argument name
  detail               Display detailed output
  interface            Name of interface to display
  protocol             Protocol to display (inet, inet6)
  |                    Pipe through a command
phil@dent> op show-interfaces protocol inet 
Interface               Admin Link Proto    Local                 Remote
fe-0/0/0.0              RLab Mgmt interface
                                   inet     10.0.0.7/24     
fe-0/0/1.0              Hub link to SJC (10.5.10.2)
                                   inet     10.5.10.1/24    
fe-0/0/2.0              Hub link to LAX (10.5.13.2)
                                   inet     10.5.13.1/24    
fe-0/0/3.0                         inet     10.5.1.2/24     
fe-1/3/0.0              Spoke link to IAD (10.5.14.2)
                                   inet     10.5.14.1/24    
fe-1/3/1.0              Spoke link to AUS (10.5.12.2)
                                   inet     10.5.12.1/24    
fe-1/3/2.0              Spoke link to SJC (10.5.11.2)
                                   inet     10.5.11.1/24    
fxp0.0                             inet     152.14.12.56/28 
fxp1.0                             inet     10.0.0.4/8      
lo0.0                              inet     10.254.254.7        --> 0/0
                                            127.0.0.1           --> 0/0
lo0.16384                          inet     127.0.0.1           --> 0/0
lo0.16385                          inet    

phil@dent> 

 

SLAX Script Contents

 

/* Machine Crafted with Care (tm) by slaxWriter */
version 1.0;


/*
- $Id: show-interfaces.slax,v 1.2 2007/10/22 03:08:02 phil Exp $
-
- Copyright (c) 2005, Juniper Networks, Inc.
- All rights reserved.
-
 */
ns junos = "http://xml.juniper.net/junos/*/junos";
ns xnm = "http://xml.juniper.net/xnm/1.1/xnm";
ns jcs = "http://xml.juniper.net/junos/commit-scripts/1.0";

import "../import/junos.xsl";
var $arguments = {
    <argument> {
        <name> "interface";
        <description> "Name of interface to display";
    }
    <argument> {
        <name> "protocol";
        <description> "Protocol to display (inet, inet6)";
    }
}
param $interface;
param $protocol;

match / {
    <op-script-output> {
        var $rpc = <get-interface-information> {
            <terse>;
            if ($interface) {
                <interface-name> $interface;
            }
        }
        var $out = jcs:invoke($rpc);
        <interface-information junos:style = "terse"> {
            if ($protocol == "inet" || $protocol == "inet6" || $protocol == "mpls" || $protocol == "tnp") {
                
                for-each ($out/physical-interface/logical-interface[address-family/address-family-name == $protocol]) {
                    call intf();
                }
            
            } else if ($protocol) {
                <xnm:error> {
                    <message> {
                        expr "invalid protocol: ";
                        expr $protocol;
                    }
                }
            
            } else {
                
                for-each ($out/physical-interface/logical-interface) {
                    call intf();
                }
            }
        }
    }
}

template intf () {
    var $status = {
        if (admin-status == "up" && oper-status == "up") {
        
        } else if (admin-status == "down") {
            expr "offline";
        
        } else if (oper-status == "down" && ../admin-status == "down") {
            expr "p-offline";
        
        } else if (oper-status == "down" && ../oper-status == "down") {
            expr "p-down";
        
        } else if (oper-status == "down") {
            expr "down";
        
        } else {
            expr oper-status _ "/" _ admin-status;
        }
    }
    var $desc = {
        if (description) {
            expr description;
        
        } else if (../description) {
            expr ../description;
        }
    }
    <logical-interface> {
        <name> name;
        if (string-length($desc)) {
            <admin-status> $desc;
        }
        <admin-status> $status;
        if ($protocol) {
            copy-of address-family[address-family-name == $protocol];
        
        } else {
            copy-of address-family;
        }
    }
}

 

XML Script Contents

 

<?xml version="1.0"?>
<script>
  <title>show-interfaces.slax</title>
  <alternate>show-interfaces.xsl</alternate>
  <author>phil</author>
  <synopsis>
    "show interfaces terse" with descriptions
  </synopsis>
  <coe>op</coe>
  <type>interfaces</type>

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

  <example>
    <title>Sample output</title>
    <config>show-interfaces.conf</config>
    <output>show-interfaces.output</output>
  </example>
  <xhtml:script xmlns:xhtml="http://www.w3.org/1999/xhtml"
                src="../../../../../web/leaf.js" type="text/javascript"/>
</script>