Using RPCs and Operational Mode Commands in Event Scripts
Most JUNOS 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.
RPC and operational mode command use in event scripts is discussed in more detail in the following sections:
Using RPCs in Event Scripts
You can invoke RPCs in event scripts. For each event script that invokes RPCs, you must include the remote-execution statement at the [edit event-options event-script file filename] hierarchy level and specify the machine where the RPC is executed as remote-hostname. You must also include the username and passphrase statements for each remote machine.
- [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 triggered by an event policy. For more information about the details that are forwarded to the event script, see . A connection handle to the remote host is generated with the jcs:open() function using remote-hostname, username, and passphrase as input parameters; for more information, see . The following code obtains a connection handle for each remote host included in the configuration:
XSLT Syntax
<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:for-each>
SLAX Syntax
for-each (event-script-input/remote-execution-details) {
var $d = remote-execution-detail;
var $connection = jcs:open($d/remote-hostname,$d/username,$d/passphrase);
...
}
After obtaining a connection handle to the remote device, the event script can execute RPCs with the jcs:execute() extension function, which is described in . To use an RPC in the event script, include the RPC in a variable declaration and execute it with the jcs:execute() function; the connection handle and RPC variable declaration are provided as parameters to the jcs:execute() function.
XSLT Syntax
<xsl:variable name="rpc">
<get-interface-information/> # JUNOS RPC for the show interfaces command
</xsl:variable>
<xsl:variable name="out" select="jcs:execute($connection, $rpc)"/>
where $connection is the connection handle to the remote host. Any number of RPCs can be executed within the context of this connection handle until it is closed with the jcs:close() function.
Using Operational Mode Commands in Event Scripts
Some operational mode commands do not have XML equivalents. If a command is not listed in the JUNOS XML API Operational Reference, it 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 xmlIf 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>
An event 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 .
<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>
...


