Help us improve your experience.

Let us know what you think.

Do you have time for a two-minute survey?

 
 

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:

Line 1 specifies the SLAX version as 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:

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.

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.

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.

Lines 9 and 10 define variables for the requested action, for example, get or get-next, and the value of the 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.

XSLT Boilerplate for SNMP Scripts

The XSLT boilerplate is:

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.

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.