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 filenamepre
.Prints the results to the terminal of the jsnapy server.
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
- Import Python Modules
- Define a Variable for the Call to SnapAdmin
- Define jsnapy Configuration Parameters
- Call jsnapy With the Defined Configuration Data
- Print the Results to the Terminal
- Results
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 (#).
### Example showing how to pass yaml data in same file ### from jnpr.junos import Device
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.
from jnpr.jsnapy import SnapAdmin from pprint import pprint from jnpr.junos import Device
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.js = SnapAdmin()
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 allowconfig_data
to contain new-line characters.Note:Another way to specify the 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 (""").config_data = """ hosts: - device: 198.51.100.10 username : <username> passwd: <password> tests: - test_exists.yml - test_contains.yml - test_is_equal.yml """
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 thesnapchk
variable.Note:You can access all of the available jsnapy options,
check
,snap
, andsnapcheck
by appending the option after the call to SnapAdmin(). For example,js.check(config_file, "snapshot1", "snapshot2")
,js.snap(config_file, "snapshot_name")
, andjs.snapcheck(config_file, "snapshot_name")
.snapchk = js.snapcheck(config_data, "pre")
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.
for val in snapchk: print "Tested on", val.device print "Final result: ", val.result print "Total passed: ", val.no_passed print "Total failed:", val.no_failed #pprint(dict(val.test_details))
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
### Example showing how to pass yaml data in same file ### from jnpr.jsnapy import SnapAdmin from pprint import pprint from jnpr.junos import Device js = SnapAdmin() config_data = """ hosts: - device: 198.51.100.10 username : <username> passwd: <password> tests: - test_exists.yml - test_contains.yml - test_is_equal.yml """ snapchk = js.snapcheck(config_data, "pre") for val in snapchk: print "Tested on", val.device print "Final result: ", val.result print "Total passed: ", val.no_passed print "Total failed:", val.no_failed #pprint(dict(val.test_details))
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 Connecting to device 198.51.100.10 ................ Taking snapshot of COMMAND: show version Taking snapshot of COMMAND: show version invoke-on all-routing-engines Taking snapshot of COMMAND: show interfaces terse lo* *************************** Device: 198.51.100.10 *************************** Tests Included: test_version_check *************************** Command: show version *************************** PASS | All "//package-information/name" exists at xpath "//software-information" [ 59 matched ] *************************** Device: 198.51.100.10 *************************** Tests Included: test_version_check ************ Command: show version invoke-on all-routing-engines ************ Test Failed!!! Junos version does not contains package name as jbase Test Failed!!! Junos version does not contains package name as jbase FAIL | All "//package-information/name[1]" do not contains j" [ 57 matched / 2 failed ] *************************** Device: 198.51.100.10 *************************** Tests Included: test_interfaces_terse ********************* Command: show interfaces terse lo* ********************* PASS | All "admin-status" is equal to "up" [ 1 matched ] ------------------------------- Final Result!! ------------------------------- Total No of tests passed: 2 Total No of tests failed: 1 Overall Tests failed!!! Tested on 198.51.100.10 Final result: Failed Total passed: 2 Total failed: 1