Most operational commands that can be executed manually on the Junos CLI prompt can also be invoked within an automation script. Junos processes the commands in the same manner as if they were run from the CLI. However, Junos generates their output in XML and provides it to the script. The script can then parse the results and perform any desired actions based on the gathered information.
JUNOS XML API
Invoking operational commands in Junos is possible through use of the Junos XML API. The script code sends API Elements to Junos, which then processes the received elements, performs the associated actions, and returns the results to the script.
Many operational commands are mapped directly to an API Element. For example, the
“clear interfaces statistics” CLI command is mapped to
<clear-interfaces-statistics>. Operational commands that do not have a specific API Element can be executed by using the
<command> element with the CLI command as its text content:
<command> "show route";
The Script code sends API elements to Junos by calling the
jcs:invoke() function and providing the element as an argument. The API Element can be expressed either through a result tree fragment variable, or it can be a string containing the API Element’s name. Any results from
jcs:invoke() are returned as a node-set and should be assigned to a variable:
var $clear-statistics-rpc = <clear-interfaces-statistics-all>;
var $results = jcs:invoke( $clear-statistics-rpc );
var $results = jcs:invoke( "clear-interfaces-statistics-all" );
In both cases, the clear interfaces statistics all command is invoked, and any XML results are assigned to the
$results variable. The difference between specifying the API Element in XML versus providing the string name is that XML attributes, text content, and child elements can only be included when the API Element is expressed as an XML data structure assigned to a variable that is passed to