netutils
netutils is a Python library offering a collection of functions and objects for common network automation tasks, such as BGP ASN conversion, interface name standardization, MAC address formatting, IP address calculations, and configuration parsing. It is actively maintained with frequent releases, typically every few weeks, and is currently at version 1.17.2.
Warnings
- breaking Direct instantiation of `HPEConfigParser` was deprecated in v1.17.0. Users should now subclass a private internal class instead.
- gotcha The `encrypt_cisco_type7` function in versions prior to 1.15.2 incorrectly ignored a `salt=0` parameter, leading to unexpected encryption results for that specific salt value.
- gotcha Parsing empty Palo Alto Networks PanOS configurations in versions prior to 1.17.1 would raise an error.
- breaking The library explicitly requires Python versions >=3.10 and <3.15.
- gotcha Many configuration parsing functionalities (e.g., `ConfigParser`) rely on the `ntc-templates` library. Failure to install `ntc-templates` will result in `ImportError` or `ConfigParser` not being able to find device OS definitions.
Install
-
pip install netutils -
pip install netutils[napalm]
Imports
- ConfigParser
from netutils.config.parser import ConfigParser
- encrypt_cisco_type7
from netutils.password import encrypt_cisco_type7
- normalize_interface_name
from netutils.interface import normalize_interface_name
Quickstart
from netutils.config.parser import ConfigParser
# Example Cisco IOS configuration string
config_text = """
hostname my-router
interface GigabitEthernet0/0
ip address 192.168.1.1 255.255.255.0
no shutdown
interface Loopback0
ip address 10.0.0.1 255.255.255.255
"""
# Instantiate the ConfigParser for Cisco IOS
# 'cisco_ios' is a key from ntc-templates, which is a required dependency
try:
parser = ConfigParser(config_text, 'cisco_ios')
parsed_config = parser.parse()
print("Parsed Configuration:\n", parsed_config)
# Example using an IP utility function
from netutils.ip import is_valid_ipv4_address
print(f"Is '192.168.1.1' a valid IPv4 address? {is_valid_ipv4_address('192.168.1.1')}")
except ImportError:
print("Error: 'ntc-templates' not found. Please install it: pip install ntc-templates")
except Exception as e:
print(f"An error occurred: {e}")