Automation
Automation

Scripting How-To: Open an SSH session from within a SLAX script

by Cordelia on ‎08-10-2015 07:10 PM - edited on ‎09-19-2017 07:43 AM by Administrator Administrator (2,576 Views)

Overview

Allows Junos Space 13.2 and later to open an SSH session to execute a command on multiple devices.

Feature

Notes:

This functionality will only be available in Junos Space 13.2 , and is not possible within any earlier versions as it requires a newer version of libslax/juise that will be included in Junos Space 13.2.

In addition, this functionality only appeared very late in the 13.2 development, and therefore a very recent build of 13.2 was used to test this functionality.

The version information for juise included within Space 13.2 is as follows:

1 juise --version
2 libjuice version 0.5.9
3 libslax version 0.17.2
4 Using libxml 20901, libxslt 10128 and libexslt 817
5 juise was compiled against libxml 20708, libxslt 10128 and libexslt 817
6 libxslt 10128 was compiled against libxml 20708
7 libexslt 817 was compiled against libxml 20708

The referenced script when installed in Junos Space 13.2, provides the user the ability to select one or more managed Junos devices from within the device management view, and execute the script "shell-exec.slax".

The script expects a single parameter, which is the command to be executed across the selected devices.  Once the script is executed, it connects to each device in turn and executes the command and displays the results in a single results window.

There is no requirement to provide user credentials for the SSH connections, as that information is supplied by Junos Space to the script behind the scenes.

In addition, the script also creates a menu item "EXEC Command" within Junos Space, however due to current limitations, this menu item will only be visible if a single device is selected, and not when multiple devices are selected within the device management view.   Selecting the "EXEC Command" menu item from within the "Device Operations" menu will then produce a script execution window where the command to be executed can be supplied and the script can then be executed.

A brief explanation of the Junos Space specific annotations that are demonstrated with this script follows:

To declare the script as a local script that is executed on the Junos Space platform the following annotation is used.

1 /* @ISLOCAL = "true" */

To declare the name of the script the following annotation is used, in addition this annotation also defines the menu name that the script will be called by.

1 /* @NAME = "EXEC Command" */

To declare a description for the script the following annotation is used.

1 /* @DESCRIPTION = "slax script demonstrates ssh" */

To enable the script name to be promoted to a menu item then following annotation is used.

1 /* @PROMOTE = "yes" */

To permit the script to be executable against multiple selected devices the followin annotation is used.

1 /* @EXECUTIONTYPE = "GROUPEDEXECUTION" */

To pass the device credentials for the selected devices to the script so that it can handle connections to multiple devices the following annotation is used.

1 /* @PASSDEVICECREDENTIALS = "true" */

The context "//" allows the script to be executed from multiple locations such as the Device Management view, Physical Inventory View, Physical and Logical interface views etc.

1 /* @CONTEXT = "//" */

The script displays a drop-down list of pre-defined commands to be executed, this behaviour is defined using the following annotation.

1 /* @VARIABLECONTEXT = "[{'name':'COMMAND','defaultvalue':'show version','selectionvalues':'show version,show system processes summary'}]" */

If a drop-down selection list is not required, remove the entire @VARIABLECONTEXT annotation and the default behaviour will be a blank text entry field.   Although with the selection list it is possible

to edit the command displayed prior to executing the script, so it is a useful way to provide some predefined commands.

The SSH session itself is made possible with the following:

1 var $conn_info := {
2       <username> "username";
3       <passphrase> "password";
4       <method> "shell";
5       <header-timeout> 1;
6 }
7 var $conn = jcs:open( "x.x.x.x", $conn_info);

As can be seen, it is using

jcs:open()

, but using a "shell" method.

Commands are sent to the connection using

1 jcs:send($conn , "command.....")

Data is received from the shell session using

1 jcs:receive($conn)

Example

Shown below are some screenshots of the script executed within Junos Space.

3.png

4.png

5.png

6.png

Script Example

 

Source

Original from Andrew Sharp blog post Jan. 11, 2014. Released toTechWiki with permission.