Required Boilerplate for SNMP Scripts
Define the boilerplate for SNMP scripts.
You can write Junos OS SNMP scripts in Extensible Stylesheet Language Transformations (XSLT), Stylesheet Language Alternative syntaX (SLAX), or Python. SNMP scripts must include the necessary boilerplate required for that script language. Include boilerplate for both basic script functionality, as well as any optional functionality used within the script such as the Junos OS extension functions and named templates. This topic provides standard boilerplate that you can use in XSLT, SLAX, and Python SNMP scripts.
SLAX Boilerplate for SNMP Scripts
The SLAX SNMP script boilerplate is as follows:
1 version 1.2;
2 ns junos = "http://xml.juniper.net/junos/*/junos";
3 ns xnm = "http://xml.juniper.net/xnm/1.1/xnm";
4 ns jcs = "http://xml.juniper.net/junos/commit-scripts/1.0";
5 ns dyn = "http://exslt.org/dynamic";
6 ns snmp extension = "http://exslt.org/functions";
7 match / {
8 <snmp-script-results> {
9 var $snmp-action = snmp-script-input/snmp-action;
10 var $snmp-oid = snmp-script-input/snmp-oid;
<!-- ... insert your code here ... -->
11 <snmp-oid> $snmp-oid;
12 <snmp-type> $snmp-type;
13 <snmp-value> $snmp-value;
}
}
Line 1 specifies the SLAX version as 1.2.
1 version 1.2;
Lines 2 through 6 list all the namespace mappings commonly used in SNMP scripts. Not all of these prefixes are used in this example. Listing all namespace mappings prevents errors if you use the mappings in later versions of the script. These namespace mappings enable you to use extension functions and named templates in your scripts. For more information about extension functions and named templates, see:
-
Understanding Extension Functions in Junos OS Automation Scripts
-
Understanding Named Templates in Junos OS Automation Scripts
Line 5 and line 6 have EXSLT namespace mappings. SNMP extension functions are defined
in the namespace with the associated URI http://exslt.org/functions. Line 6
registers the snmp extension namespace with the EXSLT functions
namespace, allowing you to define customized functions using snmp
as a prefix within your SLAX script. For more information about the EXSLT namespace,
see https://exslt.github.io/func/index.html.
2 ns junos = "http://xml.juniper.net/junos/*/junos"; 3 ns xnm = "http://xml.juniper.net/xnm/1.1/xnm"; 4 ns jcs = "http://xml.juniper.net/junos/commit-scripts/1.0"; 5 ns dyn = "http://exslt.org/dynamic"; 6 ns snmp extension = "http://exslt.org/functions";
Line 7 defines an unnamed template, match /, that represents the top
level of the XML hierarchy. All XML Path Language (XPath) expressions
in the script must start at the top-level element in the hierarchy. Starting at the
root element enables the script to access all possible Junos XML and Junos XML
protocol remote procedure calls (RPCs). For more information, see XPath
Overview.
7 match / {
After the match / tag element, the
<snmp-script-results> tag must be the top-level child
tag, as shown in Line 8. The script returns the value of this element to the OID
requester.
8 <snmp-script-results> {
Lines 9 and 10 define variables for the requested action, for example,
get or get-next, and the value of the OID.
9 var $snmp-action = snmp-script-input/snmp-action; 10 var $snmp-oid = snmp-script-input/snmp-oid;
Between Line 10 and Line 11, you can define additional code that includes XSLT
templates that are called from within the match / template.
Lines 11 through 13 define the values returned by the SNMP script to the OID
requester. The SNMP script uses the input value of
snmp-script-input/snmp-oid for the value of
<snmp-oid>. For the SNMP script feature,
<snmp-type> supports the following OID types:
-
Counter32 -
Counter64 -
Integer32 (or INTEGER) -
Unsigned32 -
OCTET STRING
You set the <snmp-value> to the return value from the
script.
11 <snmp-oid> $snmp-oid; 12 <snmp-type> $snmp-type; 13 <snmp-value> $snmp-value;
XSLT Boilerplate for SNMP Scripts
The XSLT boilerplate is:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:junos="http://xml.juniper.net/junos/*/junos"
xmlns:xnm="http://xml.juniper.net/xnm/1.1/xnm"
xmlns:jcs="http://xml.juniper.net/junos/commit-scripts/1.0"
xmlns:dyn="http://exslt.org/dynamic"
xmlns:snmp="http://exslt.org/functions"
version="1.0" extension-element-prefixes="snmp">
<xsl:template match="/">
<snmp-script-results>
<xsl:variable name="snmp-action" select="snmp-script-input/snmp-action"/>
<xsl:variable name="snmp-oid" select="snmp-script-input/snmp-oid"/>
<!-- Insert your code here -->
<snmp-oid>
<xsl:value-of select="$snmp-oid"/>
</snmp-oid>
<snmp-type>
<xsl:value-of select="$snmp-type"/>
</snmp-type>
<snmp-value>
<xsl:value-of select="$snmp-value"/>
</snmp-value>
</snmp-script-results>
</xsl:template>
</xsl:stylesheet>
Python Boilerplate for SNMP Scripts
Python SNMP scripts do not have a required boilerplate. However, Python SNMP scripts
must import any objects that the script uses. For example, Python SNMP scripts must
include the import jcs statement in order to use the
get_snmp_action(), get_snmp_oid(), and
emit_snmp_attributes() functions. These functions retrieve the
action and OID values passed to the script and return the data for the requested MIB
object.
import jcs if __name__ == '__main__':
Python automation scripts do not need to include an interpreter directive line
(#!/usr/bin/env python) at the start of the script. However,
the program still executes correctly if one is present.