Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

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).

About netmri_easy.py

The netmri_easy.py provides an interface to the NetMRI API from python scripts run on the NetMRI appliance.

...

The following subsections describe the Python functions encompassed by netmri_easy.py:.

Constructor

The NetMRIEasy  constructor options include the following:

easy = NetMRIEasy( **params );

The Constructor for NetMRI_Easy takes a kwargs for options:

host - api host. Provided by NetMRI

username  - host username. Provided by NetMRI

password - host password. Provided by NetMRI

job_id - Job id. Provided by NetMRI

bacth_id - bacth id. Provided by NetMRI

device_id - device id. Provided by NetMRI

Methods

easy.set_variable( variable_name, variable_value )

Set a server-side variable. This is useful when retrieving a template via get_template and the template contains variables that are not defined by the Script-Variables for the current job. This is because template merging (i.e. variable substitution) is performed on the server-side.

Example

easy.set_variable('username', 'test_username')

This method requires a CLI connection to the device, NetMRI must have credentials for the device the NetMRIEasy script is run against.

easy.send_command( command )

Send a command to the target device.

Example

output = easy.send_command("show version")

This method requires a CLI connection to the device and that the NetMRI have credentials for the device the NetMRIEasy script is run against.

easy->get_template( $template_name )

Retrieve a template. For the given template, retrieve the content and perform any necessary variable substitutions.

Example

contents = easy.get_template("Cisco Router")

This method requires a CLI connection to the device and that the NetMRI have credentials for the device the NetMRIEasy script is run against.

easy.get_list_value(list_name, key_column, key_value, value_column, default)

Look up a value in a list. For the given list name, finds the first row containing the given key value in the given key column and returns the value contained in the given value column. If the look up fails, the given default is returned.

The default is  is returned if no value is found. If default is not specified, then empty string " " is used as the default. This method requires a CLI connection to the device, NetMRI must have credentials for the device the NetMRIEasy script is run against.

easy.generate_issue(severity, issue_type_id, **kwargs)

Generate an issue. For the given issue type id, generate an issue of the given severity using the name/value pairs defined in the given parameters as the issue details.

severity

severity is one of "error", "warning" or "info"

issue_type_id

String issue type id

kwargs

arguments containing the issue details.

Example

issue_id = easy.generate_issue("info", "IOSBanLoginUpdateSuccess", **{

     "Name":'Sample Name',

     "Host":'1.2.3.4'

})

easy.log_message(severity, message)

Log a message of the given severity (one of 'debug', 'error', 'warning' or 'info'). The message is written to the custom.log file.

This method requires a CLI connection to the device and that NetMRI have credentials for the device the NetMRIEasy script is run against.

easy.broker(broker_name)

Get the broker object of the corresponding API controller. Similar to device or cli_connection. Broker objects can be used to query and modify objects on the NetMRI appliance.

...