Skip to main content

Retrieve cli result and parse it using NSO live status and PyATS parsers

Project description

nso_live_status

This library is an example on how you can retrieve raw cli with Cisco NSO and parse the result with pyats/genie parser.

this code only works with cli based NEDs

Installation

pip install nso-live-status

Usage

In an NSO Action let's say you want to check the version of your devices.

In the python code of the package you will use it like this :

import ncs
from nso_live_status import run_live_status
from distutils.version import LooseVersion
from ncs.dp import Action


CHECK_OK = "OK"
CHECK_NOK = "NOK"
CHECK_ERROR = "ERROR"


class CheckVersion(Action):
    @Action.action
    def cb_action(self, uinfo, name, kp, input, output, trans):
        self.log.info(f"Action CheckVersion , device : {input.device} target version : {input.target_version}")
        command = "show version"
        root = ncs.maagic.get_root(trans)

        show_version = run_live_status(root, input.device, command)

        target_version = LooseVersion(input.target_version)
        if show_version.has_structured_output:
            current_version = LooseVersion(show_version.structured_output["software_version"])
            if current_version == target_version:
                output.check_status = CHECK_OK
                output.check_message = f"[check_version] Current version of the device {current_version} match the target version {target_version}"
            else:
                output.check_status = CHECK_NOK
                output.check_message = f"[check_version] Current version of the device {current_version} doesn't match the target version {target_version}"
        else:
            output.check_status = CHECK_ERROR
            output.check_message = f"[check_version] ERROR unable to retrieve structured output from {input.device} with the command {command}"

run_live_status will populate and return this dataclass :

@dataclass()
class CommandResult:
    has_error: bool = False
    raw_cli: str = ""
    structured_output: str = ""

    @property
    def has_structured_output(self):
        try:
            if len(self.structured_output) > 0:
                return True
            else:
                return False
        except TypeError:
            return False

If a pyats parser is found for the cli command you will find a dict in structured_output

License

This project is licensed to you under the terms of the Cisco Sample Code License.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

nso_live_status-1.0.6-py3-none-any.whl (8.7 kB view details)

Uploaded Python 3

File details

Details for the file nso_live_status-1.0.6-py3-none-any.whl.

File metadata

  • Download URL: nso_live_status-1.0.6-py3-none-any.whl
  • Upload date:
  • Size: 8.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.10.1 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.3

File hashes

Hashes for nso_live_status-1.0.6-py3-none-any.whl
Algorithm Hash digest
SHA256 b09be22086d4b021cf48a66f5267cb5eb6bbfb21477ac6012be6828916c387be
MD5 083a65cc5f2529887b457cebec28b52e
BLAKE2b-256 56a0b36546e6320658a22596636a4e2d10067417075c3dc888690537b2bfeb9d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page