mollerblog.jpg

5 Python Scripts to get you started in NetMRI

 

5 Python Scripts to get you started in NetMRI

Anatomy of a Python Script

Note: Well-known variables for Python scripting are listed in the topic Scripting Well-Known Variables (Perl, Python, and CCS).

Python scripts use a script header similar to a CCS script, contained within a well-known comment block.

The script header block influences a number of runtime behaviors, including:

Script-Timeout

Specifies the per-command timeout for the entire script in seconds.

  • Type: Integer
  • Required: No
  • Default if not specified: 60

Script-Login

Specifies whether the job engine should automatically establish a connection with the target device.

  • Type: Boolean
  • Required: No
  • Default if not specified: true

Script-Variables

  • Specifies inputs needed by the script.
  • Type: Tuple (ordered list of elements)
  • Required: No
  • Default if not specified: None

Script-Filter

Specifies the device types processed by the script.

  • Type: String
  • Required: Yes

A Python Script header block must be defined inside of comments within a "well known" comment section (between # BEGIN-SCRIPT-BLOCK and a # END-SCRIPT-BLOCK). The following example demonstrates the difference between a CCS and Python Script header block that specifies a Script-Filter that applies to all Cisco IOS devices.

As a comparison, a CCS implementation is straightforward:

Script-Filter: $Vendor == "Cisco" and $sysDesc like /IOS/

You can filter by the network view:

Script-Filter: $network == "blue"

A comparable Python implementation is as follows:

# BEGIN-SCRIPT-BLOCK

# Script-Filter: $Vendor == "Cisco" and $sysDesc like /IOS/

# END-SCRIPT-BLOCK

Python scripts run inside the sandbox, using the Pyrthon API to communicate with the Device Interaction Server (DIS) on NetMRI. The DIS proxies CLI requests/responses to/from network devices on behalf of the Python scripts. Before commands can be sent to a network device, a Python script must first establish a DIS session (see v2 API object DisSession). After a session has been established, a CLI connection with the target device must be established for the given session (see v2 API object CliConnection). The majority of the time, a Python script will follow this same initialization sequence. First, establish the API session, establish the DIS session, then establish a CLI connection with the target device. For this reason, netmri_easy.py, a pre-installed Python library, is provided in the appliance (see Configuration Management –> Job Management –> Library –> netmri_easy).

 

 

Script 1

This will show you the example of sending “show version” on a device.  The second command is to show you that we can use the CCS DEBUG option which will not run “show version” on the device but show you that we would have if you remove the DEBUG.

 

# BEGIN-SCRIPT-BLOCK
#
# Script-Filter:
#     true
#
# END-SCRIPT-BLOCK

from infoblox_netmri.easy import NetMRIEasy

# This values will be provided by NetMRI before execution
defaults = {
    "api_url": api_url,
    "http_username": http_username,
    "http_password": http_password,
    "job_id": job_id,
    "device_id": device_id,
    "batch_id": batch_id
}

# Create NetMRI context manager. It will close session after execution
with NetMRIEasy(**defaults) as easy:
    easy.send_command('show version')
    easy.send_command('DEBUG:show version')

 

 

Showing results for 
Search instead for 
Did you mean: