Help us improve your experience.

Let us know what you think.

Do you have time for a two-minute survey?

header-navigation
keyboard_arrow_up
close
keyboard_arrow_left
Automation Scripting User Guide
Table of Contents Expand all
list Table of Contents
file_download PDF
{ "lLangCode": "en", "lName": "English", "lCountryCode": "us", "transcode": "en_US" }
English
keyboard_arrow_right

Example: Generate a Custom Warning Message

date_range 19-Nov-24

Junos OS commit scripts can generate custom warning messages during a commit operation to alert you when the configuration does not comply with custom configuration rules. The commit process is not affected by warnings. This example creates a commit script that generates a custom warning message when a specific statement is not included in the device configuration.

Requirements

Junos OS Release 16.1R3 or later release when using a Python script.

Overview and Commit Script

Using a commit script, write a custom warning message that appears when the source-route statement is not included at the [edit chassis] hierarchy level.

The script is shown in XSLT, SLAX, and Python.

XSLT Syntax

content_copy zoom_out_map
<?xml version="1.0" standalone="yes"?>
<xsl:stylesheet version="1.0"
    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">
    <xsl:import href="../import/junos.xsl"/>
 
    <xsl:template match="configuration">
        <xsl:if test="not(chassis/source-route)">
            <xnm:warning>
                <xsl:call-template name="jcs:edit-path">
                    <xsl:with-param name="dot" select="chassis"/>
                </xsl:call-template>
                <message>IP source-route processing is not enabled.</message>
            </xnm:warning>
        </xsl:if>
    </xsl:template>
</xsl:stylesheet>

SLAX Syntax

content_copy zoom_out_map
version 1.2;
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";
 
match configuration {
    if (not(chassis/source-route)) {
        <xnm:warning> {
            call jcs:edit-path($dot = chassis);
            <message> "IP source-route processing is not enabled.";
        }
    }
}

Python Syntax

content_copy zoom_out_map
from junos import Junos_Configuration
import jcs

def main():
    root = Junos_Configuration
    if not(root.xpath("./chassis/source-route")):
        jcs.emit_warning("IP source-route processing is not enabled.")

if __name__ == '__main__':
    main()

Configuration

Procedure

Step-by-Step Procedure

Download, enable, and test the script. To test that a commit script generates a warning message correctly, make sure that the candidate configuration contains the condition that elicits the warning. For this example, ensure that the source-route statement is not included at the [edit chassis] hierarchy level.

To test the example in this topic:

  1. Copy the script into a text file, name the file source-route.xsl, source-route.slax, or source-route.py as appropriate, and copy it to the /var/db/scripts/commit/ directory on the device.

    Note:

    Unsigned Python scripts must be owned by either root or a user in the Junos OS super-user login class, and only the file owner can have write permission for the file.

  2. In configuration mode, configure the file statement and the script filename at the [edit system scripts commit] hierarchy level.

    content_copy zoom_out_map
    [edit]
    user@host# set system scripts commit file source-route.xsl
    
  3. If the script is written in Python, enable the execution of unsigned Python scripts.

    content_copy zoom_out_map
    [edit]
    user@host# set system scripts language python
    
    Note:

    Configure the language python3 statement to use Python 3 to execute Python scripts, or configure the language python statement to use Python 2.7 to execute Python scripts. For more information, see language.

  4. If the source-route statement is included at the [edit chassis] hierarchy level, issue the delete chassis source-route configuration mode command.

    content_copy zoom_out_map
    [edit]
    user@host# delete chassis source-route
    

Verification

Verifying Script Execution

Purpose

Verify the warning message generated by the commit script.

Action

Execute the commit check or commit command and review the output. The commit script generates a warning message when the source-route statement is not included at the [edit chassis] hierarchy level of the configuration. The warning does not affect the commit process.

content_copy zoom_out_map
[edit]
user@host# commit check
[edit chassis]
    warning: IP source-route processing is not enabled.
configuration check succeeds
content_copy zoom_out_map
[edit]
user@host# commit
[edit chassis]
    warning: IP source-route processing is not enabled.
 commit complete

To display the XML-formatted version of the warning message, issue the commit check | display xml command.

content_copy zoom_out_map
[edit]
user@host# commit check | display xml
<rpc-reply xmlns:junos="http://xml.juniper.net/junos/10.0R1/junos">
    <commit-results>
        <routing-engine junos:style="normal">
            <name>re0</name>
            <xnm:warning>
                <edit-path>
                    [edit chassis]
                </edit-path>
                <message>
                    IP source-route processing is not enabled.
                </message>
            </xnm:warning>
            <commit-check-success/>
        </routing-engine>
    </commit-results>
</rpc-reply>

To display a detailed trace of commit script processing, issue the commit check | display detail command.

content_copy zoom_out_map
[edit]
user@host# commit check | display detail
2009-06-15 14:40:29 PDT: reading commit script configuration
2009-06-15 14:40:29 PDT: testing commit script configuration
2009-06-15 14:40:29 PDT: opening commit script '/var/db/scripts/commit/source-route-warning.xsl'
2009-06-15 14:40:29 PDT: reading commit script 'source-route-warning.xsl'
2009-06-15 14:40:29 PDT: running commit script 'source-route-warning.xsl'
2009-06-15 14:40:29 PDT: processing commit script 'source-route-warning.xsl'
[edit chassis]
    warning: IP source-route processing is not enabled.
2009-06-15 14:40:29 PDT: no errors from source-route-warning.xsl
2009-06-15 14:40:29 PDT: saving commit script changes
2009-06-15 14:40:29 PDT: summary: changes 0, transients 0 (allowed), syslog 0
2009-06-15 14:40:29 PDT: no commit script changes
2009-06-15 14:40:29 PDT: exporting juniper.conf
2009-06-15 14:40:29 PDT: expanding groups
2009-06-15 14:40:29 PDT: finished expanding groups
2009-06-15 14:40:29 PDT: setup foreign files
2009-06-15 14:40:29 PDT: propagating foreign files
2009-06-15 14:40:30 PDT: complete foreign files
2009-06-15 14:40:30 PDT: daemons checking new configuration
configuration check succeeds
footer-navigation