Understanding Junos Snapshot Administrator in Python when Running as a Python Module
Junos® Snapshot Administrator in Python (jsnapy) enables you to capture and audit runtime environment snapshots of your networked devices running the Junos OS. Jsnapy can be run from the command line of a network server (jsnapy server) or called as a module from within other python scripts on the jsnapy server. See Junos Snapshot Administrator in Python Overview for details about CLI operation.
Once installed on the network server, jsnapy is ready to be used as a module within other python scripts and programs. No further configuration is needed. Jsnapy retains all of its functionality when used as a module in a python script or program.
In order to use jsnapy as a module inside of another python
script, you must first use the python import
statement so that the python script has access to jsnapy features.
Because the script relies not only on jsnapy but also an understanding
of Junos OS devices when working with snapshots, you must create two
distinct import statements, each importing a different module from
the available Juniper python libraries, jnpr.
from jnpr.jsnapy import SnapAdmin from jnpr.junos import Device
Importing he SnapAdmin module gives the script or program access
to all of the same jsnapy options available on the command line. When
used within the script, the SnapAdmin()
function is usually
assigned to a variable for use later in the script. Arguments are
passed to SnapAdmin()
to control the available jsnapy options.
The following example assigns SnapAdmin to the variable js
and then calls jsnapy with each of the 3 major
options, snap
, check
, and snapcheck
.
Assign the SnapAdmin Function to a Variable
js = SnapAdmin()
Call jsnapy 3 Times
snapshot = js.snap(config_file, "snapshot_filename") chk = js.check(config_file, snapshot_1, snapshot_2) snapchk = js.snapcheck(config_file, "snapshot_filename")
The Device module allows python scripts to be able to connect-to,
login, and run commands on Junos OS devices. You supply connection
IP address, username, and password arguments when calling the Device
module from within python scripts. For example, the following line
from a python script creates a variable called device_object
for use later in the script.
device_object = Device(host='192.0.2.1', user='username', password='password')
The variables named as argument values above can come from a variety of sources.
You can build the configuration file information into the python script by creating a variable in which to store the data and then specifying all of the host and test information needed to connect to and test that device. For example,
config_data = """ hosts: - device: 192.0.2.1 username : username passwd: password tests: - test_exists.yml - test_does_not_contain.yml """
You can specify an existing YAML formatted configuration file that contains connection and test information for the target device. For example:
config_file = /etc/jsnapy/samples/config_single_snapcheck.yml
You can use a combination of a script defined device object for connection purposes and a list of existing YAML formatted test files to test the device. For example, the following line uses the previously defined
device_object
along with a list of tests to run and a name for the snapshot file.js.snapcheck({'tests': ['test_exists.yml', 'test_contains.yml', 'test_is_equal.yml']}, "snapshot_name", dev=device_object)
A wide variety of options are available when running jsnapy from within other Python scripts or programs. Strong familiarity with Python scripting and Junos OS device interaction is helpful in learning what is possible.