Use Custom YANG RPCs on Devices Running Junos OS
You can add YANG data models that define custom RPCs on supported devices running Junos OS. Creating custom RPCs enables you to precisely define the input parameters and operations and the output fields and formatting for your specific operational tasks on those devices.
To add an RPC to a device running Junos OS, download the YANG
module that defines the RPC, along with any required action scripts
to the device, and add the files to a new or existing YANG package
by issuing the request system yang add
or request
system yang update
operational command. For detailed information
about adding YANG modules to devices running Junos OS, see Manage YANG Packages, Modules, and Scripts on Junos Devices.
Starting in Junos OS Release 17.3R1, when you load custom YANG data models onto the device, you do not need to explicitly load any required Junos OS extension modules. In earlier releases, you must load the Junos OS extension modules for any packages that use the modules.
After you add the modules and action scripts to the device,
you can execute the RPC either locally, provided that the RPC definition
includes the junos:command
statement, or
remotely. To execute an RPC in the Junos OS CLI, issue the command
defined by the RPC’s junos:command
statement. To execute an RPC remotely, use the RPC name in an RPC
request operation.
Consider the following YANG module and RPC definition:
module sw-info { namespace "http://yang.juniper.net/examples/rpc-cli"; prefix rpc-cli; import junos-extension { prefix junos; } rpc get-sw-info { description "Show software information"; junos:command "show sw-info" { junos:action-execute { junos:script "sw-info.py"; } } input { leaf routing-engine { type string; description "Routing engine for which to display information"; } ... } output { ... } } }
Starting in Junos OS Release 17.3, the action-execute
statement is a substatement to command
. In earlier releases, the action-execute
and command
statements are placed at
the same level, and the command
statement
is optional.
To execute this RPC in the Junos OS CLI, issue the show sw-info
command defined by the junos:command
statement, and include any required or optional input parameters.
For example:
user@host> show sw-info routing-engine re0
To execute this RPC remotely, send an RPC request that uses the RPC name for the request tag, and include any required or optional input parameters.
<rpc> <get-sw-info> <routing-engine>re0</routing-engine> </get-sw-info> </rpc>
When you execute a custom RPC, the device invokes the action
script that is defined in the junos:script
statement, which in this example is the sw-info.py script. An RPC’s action script should emit any necessary XML
elements for that RPC’s output.
When you execute an RPC in the Junos OS CLI by issuing the command
defined by the junos:command
statement,
the device displays the RPC output, if there is any, using the CLI
formatting defined by the RPC. If the RPC does not define CLI formatting,
the device does not display any output for that RPC in the CLI. However,
you can still display the RPC’s XML output in the CLI by appending | display xml
to the command.
user@host> show sw-info routing-engine re0 | display xml
When you execute an RPC remotely, the RPC output defaults
to XML. However, you can specify a different output format by including
the format
attribute in the opening request
tag of the RPC. To display CLI formatting, provided that the RPC defines
this format, set the format
attribute to text
or ascii
. To display
the output in JavaScript Object Notation (JSON), set the format
attribute to json
. For example:
<rpc> <get-sw-info format="text"> <routing-engine>re0</routing-engine> </get-sw-info> </rpc>
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.