Using RPCs and Operational Mode Commands in Op Scripts

Most Junos OS operational mode commands have XML equivalents. These XML commands can be executed remotely using the remote procedure call (RPC) protocol. All operational mode commands that have XML equivalents are listed in the Junos XML API Operational Reference.

Use of RPC 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 an op script, include the RPC in a variable declaration, as shown in the following code snippet. Invoke the RPC with the jcs:invoke() or jcs:execute() extension function and include the RPC variable as a parameter. The jcs:invoke function executes the RPC on the local device. You can use the jcs:execute function with a connection handle to execute the RPC on a remote machine. The following snippet is expanded and fully described in Example: Customizing 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);

Using Operational Mode Commands in Op Scripts

Some operational mode commands do not have XML equivalents. If a command is not listed in the Junos XML API Operational 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>

Note: 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 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 something like 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 Reference is normally more reliable.

An op script 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: Displaying 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>
...