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

Junos OS SNMP scripts can be written 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 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 can be used in XSLT, SLAX, and Python SNMP scripts.

SLAX Boilerplate for SNMP Scripts

The SLAX SNMP script boilerplate is as follows:

Note:

SLAX and XSLT SNMP scripts are based on Junos XML and Junos XML protocol tag elements. Like all XML elements, angle brackets enclose the name of a Junos XML or Junos XML protocol tag element in its opening and closing tags. This is an XML convention, and the brackets are a required part of the complete tag element name. They are not to be confused with the angle brackets used in the documentation to indicate optional parts of Junos OS CLI command strings.

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 the mappings are used in later versions of the script. These namespace mappings enable you to use extension functions and named templates in your scripts. These extension functions and named templates are discussed in Understanding Extension Functions in Junos OS Automation Scripts and 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 http://exslt.org/func/index.html.

Line 7 defines an unnamed template, match /, that represents the top level of the configuration hierarchy. All XML Path Language (XPath) expressions in the script must start at the top-level element in the configuration hierarchy. This allows 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> container tag must be the top-level child tag, as shown in Line 8. The value of this container is returned to the OID requester.

Lines 9 and 10 define variables based on the corresponding elements that you can use in your code to determine whether the action is 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 value of <snmp-oid> is taken from the input value of snmp-script-input/snmp-oid. For the SNMP script feature, the following object identifier types for <snmp-type> are supported:

  • Counter32

  • Counter64

  • Integer32

  • 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, but they must import any objects that are used in the script. 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 that 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 will still execute correctly if one is present.