Automation
Automation

Scripting How-To: Using the link-up script

by Cordelia on ‎08-10-2015 10:40 AM - edited on ‎09-15-2017 04:21 PM by Administrator Administrator (1,460 Views)

Overview

Logging of MAC addresses with correlation to the interface (EX) they are attached to. This applies to SLAX version 1.0 and higher.

 

Description

 

When a link becomes active, a message needs to be written to syslog indicating the interface name and the MAX address of the device(s) plugged into the interface.


Source Code


GitHub Links


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

Example Configuration

 

01	You invoke it by adding the following to the switch's configuration:
02	 
03	event-options {
04	    policy LINK-UP {
05	        events snmp_trap_link_up;
06	        then {
07	            event-script link-up.slax {
08	                arguments {
09	                    ifname "{$$.interface-name}";
10	                }
11	            }
12	       }
13	    }
14	}
15	 
16	 
17	The output is SYSLOG will look like:
18	 
19	Aug  1 16:14:37   cscript: link-up.slax: IFNAME ge-0/0/3 MAC 00:1e:37:86:13:30
20	 
21	 
22	This was tested on EX4200 running JUNOS 9.6.
23	 
24	Note: To allow time for the ethernet switching table to populate, I had to add a delay once the event triggers.

 

Example Output

 

1	Aug  1 16:14:37   cscript: link-up.slax: IFNAME ge-0/0/3 MAC 00:1e:37:86:13:30

 

SLAX Script Contents

 

001	/*
002	*
003	*    NAME: link-up.slax
004	*    PURPOSE: The requirement is that when a link becomes
005	*             active, a message needs to be written to
006	*             syslog indicating the interface name and
007	*             the MAC addresses of the device(s) plugged
008	*             into the interface.
009	*
010	*    CREATED: 08/10/09
011	*    BY: Jay Wilson (Solutions Architect - Western Area)
012	*    LAST MOD: 08/11/09
013	*    BY: Jay Wilson
014	*    VERSION: 1.00
015	*
016	*    MODIFICATION HISTORY:
017	*        V1.00 = Initial release
018	*        V1.01 = Added delay to allow MAC table to
019	*                be populated.
020	*
021	*/
022	version 1.0;
023	ns junos = "http://xml.juniper.net/junos/*/junos";
024	ns xnm = "http://xml.juniper.net/xnm/1.1/xnm";
025	ns jcs = "http://xml.juniper.net/junos/commit-scripts/1.0";
026	import "../import/junos.xsl";
027	 
028	/*
029	*
030	*  Define the argument that will be passed into the script.
031	*  This argument is by the event attribute "interface-name"
032	*  when the event triggers.
033	*
034	*/
035	var $arguments = <argument> {
036	    <name> "ifname";
037	    <description> "Interface to report about.";
038	}
039	param $ifname;
040	 
041	/*
042	*
043	*   debug1 -> Highlevel debugging
044	*   debug7 -> Lowlevel debugging
045	*   0 == off
046	*   1 == on
047	*
048	*/
049	var $debug1 = "0";
050	var $debug7 = "0";
051	 
052	match / {
053	/*
054	*
055	*   Get the MAC table from the switch
056	*
057	*/
058	    expr jcs:sleep(3);
059	    var $rpc = <get-ethernet-switching-table-information> {
060	        }
061	    var $switch-output = jcs:invoke($rpc);
062	    call debug ( $type = "r", $on = $debug7, $output = $switch-output//text());
063	/*
064	*
065	*   Process the MAC table
066	*
067	*/
068	    for-each ($switch-output/ethernet-switching-table/mac-table-entry) {
069	        for-each (mac-interfaces-list) {
070	        call debug ( $type = "w", $on = $debug1, $output = "In the Loop.");
071	        if (contains(mac-interfaces,$ifname)) {
072	            var $message = "IFNAME " _ $ifname _ " MAC " _ ../mac-address;
073	            call write-it ($message = $message);
074	        }
075	        }
076	    }
077	}
078	/*
079	*   NAME: WRITE-IT
080	*   PURPOSE: Writes a message to SYSLOG that is passed to it.
081	*   CALLED: Can be called at any time from any where.
082	*
083	*   PARMS PASSED:
084	*        $message = The string to print out
085	*
086	*/
087	template write-it($message) {
088	    expr jcs:syslog("user.warn","link-up.slax: ",$message);
089	}
090	/*
091	*   NAME: DEBUG
092	*   PURPOSE: Writes debugging information to SYSLOG.
093	*   CALLED: Can be called at any time from any where.
094	*
095	*   PARMS PASSED:
096	*        $type = indicates the type of information
097	*                being passed in.  An "r" means the
098	*                information is a node-set result.
099	*        $on = turns on writing it or not
100	*        $output = the string that needs to be
101	*                  printed
102	*
103	*/
104	template debug ( $type, $on, $output) {
105	    if ($on == "1") {
106	        if ($type == "r" ) {
107	            for-each( $output ) {
108	                call write-it($message = . );
109	            }
110	        }
111	        else {
112	            call write-it($message = $output);
113	        }
114	    }
115	}

 

XML Script Contents

 

01	<?xml version="1.0"?>
02	<script>
03	<title>link-up.slax</title>
04	<author>jayw</author>
05	<synopsis>
06	 
07	</synopsis>
08	<coe>event</coe>
09	<type>ethernet-switching</type>
10	 
11	<description>
12	Logging of MAC addresses with correlation to the interface (EX) they are attached to
13	 
14	</description>
15	 
16	 <example>
17	 <title>Example</title>
18	 <config>example-1.conf</config>
19	 </example>
20	 
21	<xhtml:script xmlns:xhtml="http://www.w3.org/1999/xhtml"
22	src="../../../../../web/leaf.js"
23	type="text/javascript"/>
24	</script>