- 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 Op Scripts
- play_arrow Op Scripts Overview
- play_arrow Create and Execute Op Scripts
- Required Boilerplate for Op Scripts
- Map Operational Mode Commands and Output Fields to Junos XML Notation
- How to Use RPCs and Operational Mode Commands in Op Scripts
- Declare and Use Command-Line Arguments in Op Scripts
- Configure Help Text for Op Scripts
- Define Operational Mode Commands to Allow in an Op Script
- Enable an Op Script and Define a Script Alias
- Configure Checksum Hashes for an Op Script
- Execute an Op Script on the Local Device
- Execute an Op Script from a Remote Site
- Disable an Op Script
- play_arrow Op Script Examples
- Change the Configuration Using SLAX and XSLT Scripts
- Example: Change the Configuration Using SLAX and XSLT Op Scripts
- Example: Change the Configuration Using Python Op Scripts
- Example: Customize Output of the show interfaces terse Command Using an Op Script
- Example: Display DNS Hostname Information Using an Op Script
- Example: Find LSPs to Multiple Destinations Using an Op Script
- Example: Restart an FPC Using an Op Script
- Example: Export Files Using an Op Script
- Example: Import Files Using an Op Script
- Example: Search Files Using an Op Script
- play_arrow Provision Services Using Service Template Automation
- play_arrow Troubleshoot Op 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
How to Use RPCs and Operational Mode Commands in Event Scripts
Most Junos OS operational mode commands have XML equivalents. Event scripts can execute these XML commands on a local or remote device using the remote procedure call (RPC) protocol. All operational mode commands that have XML equivalents are listed in the Junos XML API Operational Developer Reference.
SLAX and XSLT scripts execute RPCs on a local or remote device
by using the jcs:invoke()
or jcs:execute()
extension functions, respectively. In
Python scripts, RPCs are easy to execute using Junos PyEZ APIs. Each instance of the Junos PyEZ Device
class has an rpc
property
that enables you to execute any RPC available through the Junos XML
API. After establishing a session with a local or remote device, you
can execute the RPC by appending the rpc
property and RPC method name to the device instance. The return
value is an XML object starting at the first element under the <rpc-reply>
tag.
Use of RPCs and operational mode commands in event scripts is discussed in more detail in the following sections:
Executing RPCs on a Local Device
In a SLAX or XSLT event script, to execute an RPC on the local
device, include the RPC in a variable declaration, and call the jcs:invoke()
extension function with the RPC variable
as an argument. The following snippet invokes an RPC on the local
device:
XSLT Syntax
<xsl:variable name="rpc"> <get-interface-information/> # Junos RPC for the show interfaces command </xsl:variable> <xsl:variable name="out" select="jcs:invoke($rpc)"/> ...
SLAX Syntax
var $rpc = <get-interface-information>; var $out = jcs:invoke($rpc);
In a Python event script, to execute an RPC on the local device,
create the Device
instance using an empty
argument list, and append the rpc
property
and the RPC method name and argument list to the device instance.
Python Syntax
from jnpr.junos import Device with Device() as jdev: rsp = jdev.rpc.get_interface_information()
When you create the Device
instance
using an empty argument list to connect to the local device, Junos
OS uses the access privileges of the user configured at the [edit
event-options event-script file filename python-script-user]
hierarchy level. If the python-script-user
statement is omitted, Junos OS uses the access privileges of the
generic, unprivileged user and group nobody
.
Executing RPCs on a Remote Device
In a SLAX or XSLT event script, to execute an RPC on a remote
device, first include the RPC in a variable declaration, and create
a connection handle using the jcs:open()
extension function with the arguments required to connect to the
remote device. Then call the jcs:execute()
extension function and include the connection handle and RPC variable
as arguments. For example:
XSLT Syntax
<xsl:variable name="rpc"> <get-interface-information/> </xsl:variable> <xsl:variable name="connection" select="jcs:open('198.51.100.1', 'bsmith', 'test123')"/> <xsl:variable name="out" select="jcs:execute($connection, $rpc)"/> <xsl:value-of select="jcs:close($connection)"/> ...
SLAX Syntax
var $rpc = <get-interface-information>; var $connection = jcs:open("198.51.100.1", "bsmith","test123"); var $out = jcs:execute($connection, $rpc); expr jcs:close($connection); ...
In a Python event script, to execute an RPC on a remote device,
first create an instance of Device
using
the arguments required to connect to the remote device. Then execute
the RPC by appending the rpc
property and
the RPC method name and argument list to the device instance.
Python Syntax
from jnpr.junos import Device with Device(host='198.51.100.1', user='bsmith', passwd='test123') as jdev: rsp = jdev.rpc.get_interface_information()
Junos OS connects to and executes operations on the remote device
using the access privileges of the user specified in the Device()
argument list, even if a different user is
configured for the python-script-user
statement
at the [edit event-options event-script file filename]
hierarchy level.
To avoid adding the remote connection details directly into
an event script, you can specify remote execution details for each
event script that executes RPCs on a remote device at the [edit
event-options event-script file filename remote-execution]
hierarchy level. We recommend adding the
remote execution details to the configuration instead of directly
in the event script, because all of the information is available in
a single location, and the passphrase is encrypted in the configuration.
For each remote device where an RPC is executed, configure the device hostname and the corresponding username and passphrase.
[edit event-options event-script file filename] remote-execution { remote-hostname { username username; passphrase passphrase; } }
The remote hostnames and their corresponding username and passphrase, in addition to the event details, are passed as input to the event script when it is executed by an event policy. For more information about the details that are forwarded to the event script, see Use Event and Remote Execution Details in Event Scripts.
An event script references the remote execution details in the argument list of the function used to create the connection to the remote host. Once the connection has been established, the script can execute RPCs on that device.
In Python event scripts, you reference the remote execution
details in the argument list of the Junos PyEZ Device()
instance. The following code iterates over the remote execution
details for all hosts configured for that event script and connects
to and executes the same RPC on each host.
Python Syntax
from junos import Junos_Remote_Execution_Details from jnpr.junos import Device def main() for remote in Junos_Remote_Execution_Details(): hostname = remote.host username = remote.user passphrase = remote.passwd with Device(host=hostname, user=username, passwd=passphrase) as jdev: inv = jdev.rpc.get_interface_information() #process RPC information... if __name__ == "__main__": main()
In SLAX or XSLT scripts, create a connection to the remote host
by using the jcs:open()
function and reference
the remote execution details in the argument list. For example:
XSLT Syntax
<xsl:variable name="rpc"> <get-interface-information/> </xsl:variable> <xsl:for-each select="event-script-input/remote-execution-details"> <xsl:variable name="d" select="remote-execution-detail"/> <xsl:variable name="connection" select="jcs:open($d/remote-hostname,$d/username,$d/passphrase)"/> <xsl:variable name="out" select="jcs:execute($connection, $rpc)"/> <xsl:value-of select="jcs:close($connection)"/> ... </xsl:for-each>
SLAX Syntax
var $rpc = <get-interface-information>; for-each (event-script-input/remote-execution-details) { var $d = remote-execution-detail; var $connection = jcs:open($d/remote-hostname,$d/username,$d/passphrase); var $out = jcs:execute($connection, $rpc); expr jcs:close($connection); ... }
To execute an RPC on a remote device, an SSH session must be established. In order for the script to establish the connection, you must either configure the SSH host key information for the remote device on the local device where the script will be executed, or the SSH host key information for the remote device must exist in the known hosts file of the user executing the script. For each remote device where the RPC is executed, configure the SSH host key information using one of the following methods:
To configure SSH known hosts on the local device, include the
host
statement, and specify hostname and host key options for the remote device at the[edit security ssh-known-hosts]
hierarchy level of the configuration.To manually retrieve SSH host key information, issue the
set security ssh-known-hosts fetch-from-server hostname
configuration mode command to instruct Junos OS to connect to the remote device and add the key.content_copy zoom_out_mapuser@host# set security ssh-known-hosts fetch-from-server router2 The authenticity of host 'router2 (198.51.100.1)' can't be established. RSA key fingerprint is 30:18:99:7a:3c:ed:40:04:0f:fd:c1:57:7e:6b:f3:90. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added 'router2,198.51.100.1' (RSA) to the list of known hosts.
To manually import SSH host key information from a file, use the
set security ssh-known- hosts load-key-file filename
configuration mode command and specify the known-hosts file.content_copy zoom_out_mapuser@host# set security ssh-known-hosts load-key-file /var/tmp/known_hosts Import SSH host keys from trusted source /var/tmp/known_hosts ? [yes,no] (no) yes
Alternatively, the user executing the script can log in to the local device, SSH to the remote device, and then manually accept the host key, which is added to that user’s known hosts file. In the following example, root is logged in to router1. In order to execute a remote RPC on router2, root adds the host key of router2 by issuing the
ssh router2
operational mode command and manually accepting the key.content_copy zoom_out_maproot@router1> ssh router2 The authenticity of host 'router2 (198.51.100.1)' can't be established. RSA key fingerprint is 30:18:99:7a:3c:ed:40:04:0f:fd:c1:57:7e:6b:f3:90. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added 'router2,198.51.100.1' (RSA) to the list of known hosts.
After configuring the required SSH host key and obtaining a
connection handle to the remote device, the event script can execute
RPCs with the jcs:execute()
extension function
on that remote device.
Displaying the RPC Tags for a Command
You can display the RPC XML tags for operational mode commands
in the CLI of the device. To display the RPC XML tags for a command,
enter display xml rpc
after the pipe symbol ( | ).
The following example displays the RPC tags for the show
route
command:
user@host> show route | display xml rpc <rpc-reply xmlns:junos="http://xml.juniper.net/junos/10.1I0/junos"> <rpc> <get-route-information> </get-route-information> </rpc> <cli> <banner></banner> </cli> </rpc-reply>
SLAX and XSLT scripts can execute RPCs using the RPC XML tags. Python scripts must convert the RPC tags and command options into a format suitable for Python. For more information about using Junos PyEZ to execute RPCs and about mapping RPC tags to the corresponding Python method and method arguments, see Using Junos PyEZ to Execute RPCs on Devices Running Junos OS.
Using Operational Mode Commands in Event Scripts
Some operational mode commands do not have XML equivalents.
SLAX and XSLT scripts can execute commands that have no XML equivalent
by using the <command>
element. Python
scripts can execute these commands by using the Junos PyEZ cli()
method defined in the Device
class.
If a command is not listed in the Junos XML API Operational Developer Reference,
the command does not have an XML equivalent. Another way to determine
whether a command has an XML equivalent is to issue the command followed
by the | display xml
command.
user@host> operational-mode-command | display xml
If the output includes only tag elements like <output>
, <cli>
, and <banner>
, the command might not have an XML equivalent. In the following
example, the output indicates that the show host
command
has no XML equivalent:
user@host> show host hostname | display xml <rpc-reply xmlns:junos="http://xml.juniper.net/junos/10.0R1/junos"> <output> ... </output> <cli> <banner></banner> </cli> </rpc-reply>
For some commands that have an XML equivalent, the output of
the piped | display xml
command does not include
tag elements other than <output>
, <cli>
, and <banner>
only because the relevant feature is not configured. For example,
the show services cos statistics forwarding-class
command
has an XML equivalent that returns output in the <service-cos-forwarding-class-statistics>
response tag, but if the configuration does not include any statements
at the [edit class-of-service]
hierarchy level, then there
is no actual data for the show services cos statistics forwarding-class |
display xml
command to display. The output is similar to this:
user@host> show services cos statistics forwarding-class | display xml <rpc-reply xmlns:junos="http://xml.juniper.net/junos/8.3I0/junos"> <cli> <banner></banner> </cli> </rpc-reply>
For this reason, the information in the Junos XML API Operational Developer Reference is usually more reliable.
SLAX and XSLT event scripts can execute commands that have no
XML equivalent. Use the <command>
, <xsl:value-of>
, and <output>
elements in the script, as shown in the following code snippet.
This snippet is expanded and fully described in Example: Display DNS Hostname Information Using an Op Script.
<xsl:variable name="query"> <command> <xsl:value-of select="concat('show host ', $hostname)"/> </command> </xsl:variable> <xsl:variable name="result" select="jcs:invoke($query)"/> <xsl:variable name="host" select="$result"/> <output> <xsl:value-of select="concat('Name: ', $host)"/> </output> ...
Python event scripts can execute commands that have no XML equivalent
by using Junos PyEZ APIs. The cli()
method
defined in the Device
class executes an operational mode
command and returns the output in text format. For example:
from jnpr.junos import Device def main(): with Device() as jdev: res = jdev.cli('show host hostname', warning=False) print (res) if __name__ == "__main__": main()
You can also specify format='xml'
to return the output formatted as Junos OS XML elements. For more
information about the Junos PyEZ cli
method,
see http://junos-pyez.readthedocs.org/en/latest/_modules/jnpr/junos/device.html#Device.cli .