Netutils

repository·develop·Indexed 42 days ago

https://github.com/networktocode/netutils

A lightweight Python library providing utility functions for common network automation tasks, including ASN conversion, interface normalization, and password encryption. Version 1.19.2a0 includes modules for DNS operations, ping functionality, and NIST URL generation, as well as a framework for parsing network configuration files.

Tokens
57K
Snippets
141
Records
180
Agent score
54%

What's inside Netutils

  1. Overview of the netutils library

    develop

    netutils is a lightweight Python library providing a collection of functions designed for common network automation tasks. It aims to centralize utility logic—such as BGP ASN conversions, interface name normalization, and password encryption—to reduce code duplication in network automation scripts.

    Key characteristics of the library include:

    • Zero mandatory dependencies: The library does not require any external packages to run, though it may support optional dependencies that the user can opt into.
    • Function-centric design: The library prefers standalone functions over complex class hierarchies.
    • Flat structure: It utilizes a flat folder and file organization for simplicity.
    • Docstring-driven documentation: Every public function includes examples within its docstrings to guide usage.
  2. Overview of Netutils

    develop

    Netutils is a lightweight Python library providing a centralized collection of functions for common network automation tasks. It is designed to simplify repetitive operations such as:

    • Converting BGP ASNs to and from dotted format.
    • Normalizing interface names.
    • Performing "type 5" password encryption.

    The library aims to provide these utilities without adding significant overhead to your automation workflows.

  3. Understand netutils optional dependencies

    develop

    To keep the core library lean, netutils uses an opt-in dependency model. Many powerful features require additional libraries to be installed separately.

    If you attempt to use a function that relies on an optional dependency that is not installed (for example, calling get_napalm_getters() without having napalm installed), the function will raise an exception and warn you that the required library is missing.

  4. Handle duplicate service names in Protocol Mappers

    develop

    Because IANA does not always provide a strict name-to-number mapping, service names may be duplicated across different port numbers. To handle this, netutils appends -secondary to the service name for subsequent occurrences.

    For example, if compressnet is mapped to port 2 and port 3, the internal representation will look like:

    • compressnet: port 2
    • compressnet-secondary: port 3
  5. How OUI mapping imports work in netutils

    develop

    To optimize performance and avoid loading large datasets unnecessarily, netutils uses lazy loading for OUI (Organizationally Unique Identifier) mappings. When calling functions like get_oui, the OUI_MAPPINGS dictionary is only imported from netutils.oui_mappings at runtime when the function is actually invoked.

    Note that this optimization applies to the Python API; Jinja mappings will still load the full oui_mappings file as required by the template engine.

    @_valid_mac
    def get_oui(mac: str) -> str:
        from netutils.oui_mappings import OUI_MAPPINGS
  6. Understand the ACLRule class for Access Control List management

    develop

    The ACLRule class is designed for managing and evaluating Access Control List (ACL) rules. It is highly extensible, allowing you to customize how business logic is applied to network attributes like src_ip, src_zone, dst_ip, dst_port, dst_zone, and action.

    Key Capabilities:

    • Data Expansion: Converts high-level identifiers (like address-group names) into detailed attributes (like specific IP addresses).
    • Cartesian Product (Permutations): Automatically expands rules into a set of individual permutations (e.g., expanding a rule with multiple source IPs into separate rules for each IP) to simplify evaluation.
    • Validation: Verifies technical feasibility (e.g., checking if IPs exist in IPAM or are routable).
    • Enforcement: Applies security controls (e.g., checking if a PCI zone is attempting to communicate with a non-PCI environment).
    • Matching: Compares rules to identify if a specific access request is already covered by an existing rule.
  7. Extend ACLRule with dynamic process and match methods

    develop

    When inheriting from ACLRule, you can implement custom logic for specific attributes by using the f"{type}_{attr}" naming convention.

    • To add custom processing logic, define a method named process_{attr_name} (e.g., process_src_ip).
    • To add custom matching logic, define a method named match_{attr_name} (e.g., match_dst_port).

    The process and match methods will automatically discover and invoke these methods during execution.

  8. Use Poetry for dependency management

    develop

    Netutils uses Poetry instead of standard virtualenv commands.

    Key Commands

    • poetry shell: Creates and activates a virtual environment managed by Poetry. This ensures all subsequent commands run within the project's context.
    • poetry install: Installs both main project dependencies (tool.poetry.dependencies) and development dependencies (tool.poetry.group.dev.dependencies) into the virtual environment.

    Dependency Structure

    Dependencies are defined in pyproject.toml:

    • tool.poetry.dependencies: Main runtime dependencies.
    • tool.poetry.group.dev.dependencies: Dependencies for linting, testing, and documentation building.
  9. Understand the PYNTC library mapping to normalized types

    develop

    The netutils library provides a mapping between specific PYNTC (Python Network to Code) driver types and their corresponding normalized types. When working with network data, you can use these mappings to translate vendor-specific driver identifiers into a standardized, normalized format used across the netutils ecosystem.

    | PYNTC | | NORMALIZED |
    | ---------- | -- | ------ |
    | arista_eos_eapi | → | arista_eos |
    | cisco_aireos_ssh | → | cisco_wlc |
    | cisco_asa_ssh | → | cisco_asa |
    | cisco_ios_ssh | → | cisco_ios |
    | cisco_iosxr_ssh | → | cisco_xr |
    | cisco_nxos_nxapi | → | cisco_nxos |
    | f5_tmos_icontrol | → | bigip_f5 |
    | juniper_junos_netconf | → | juniper_junos |
  10. Extend ACLRule with dynamic validate and enforce methods

    develop

    When inheriting from ACLRule, you can add custom validation or enforcement logic using the f"{type}_*" naming convention.

    • To add custom validation logic, define a method named validate_{logic_name} (e.g., validate_ip_in_network).
    • To add custom enforcement logic, define a method named enforce_{logic_name}.

    The validate and enforce methods will automatically discover and invoke these methods.

    Ordering: By default, these methods are called in alphabetical order (as returned by Python's dir()). You can control the execution order by defining order_validate or order_enforce respectively.