Help us improve your experience.

Let us know what you think.

Do you have time for a two-minute survey?

Example: Use Jsnapy as a Python module

 

This example shows how to use the features of Junos Snapshot Administrator in Python (jsnapy) in a python script or program. When username and password entries are required in this script, replace <username> and <password> with appropriate values.

A default installation of jsnapy includes many sample configuration and test files in the /etc/jsnapy/samples/ directory, including several Python script examples named module_check.py, module_data.py, module_device.py, and module_snapcheck.py. These files each demonstrate different features of jsnapy running as a module within a python script.

This example will use a slightly modified version of the Python script file, module_data.py, to demonstrate running jsnapy with the snapcheck option. The script demonstrates how to pass YAML configuration data from within the Python script.

Requirements

This example uses the following hardware and software components:

  • A device running Junos OS

  • An instance of jsnapy installed on a server (jsnapy server)

  • A text editor with which to view and change the script

Before you write your own Python scripts for jsnapy, be sure you have a thorough understanding of both Python programming concepts and jsnapy operation.

Overview

In this example, we examine the provided Python script /etc/jsnapy/samples/module_data.py. We will see how the script imports jsnapy as a module, how it defines jsnapy configuration parameters, and how it displays the results of the snapcheck operation on a remote Junos OS device.

Topology

This example uses a simple topology where the jsnapy server connects to a single remote Junos OS device. Connections to more than one device can be achieved but will not be discussed in this example.

Examining the Python Script

The python script performs these tasks:

  • Imports the required Python modules into the script.

  • Defines a variable that is used to call the SnapAdmin() function from the jsnapy module.

  • Defines the jsnapy configuration parameters and assigns them to a variable.

  • Calls jsnapy with the snapcheck option using the previously defined configuration variable as the configuration argument and the snapshot filename pre.

  • Prints the results to the terminal of the jsnapy server.

Note

In its original form, the Python script, module_data.py, imports and calls the Data pretty printer module, pprint. When called from the script, this module causes python to print all of the snapshot data that jsnapy receives from the Junos OS device to the terminal of the jsnapy server. Due to the length of this data, the call to pprint is commented out for this example by prepending the call with the pound sign (#).

Note the Comments

Step-by-Step Procedure

  • Because we are examining an existing script in this example, we will point out the comments that appear as the first line of the file. Comments can be placed anywhere within the script by beginning the comment with the pound sign (#).

Import Python Modules

Step-by-Step Procedure

  • This script uses three imported modules, one for jsnapy, one for printing complex data to the terminal, and one for working with Junos OS devices.

Define a Variable for the Call to SnapAdmin

Step-by-Step Procedure

  • Here the script assigns the SnapAdmin() function from the jsnapy module to a variable named js for ease of use and to be able to pass arguments when calling jsnapy.

Define jsnapy Configuration Parameters

Step-by-Step Procedure

  • Here the script defines the Junos OS host and which tests will be performed on that host. It assigns these parameters to the variable config_data. The triple quotes allow config_data to contain new-line characters.Note

    Another way to specify the Junos Os hosts and tests to be performed is to reference a jsnapy configuration file. The configuration file can define one or more hosts and one or more test files. Because the configuration data is contained within the python script in this example, the config_data variable does not reference a configuration file outside of the script but is filled with the key-value pairs that appear between the triple-quotes (“ “ “).

Call jsnapy With the Defined Configuration Data

Step-by-Step Procedure

  • Here the script calls jsnapy with the snapcheck option and passes the configuration information and the name of the snapshot file. The information returned as a result of this call is stored as values in the snapchk variable.Note

    You can access all of the available jsnapy options, check, snap, and snapcheck by appending the option after the call to SnapAdmin(). For example, js.check(config_file, “snapshot1”, “snapshot2”), js.snap(config_file, “snapshot_name”), and js.snapcheck(config_file, “snapshot_name”).

Print the Results to the Terminal

Step-by-Step Procedure

  • Here the script loops through the returned values and prints them to the jsnapy server terminal in a readable format. This is also where we alter the script to prevent it from printing the entire snapshot to the terminal.

Results

For ease of viewing, the contents of the modified script are shown below by running the cat command on the file module_data.py.

user@jsnapy-server$ cat module_data.py

Verification

Verifying the Operation of the Script

Purpose

Once your Python script and the needed jsnapy configuration and test files are complete, you can verify the operation of the script by running it from the jsnapy server terminal.

Action

user@jsnapy-server$ python module_data.py

Meaning

Troubleshooting

To troubleshoot [item], perform these tasks:

Troubleshooting [item]

Problem

Solution