How to Use RPCs and Operational Mode Commands in Op Scripts
Most Junos OS operational mode commands have XML equivalents. Op 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.
Use of RPCs and operational mode commands in op scripts is discussed in more detail in the following sections:
Using RPCs in Op Scripts
To use an RPC in a SLAX or XSLT op script, include the RPC in
a variable declaration, and then invoke the RPC using the jcs:invoke()
or jcs:execute()
extension function with RPC variable as an argument. The jcs:invoke()
function executes the RPC on the local
device. The jcs:execute()
function, in
conjunction with a connection handle, executes the RPC on a remote
device.
The following snippet, which invokes an RPC on the local device, is expanded and fully described in Example: Customize Output of the show interfaces terse Command Using an Op Script:
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);
The following snippet invokes the same RPC on a remote device:
XSLT Syntax
<xsl:variable name="rpc"> <get-interface-information/> # Junos RPC for the show interfaces command </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 Python op 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.
To execute the RPC on the local device, create the Device
instance using an empty argument list. To execute
the RPC on a remote device, create an instance of Device
using the appropriate arguments to connect to that device.
The following code invokes an RPC on the local device and prints the reply:
Python Syntax
from jnpr.junos import Device from lxml import etree with Device() as jdev: rsp = jdev.rpc.get_interface_information() print (etree.tostring(rsp, encoding='unicode'))
The following code invokes the same RPC on a remote device and prints the reply:
Python Syntax
from jnpr.junos import Device from lxml import etree import jcs user = jcs.get_input('Enter username: ') password = jcs.get_secret('Enter user password: ') with Device(host='198.51.100.1', user=user, password=password) as jdev: rsp = jdev.rpc.get_interface_information() print (etree.tostring(rsp, encoding='unicode'))
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 an RPC is executed, configure the SSH host key information with 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.user@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.user@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.root@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.
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 Op Scripts
Some operational mode commands do not have XML equivalents.
SLAX and XSLT scripts can execute commands that have no XML equivalent
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, for example:
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 op scripts can include 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 op 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 .