- play_arrow Overview
- play_arrow Junos Automation Scripts Overview
- play_arrow Junos XML Management Protocol and Junos XML API Overview
-
- play_arrow Automation Scripting Using XSLT
- play_arrow XSLT Overview
- play_arrow Standard XPath and XSLT Functions Used in Automation Scripts
- play_arrow Standard XSLT Elements and Attributes Used in Automation Scripts
-
- play_arrow Automation Scripting Using SLAX
- play_arrow SLAX Overview
- SLAX Overview
- SLAX Syntax Rules Overview
- SLAX Elements and Element Attributes Overview
- SLAX Elements as Function Arguments
- Understanding SLAX Default Namespaces
- XPath Expressions Overview for SLAX
- SLAX Templates Overview
- SLAX Functions Overview
- SLAX Parameters Overview
- SLAX Variables Overview
- SLAX Statements Overview
- XSLT Elements Without SLAX Equivalents
- SLAX Operators
- play_arrow SLAX Statements
- append
- apply-imports
- apply-templates
- attribute
- attribute-set
- call
- copy-node
- copy-of
- decimal-format
- element
- else
- else if
- expr
- fallback
- for
- for-each
- function
- if
- import
- key
- match
- message
- mode
- mvar
- number
- output-method
- param
- preserve-space
- priority
- processing-instruction
- result
- set
- sort
- strip-space
- template
- terminate
- trace
- uexpr
- use-attribute-sets
- var
- version
- while
- with
- play_arrow The libslax Distribution for Automation Scripting
- libslax Distribution Overview
- libslax Library and Extension Libraries Overview
- Download and Install the libslax Distribution
- libslax Default Extension Libraries: bit, curl, db, os, and xutil
- Understanding the SLAX Processor (slaxproc)
- How to Use the SLAX Processor (slaxproc)
- SLAX Debugger, Profiler, and callflow
-
- play_arrow Automation Scripting Using Python
- play_arrow Python Overview
- Understanding Python Automation Scripts for Junos Devices
- Requirements for Executing Python Automation Scripts on Junos Devices
- Overview of Python Modules on Junos Devices
- How to Use Python Interactive Mode on Devices Running Junos OS
- How to Use the psutil Module to Retrieve Process and System Information on Devices Running Junos OS
- How to Use the Requests Library for Python on Devices Running Junos OS
- IPv6 Support in Python Automation Scripts
- How to Specify the Routing Instance in Python 3 Applications on Devices Running Junos OS Evolved
-
- play_arrow Automation Script Input
- play_arrow Global Parameters in Automation Scripts
-
- play_arrow Extension Functions and Named Templates for Automation Scripts
- play_arrow Extension Functions for Automation Scripting
- play_arrow Extension Functions in the jcs and slax Namespaces
- base64-decode() Function (SLAX)
- base64-encode() Function (SLAX)
- break-lines() Function (SLAX and XSLT)
- close() Function (SLAX and XSLT)
- dampen() Function (Python, SLAX, and XSLT)
- document() Function (SLAX)
- emit_error() Function (Python)
- emit_snmp_attributes Function (Python)
- emit_warning() Function (Python)
- empty() Function (SLAX and XSLT)
- evaluate() Function (SLAX)
- execute() Function (SLAX and XSLT)
- first-of() Function (SLAX and XSLT)
- get-command() Function (SLAX)
- get-hello() Function (SLAX and XSLT)
- get-input() Function (SLAX and XSLT) and get_input() (Python)
- get-protocol() Function (SLAX and XSLT)
- get-secret() Function (SLAX and XSLT) and get_secret() (Python)
- get_snmp_action() Function (Python)
- get_snmp_oid() Function (Python)
- hostname() Function (Python, SLAX, and XSLT)
- invoke() Function (SLAX and XSLT)
- open() Function (SLAX and XSLT)
- output() Function (Python, SLAX, and XSLT)
- parse-ip() Function (SLAX and XSLT) and parse_ip() (Python)
- printf() Function (Python, SLAX, and XSLT)
- progress() Function (Python, SLAX, and XSLT)
- regex() Function (SLAX and XSLT)
- set_routing_instance() Function (Python)
- sleep() Function (SLAX and XSLT)
- split() Function (SLAX and XSLT)
- sysctl() Function (Python, SLAX, and XSLT)
- syslog() Function (Python, SLAX, and XSLT)
- trace() Function (Python, SLAX, and XSLT)
- play_arrow Named Templates for Automation Scripting
- play_arrow Named Templates in the jcs Namespace
-
- play_arrow Manage Automation Scripts
- play_arrow Store and Enable Scripts
- play_arrow Configure a Remote Source for Scripts
- play_arrow Configure the Session Protocol for Scripts
- play_arrow Control Execution of Scripts
- play_arrow Synchronize Scripts Between Routing Engines
- play_arrow Convert Scripts Between SLAX and XSLT
-
- play_arrow Commit Scripts
- play_arrow Commit Scripts Overview
- play_arrow Create and Execute Commit Scripts
- Required Boilerplate for Commit Scripts
- XML Syntax for Common Commit Script Tasks
- Design Considerations for Commit Scripts
- How to Avoid Potential Conflicts When Using Multiple Commit Scripts
- Line-by-Line Explanation of Sample Commit Scripts
- Control the Execution of Commit Scripts During Commit Operations
- Control the Execution of Commit Scripts in the QFabric System
- Configure Checksum Hashes for a Commit Script
- How to Process Large Configurations Against Commit Scripts
- Example: Retrieve the Pre-Inheritance Candidate Configuration in a Commit Script
- play_arrow Generate a Custom Warning, Error, or System Log Message Using Commit Scripts
- Overview of Generating Custom Warning, Error, and System Log Messages
- Generate a Custom Warning, Error, or System Log Message in Commit Scripts
- SLAX and XSLT Commit Script Tag Elements to Use When Generating Messages
- Example: Generate a Custom Warning Message
- Example: Generate a Custom Error Message
- Example: Generate a Custom System Log Message
- play_arrow Generate Persistent or Transient Configuration Changes Using Commit Scripts
- Overview of Generating Persistent or Transient Configuration Changes Using Commit Scripts
- Generate a Persistent or Transient Configuration Change in SLAX and XSLT Commit Scripts
- Generate a Persistent or Transient Configuration Change in Python Commit Scripts
- SLAX and XSLT Commit Script Tag Elements to Use When Generating Persistent and Transient Configuration Changes
- Remove a Persistent or Transient Configuration Change Using Commit Scripts
- Example: Generate Persistent and Transient Configuration Changes Using Commit Scripts
- play_arrow Create Custom Configuration Syntax with Commit Script Macros
- Overview of Creating Custom Configuration Syntax with Commit Script Macros
- Create Custom Configuration Syntax with Commit Script Macros
- Create a Commit Script Macro to Read the Custom Syntax and Generate Related Configuration Statements
- Example: Creating Custom Configuration Syntax with Commit Script Macros
- play_arrow Commit Script Examples
- Example: Adding a Final then accept Term to a Firewall
- Example: Adding T1 Interfaces to a RIP Group
- Example: Assign a Classifier Using a Commit Script
- Example: Automatically Configure Logical Interfaces and IP Addresses
- Example: Configure Administrative Groups for LSPs
- Example: Configure a Default Encapsulation Type
- Example: Configure Dual Routing Engines
- Example: Configure an Interior Gateway Protocol on an Interface
- Example: Control IS-IS and MPLS Interfaces
- Example: Control LDP Configuration
- Example: Create a Complex Configuration Based on a Simple Interface Configuration
- Example: Impose a Minimum MTU Setting
- Example: Limit the Number of ATM Virtual Circuits
- Example: Limit the Number of E1 Interfaces
- Example: Load a Base Configuration
- Example: Prepend a Global Policy
- Example: Prevent Import of the Full Routing Table
- Example: Require Internal Clocking on T1 Interfaces
- Example: Require and Restrict Configuration Statements
- play_arrow Junos XML and XSLT Tag Elements Used in Commit Scripts
- play_arrow Troubleshoot Commit Scripts
-
- play_arrow Event Policies and Event Scripts
- play_arrow Event Policy Overview
- play_arrow Event Policy Triggers
- Use Correlated Events to Trigger an Event Policy
- Trigger an Event Policy Based on Event Count
- Example: Trigger an Event Policy Based on Event Count
- Use Regular Expressions to Refine the Set of Events That Trigger a Policy
- Example: Controlling Event Policy Using a Regular Expression
- Generate Internal Events to Trigger Event Policies
- Use Nonstandard System Log Messages to Trigger Event Policies
- Junos Logger Utility
- play_arrow Event Policy Actions
- Configure an Event Policy to Execute Operational Mode Commands
- Configure an Event Policy to Change the Configuration
- Example: Changing the Configuration Using an Event Policy
- Example: Changing the Interface Configuration in Response to an Event
- Execute Event Scripts in an Event Policy
- Change the Configuration Using an Event Script
- Configuring an Event Policy to Pass Arguments to an Event Script
- Configure Event Policies to Ignore an Event
- Example: Ignore Events Based on Receipt of Other Events
- Overview of Using Event Policies to Raise SNMP Traps
- Example: Raise an SNMP Trap in Response to an Event
- Understanding the Event System Log Priority in an Event Policy
- Example: Configuring the Event System Log Priority in an Event Policy
- Example: Limit Event Script Output Based on a Specific Event Type
- play_arrow Configure Event Policy File Archiving
- play_arrow Configure Event Policy Privileges
- play_arrow Event Scripts Overview
- play_arrow Create and Execute Event Scripts
- play_arrow Troubleshoot Event Policies and Event Scripts
-
- play_arrow SNMP Scripts
- play_arrow SNMP Scripts Overview
- play_arrow Create and Execute SNMP Scripts
- play_arrow SNMP Script Example
- play_arrow Troubleshoot SNMP Scripts
-
- play_arrow Configuration Statements and Operational Commands
Declare and Use Command-Line Arguments in Op Scripts
Junos OS op scripts can accept command-line arguments when you invoke the script. You can include declarations in the op script or statements in the configuration that enable a user to see the list of possible arguments when they request context-sensitive help for the op script in the CLI. The script must also include any necessary declarations and code to process those arguments. The following sections detail how to define the arguments and help text and use the arguments in an op script.
Declaring Op Script Command-Line Arguments
There are two ways to define the list of expected op scripts arguments that will be displayed when using context-sensitive help in the CLI:
Include declarations in the op script
Include statements in the Junos OS configuration
Script-generated and configuration-generated arguments have the same operational impact. The following sections explain how to use the different methods to define the op script arguments and display them in the CLI:
- How to Define Arguments in the Op Script
- How to Define Arguments in the Junos OS Configuration
- How to Display Arguments in the Context-Sensitive Help
How to Define Arguments in the Op Script
You can declare an op script's expected command-line arguments directly in the Python, SLAX, or XSLT op script.
To declare command-line arguments in Python op scripts:
- Declare a global dictionary named
arguments
. - For each argument, define a name-value pair that maps to the argument name and argument help text.
Python Syntax
# Define arguments dictionary arguments = {'name1': 'description1', 'name2': 'description2'} if __name__ == '__main__': ...
To display the arguments in the CLI, Python scripts must include the
if __name__ == '__main__':
statement.
To declare command-line arguments in SLAX or XSLT op scripts:
- Declare a global variable named
arguments
. - For each argument, define an
<argument>
element. - Within each
<argument>
element:- Define the
<name>
element with the name of the argument. - Optionally define a
<description>
element that provides the help text for that argument.
- Define the
XSLT Syntax
<xsl:variable name="arguments"> <argument> <name>name</name> <description>descriptive-text</description> </argument> </xsl:variable>
SLAX Syntax
var $arguments = { <argument> { <name> "name"; <description> "descriptive-text"; } }
How to Define Arguments in the Junos OS Configuration
You can declare an op script's expected command-line arguments in the Junos OS configuration, as an alternative to declaring the arguments directly in the op script.
To declare command-line arguments in the configuration:
- Navigate to the
arguments
statement at the[edit system scripts op file filename]
hierarchy level for the given script. - Configure the argument name.
- Optionally configure the
description
statement to provide the help text for the argument.
For example:
[edit system scripts op op file file filename] arguments { argument-name { description descriptive-text; } }
How to Display Arguments in the Context-Sensitive Help
After you declare arguments in either the op script or the configuration, you can use the CLI's context-sensitive help to list the op script arguments. If you include the optional argument description, the CLI displays the help text with the argument name.
user@host> op filename ? Possible completions: argument-name description argument-name description
You can also create a hidden argument for an op script by not including the argument declaration in the op script or the configuration. You use the argument as you normally would in the script, but the CLI does not display the argument or help text when you request context-sensitive help for that op script.
If you configure command-line arguments in the Junos OS configuration and
also declare arguments directly in the op script, the arguments that you
declare in the script are still available, but the CLI does not list them
under Possible completions
when you issue the op
filename ?
command. This occurs because
the management (mgd) process populates the list by first checking the
configuration for arguments. The mgd process checks the script for arguments
only if no arguments are found in the configuration. Thus, if you declare
arguments in the configuration, any arguments declared in the script become
hidden in the CLI.
For more information about configuring help text for op scripts, see Configure Help Text for Op Scripts.
Using Command-Line Arguments in Op Scripts
You execute local op scripts with the op filename
command. To pass command-line arguments to the script, include each argument name
and value when you execute the script.
user@host> op filename argument-name argument-value
If you specify an argument that the script does not recognize, the script ignores the argument.
The following sections discuss how to use the command-line arguments that are passed to Python, SLAX, and XSLT op scripts:
How to Use Arguments in Python Op Scripts
Python op scripts can use standard command-line parsing libraries to process and
use command-line arguments. For example, you can use the Python
argparse
library to easily define required and optional
arguments, specify default values, and handle the arguments in the script.
To enable users to more easily use the standard Python libraries to parse command-line arguments, we modified the way that the arguments are passed to Python op scripts. Starting in Junos OS Release 21.2R1 and Junos OS Evolved Release 21.2R1, when the device passes command-line arguments to a Python op script, it prefixes a single hyphen (-) to single-character argument names and prefixes two hyphens (--) to multi-character argument names. In earlier releases, the devices prefixes a single hyphen (-) to all argument names. You should ensure that your op script properly handles the arguments for your specific release.
The following examples use the argparse
module to handle the
script arguments. The examples define the global arguments
dictionary, and the dictionary keys are used to define the expected arguments
for the parser. We provide two example scripts, which appropriately handle the
arguments in the specified releases.
Python Syntax (Junos OS Release 21.2R1 or later)
# Junos OS Release 21.2R1 and later import argparse arguments = {'arg1': 'description1', 'arg2': 'description2', 's': 'short option'} def main(): parser = argparse.ArgumentParser(description='This is a demo script.') # Define the arguments accepted by parser # which use the key names defined in the arguments dictionary for key in arguments: if len(key) == 1: parser.add_argument(('-' + key), required=True, help=arguments[key]) else: parser.add_argument(('--' + key), required=True, help=arguments[key]) args = parser.parse_args() # Extract the value print (args.arg1) print (args.arg2) print (args.s) if __name__ == '__main__': main()
Python Syntax (Junos OS Release 21.1 and earlier)
# Junos OS Release 21.1 and earlier import argparse arguments = {'arg1': 'description1', 'arg2': 'description2', 's': 'short option'} def main(): parser = argparse.ArgumentParser(description='This is a demo script.') # Define the arguments accepted by parser # which use the key names defined in the arguments dictionary for key in arguments: parser.add_argument(('-' + key), required=True, help=arguments[key]) args = parser.parse_args() # Extract the value print (args.arg1) print (args.arg2) print (args.s) if __name__ == '__main__': main()
How to Use Arguments in SLAX and XSLT Op Scripts
To use command-line arguments in SLAX or XSLT op scripts, you must:
- Include a parameter declaration for each argument
- Ensure the parameter name is identical to the name that you defined in
either the
arguments
variable declaration in the script or thearguments
statement in the Junos OS configuration.
XSLT Syntax
<xsl:param name="name"/>
SLAX Syntax
param $name;
The op script assigns the value for each script argument to the corresponding parameter, which can then be referenced throughout the script.
Example: Declaring Arguments in XSLT Op Scripts
Declare two arguments named interface
and protocol
. Execute the
script, specifying the ge-0/2/0.0 interface and the inet
protocol as values for the arguments.
The following examples show how to declare the arguments in either the XSLT script or the configuration:
Declaring Arguments in the Op Script (script1)
<xsl:variable name="arguments"> <argument> <name>interface</name> <description>Name of interface to display</description> </argument> <argument> <name>protocol</name> <description>Protocol to display (inet, inet6)</description> </argument> </xsl:variable>
Declaring Arguments in the Configuration
[edit system scripts op] file script1 { arguments { interface { description "Name of interface to display"; } protocol { description "Protocol to display (inet, inet6)"; } } }
In addition to declaring the arguments in the script or the configuration, you must also declare the corresponding parameters in the script in order to reference the script arguments and access their values.
Declaring the Parameters
<xsl:param name="interface"/> <xsl:param name="protocol"/>
Provide the argument names and values when you execute the script. For example:
Executing the Script
user@host> op script1 interface ge-0/2/0.0 protocol inet
Example: Declaring and Using Arguments in Python Op Scripts
Declare two arguments named interface
and p
in the Python op
script. Execute the script, specifying the ge-0/2/0.0 interface and the
inet
protocol as values for the arguments. Select the
appropriate argument handling statements based on your release. The script uses
statements compatible with Junos OS Release 21.2R1 and later and comments out
the statements for handling arguments in older releases.
Declaring Arguments in the Op Script (script1.py)
from jnpr.junos import Device import argparse # Define arguments dictionary arguments = {'interface': 'Name of interface to display', 'p': 'Protocol to display (inet, inet6)'} def main(): parser = argparse.ArgumentParser() # Argument handling for Junos OS Release 21.2R1 or later for key in arguments: if len(key) == 1: parser.add_argument(('-' + key), required=True, help=arguments[key]) else: parser.add_argument(('--' + key), required=True, help=arguments[key]) # Argument handling for Junos OS Release 21.1 and earlier #for key in arguments: # parser.add_argument(('-' + key), required=True, help=arguments[key]) args = parser.parse_args() try: with Device() as dev: res = dev.rpc.get_interface_information( interface_name=args.interface, terse=True, normalize=True) if (res.find("logical-interface/oper-status") is not None): print (args.interface + " status: " + res.findtext("logical-interface/oper-status")) for elem in res.xpath("//address-family \ [normalize-space(address-family-name)=$protocol]", protocol=args.p): if (elem.find("interface-address/ifa-local") is not None): print ("inet address: " + elem.find("interface-address/ifa-local").text) except Exception as err: print (err) if __name__ == '__main__': main()
Alternatively, instead of including the arguments
dictionary in
the Python op script, you can include the arguments in the configuration exactly
as you would for SLAX and XSLT scripts.
To view the op script arguments in the CLI's context-sensitive help, issue the
op filename ?
command.
Displaying the Arguments
user@host> op script1.py ? Possible completions: <[Enter]> Execute this command <name> Argument name detail Display detailed output interface Name of interface to display invoke-debugger Invoke script in debugger mode p Protocol to display (inet, inet6) | Pipe through a command
Provide the argument names and values when you execute the script. For example:
Executing the Script
user@host> op script1.py interface ge-0/2/0.0 p inet ge-0/2/0.0 status: up inet address 198.51.100.1/24
Change History Table
Feature support is determined by the platform and release you are using. Use Feature Explorer to determine if a feature is supported on your platform.