Blogs

Scripting How-To: show interface status

By Erdem posted 11-19-2015 12:23

  
This applies to SLAX version 1.0 and higher.
 

Overview

This script mirrors the Cisco IOS command "show interface status".

 

Description

 

This script displays the logical-interface name, status, VLAN members, speed and duplex of the Ethernet switching-interfaces on the system. It is meant to provide similar output to the Cisco IOS command "show interface status". A command-line argument exists (interface) that filters the exact interface. Error checking is performed based on the interface entered by the user.

 

Source Code

 

GitHub Links

 

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

 

 

Example Configuration

 

set system scripts op file show-interface-status.slax

 

Example Output

 

user@cli# run op show-interface-status
Interface    Physical Description     Logical Description      Status VLAN Members  Vlan-Id  Port-mode   In(bps)   Out(bps)  In-Err    Out-Err   Speed-Conf  Duplex-Conf  Flow-Ctrl-Conf  Speed(neg)  Duplex(neg)  Flow-Ctrl(neg)
ae6.0                                                          up     IC-to-ISG     100      untagged    186080    187144                        3Gbps                    disabled                                             
ae7.0                                                          down   default       100      untagged    0         0                             Unspecified              disabled                                             
ge-0/0/0.0                                                     up     IC-to-ISG     100      untagged    45496     320544                        Auto        Auto         enabled         1000 Mbps   full-duplex  None        
ge-0/0/1.0                                                     up     IC-to-ISG     100      untagged    0         366048                        Auto        Auto         enabled         1000 Mbps   full-duplex  None        
ge-0/0/2.0                                                     up     IC-to-ISG     100      untagged    46040     321568                        Auto        Auto         enabled         1000 Mbps   full-duplex  None        
ge-0/0/3.0                                                     up     IC-to-ISG     100      untagged    0         366048                        Auto        Auto         enabled         1000 Mbps   full-duplex  None        
ge-0/0/6.0                                                     up     IC-to-ISG     100      untagged    47032     319520                        1000mbps    Full-Duplex  enabled                                              
ge-0/0/8.0                                                     down   default       100      untagged    0         0                             1000mbps    Full-Duplex  enabled                                              
ge-0/0/18.0                                                    up     IC-to-ISG     100      untagged    0         366048                        Auto        Auto         enabled         1000 Mbps   full-duplex  Symmetric   
ge-0/0/19.0                                                    up     IC-to-ISG     100      untagged    45496     321056                        Auto        Auto         enabled         1000 Mbps   full-duplex  Symmetric   
ge-0/0/20.0                                                    up     IC-to-ISG     100      untagged    0         366496                        Auto        Auto         enabled         1000 Mbps   full-duplex  Symmetric   
ge-0/0/21.0                                                    up     IC-to-ISG     100      untagged    0         365984                        Auto        Auto         enabled         1000 Mbps   full-duplex  Symmetric 

 

SLAX Script Contents

 

/*
 *  show_interface_staus.slax
 *
 *  Created by Todd Okolowicz (tokolowicz@juniper.net) on 20090713.
 *  Copyright (c) 2011 Juniper Networks. All rights reserved.
 *
 *  Version History
 *  ===============
 *  v0.1    Initial release (by Todd Okolowicz)
 *  v0.2    Modified to include vlans on a given port and whether
 *          or not it was trunked (by Robery Lemm)
 *
 */

version 1.0;
 
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";

/* This is imported into JUNOS as a CLI option */ 
var $arguments = { 
    <argument> { 
	<name> "interface"; 
        <description> "Name of logical interface (e.g. ge-0/0/0.0)"; 
    } 
} 
 
/* Command-line argument */ 
param $interface; 

match / {
    <op-script-results> {

        /* Send JUNOS XML API Element via jcs:invoke */
        var $results1 = jcs:invoke( "get-interface-information" );
 
        /* Send JUNOS XML API Element via jcs:invoke */
        var $command-rpc2 = <command> "show ethernet-switching interfaces";
        var $results2 = jcs:invoke( $command-rpc2 );

        var $command-rpc3 = <command> "show interfaces extensive";
        var $results3 = jcs:invoke( $command-rpc3 );

        var $command-rpc4 = <command> "show ethernet switching table";
        var $results4 = jcs:invoke( $command-rpc4 );

	 /* This is a functional code block that uses a regular
 	  * expression to check the interface cli option for correct syntax. I 
	  * disabled it and opted for the code block below. The regex is a 
	  * hard-coded and does not account for all possible hardware 
	  * configurations (i.e. EX3200 vs. EX4200, uplink vs. no uplink, 1GbE
	  * or 10GbE uplink, Virtual Chassis or stand-alone, etc.) It is more
	  * accurate to compare the cli input vs. the list of Ethernet switching
	  * interfaces actually enabled in the system. I left the regex code block
	  * in for code re-use in future scripts and learning purposes.

	  * Error check for incorrect interface syntax using a regex. 
		if ( not ( jcs:empty ( $interface ) ) and jcs:empty ( jcs:regex ( "[xg]e-[0-9]/[0-1]/(([0-9])|([1-3][0-9])|(4[0-7])).0", $interface ) ) ) {
			<xsl:message terminate="yes"> "The interface " _ $interface _ " isn't valid.\nUsage example: op show_interface_status interface ge-0/0/0.0";
		}
	  */
	
	 var $matching-interface = $results2/interface [ interface-name == $interface ];
	 if( not ( jcs:empty ($interface ) ) and jcs:empty ( $matching-interface ) ) {
	     <xsl:message terminate="yes"> "The interface " _ $interface _ " isn't valid.\nUsage example: op show_interface_status interface ge-0/0/0.0";
	 }

	 /* Create node list based on location path, loop through each node */
	 <output> jcs:printf("%-13s%-25s%-25s%-7s%-14s%-9s%-12s%-10s%-10s%-10s%-10s%-12s%-13s%-16s%-12s%-13s%-12s", "Interface", "Physical Description", "Logical Description", "Status", "VLAN Members", "Vlan-Id", "Port-mode", "In(bps)", "Out(bps)", "In-Err", "Out-Err", "Speed-Conf", "Duplex-Conf", "Flow-Ctrl-Conf", "Speed(neg)", "Duplex(neg)", "Flow-Ctrl(neg)" );
	 for-each ( $results2/interface [ string-length($interface)==0 or interface-name=$interface ] ) {
	     var $first-vlan = interface-vlan-member-list/interface-vlan-member/interface-vlan-name [position() == 1 ];
	     var $vlan-tag = interface-vlan-member-list/interface-vlan-member/interface-vlan-member-tagid [position() == 1 ];
	     var $interface-name = interface-name;
	     var $status = interface-state;
	     var $vlan-tagness = interface-vlan-member-list/interface-vlan-member/interface-vlan-member-tagness;
	     var $logical-interface = $results1/physical-interface/logical-interface [name==$interface-name];
	     var $physical-interface = $results3/physical-interface/logical-interface [name==$interface-name];
	     var $phys-interface = $results3/physical-interface;
	     <output> jcs:printf("%-13s%-25s%-25s%-7s%-14s%-9s%-12s%-10s%-10s%-10s%-10s%-12s%-13s%-16s%-12s%-13s%-12s", $interface-name, $physical-interface/../description, $logical-interface/description, $status, $first-vlan, $vlan-tag, $vlan-tagness, $logical-interface/../traffic-statistics/input-bps, $logical-interface/../traffic-statistics/output-bps, $logical-interface/../input-error-count, $logical-interface/../output-error-count, $logical-interface/../speed, $logical-interface/../duplex, $logical-interface/../if-flow-control, $physical-interface/../ethernet-autonegotiation/link-partner-speed, $physical-interface/../ethernet-autonegotiation/link-partner-duplexity,$physical-interface/../ethernet-autonegotiation/flow-control );
	     for-each ( ./interface-vlan-member-list/interface-vlan-member[position() != 1 ] ) {
	         <output> jcs:printf("%-70s%-14s%-14s", "", ./interface-vlan-name, ./interface-vlan-member-tagid );
	     }
	 }
    }
}

 

XML Script Contents

 

<?xml version="1.0"?>
<script>
<title>show-interface-status.slax</title>
<author>tokolowicz</author>
<synopsis>
This script mirrors the Cisco IOS comand "show interface status" 
</synopsis>
<coe>op</coe>
<type>display</type>

<description>
This script displays the logical-interface name, status, VLAN members and Id, speed and duplex of the Ethernet switching-interfaces on the system.
It is meant to provide similar output to the Cisco IOS command "show interface status".
A command-line argument exists (interface) that filters the exact interface.
Error checking is performed based on the interface entered by the user. 
  

</description>

 <example>
 <title>Example</title>
 <config>example-1.conf</config>
 <output>example-1.output</output>
 </example>

<xhtml:script xmlns:xhtml="http://www.w3.org/1999/xhtml"
src="../../../../../web/leaf.js" 
type="text/javascript"/>
</script>

#How-To
#showinterfacestatus
#ScriptingHow-To