Automation
Automation

Scripting How-To: Displaying script results

by Cordelia on ‎08-10-2015 06:01 PM - edited on ‎09-18-2017 02:35 PM by Administrator Administrator (3,784 Views)

Overview

Displaying results from within Junos Space 13.1R1.6 or later.

Feature

 

Junos Space Release 13.1R1.6 introduced the ability to execute scripts against one or more devices, chassis components, physical or logical interfaces, and so on.

The results of these scripts can then be displayed directly within Junos Space (or within Job Management) in raw format, embedded within <op-script-results> tags, or with a little tweaking inside the script as HTML.

Shown below is a simple slax script that can be imported into Junos Space to display the output of a "show system processes extensive" command.

This particular script does not need to be staged or enabled on a device, as it includes the @ISLOCAL = "true" annotation.   It will be executed locally on the Junos Space appliance and the command sent to the selected device via NETCONF.  There is no need to enter credentials and so on, this is all managed via Junos Space in this example. It will also work transparently with a device that uses key-based authentication versus one that uses credentials-based authentication.

01 version 1.0;
02  
03 ns junos = "http://xml.juniper.net/junos/*/junos";
06  
07 import "../import/junos.xsl";
08 /* Junos Space annotations */
09 /* @CONTEXT = "/device" */
10 /* @NAME = "System Processes" */
11 /* @DESCRIPTION = "show system processes" */
12 /* @ISLOCAL = "true" */
13  
14 var $local = jcs:open();
15 match / {
16   <op-script-results> {
17         var $command = <command> "show system processes extensive";
18         var $results = jcs:execute( $local , $command );
19         copy-of $results;
20         var $close-results = jcs:close( $local);
21   }
22 }

The results, as displayed within Junos Space, can be seen below.

11.png

Note: If you want to quickly supress the results from displaying "Script execution output details" and the <op-script-results> tags, include an empty HTML and body section, as shown below. This has the side effect of still displaying the raw data, but without the extra tags appearing.

01 version 1.0;
02 ns junos = "http://xml.juniper.net/junos/*/junos";
05 import "../import/junos.xsl";
06 /* @CONTEXT = "/device" */
07 /* @NAME = "System Processes" */
08 /* @DESCRIPTION = "show system processes" */
09 /* @ISLOCAL = "true" */
10 var $local = jcs:open();
11 match / {
12   <op-script-results> {
13         var $command = <command> "show system processes extensive";
14         var $results = jcs:execute( $local , $command );
15         copy-of $results;
16     var $close-results = jcs:close( $local);
17     <output> {
18         <html> {
19             <body> {
20             }
21         }
22     }
23   }
24 }

12.png

Finally, with a little creative HTML, the output generated can be formatted into tables and so on, or with specific colors based on conditions that can be defined within the script itself.

The example shown below is for another script, but the approach remains the same.

13.png

A snippet of the HTML code/SLAX script to produce the above results can be seen here:

01 <output> {
02     <HTML> {
03         <HEAD> {
04             copy-of jspace:html-style("4");
05         }
06         <BODY> {
07             <table border="1"> {
08                 <td id="tableheader"> {
09                     expr "Class";
10                 }
11                 <td id="tableheader"> {
12                     expr "Item";
13                 }
14                 <td id="tableheader"> {
15                     expr "Status";
16                 }
17                 <td id="tableheader"> {
18                     expr "Measurement";
19                 }
20                 for-each ( $results/environment-item ) {
21                     <tr> {
22                         <td id="cellcentered"> {
23                             expr (class);
24                         }
25                         <td> {
26                             expr (name);
27                         }
28                         if (status == "OK" ) {
29                             <td id="cellcentered"> {
30                                 expr (status);
31                             }
32                         }
33                         else {
34                             <td id="cellwarning"> {
35                                 expr (status);
36                             }
37                         }
38                         etc. etc. etc.
Source

Original from Andrew Sharp blog post Aug. 23, 2013. Released to TechWiki with permission