utils.networkparse.networkparse.parse module
Parse a network configuration file
To begin using networkparse, typically an subclass of ConfigBase will be
instantiated with the text of the configuration file. Currently, networkparse has
support for:
Cisco IOS:
ConfigIOSCisco NX-OS:
ConfigNXOSFortinet:
ConfigFortigateHP:
ConfigHPCommwareJunos:
ConfigJunos
The automatic parser, automatic() may be a more efficient way to get started if
a single script may work with multiple configuration types.
- class utils.networkparse.networkparse.parse.ConfigASA(config_content: str)
Bases:
ConfigBaseParses Cisco ASA-style configuration into common config format
Supported command output:
show running-configshow running-config allshow startup-config
nameis set to “asa” for this parser.See
ConfigBasefor more information.- property version: VersionNumber | None
Returns the major version number of the configuration
This is intended to help with differences between major versions. It identifies the version by looking for a line like “ASA Version 9.0(3)”.
- Returns:
Version number (i.e. 12.1 or 15.4) or None if the version cannot be found
- Return type:
Optional[VersionNumber]
- class utils.networkparse.networkparse.parse.ConfigBase(name='base', original_lines: List[str] = None, comment_marker: str = '!', full_match_default: bool = True, indent_size_default: int = 2)
Bases:
ConfigLineListCommon configuration base operations
ConfigBaseis really just a specializedConfigLineListthat can hold some settings and act like aConfigLinein terms of having a parent (None) and children.Refer to
ConfigLineListfor filtering and searching options after you’ve parsed a configuration file.- can_have_children(line: ConfigLine | str)
Checks if the given line is allowed to have child lines
Defaults to true, for compatibility with existing code
- property children: ConfigLineList
Allow for use of “.children” for consistency with
ConfigLineReturns
self, which is already aConfigLineList. It is likely cleaner to not use this. I.E.:config = ConfigIOS(running_config_contents) # Prefer this config.filter("interface .+") # Only use this if it looks clearer in context config.children.filter("interface .+")
- comment_marker = '!'
Defaults to ! as the comment marker, following Cisco convention. If more complex comment checking is needed override is_comment()
- full_match = True
Default setting for
full_matchinfilter. Defaults to True to prevent a search from also matching the “no” version of the line.
- get_line(line_number) ConfigLine
Get a line by line-number
Note that if the given line number falls in the middle of a multi-line string, it may not be found, depending on how the individual parser represents it.
- Parameters:
line_number – Returns the line at the given line number.
- Raises:
IndexError – The given
line_numberdoes not exist in the config- Returns:
Line at the given line number
- Return type:
ConfigLine
- property indent
The base config is never indented, at least for our purposes
- indent_size = 2
How far tree_display() should indent children. Has no effect on parsing
- name = 'base'
Name of configuration type, usable by scripts that support multiple configuration types.
- original_lines = None
Original configuration lines, before any parsing occured. The
line_numberfrom aConfigLinewill match up with this list
- parent = None
Exists to make walking up a parent tree easier–just look for parent=None to stop
Contrived example:
current_line = config.filter("no shutdown", depth=None) while current_line.parent is not None: print(current_line) current_line = current_line.parent
- property version: VersionNumber | None
Returns the version number of the configuration
This is intended to help with major version differences, i.e., IOS v12 vs. v15 differences. Generally it identifies the version by looking for a line like “version 12.2”, although the details vary based on the parser in use. More details can be found on each of the base classes.
- Returns:
Floating-point version number (i.e. 12.1 or 15.4) or None if the version cannot be found
- Return type:
Optional[VersionNumber]
- class utils.networkparse.networkparse.parse.ConfigFortigate(config_content: str)
Bases:
ConfigBaseParses Fortinet-style configuration into common config format
Supported command output:
show full-configuration
nameis set to “fortios” for this parser.See
ConfigBasefor more information.- property version: VersionNumber | None
Returns the version number of the configuration
This is intended to help with v12 vs. v15 types of differences. It identifies the version by looking for a line like “#config-version=FGVM64-6.2.0-FW-build0866-190328:opmode=0:vdom=0:user=admin”.
- Returns:
Float version number (i.e. 6.2) or None if the version cannot be found
- Return type:
Optional[VersionNumber]
- class utils.networkparse.networkparse.parse.ConfigHPCommware(config_content: str)
Bases:
ConfigBaseParses HP Commware-style configuration into common config format
nameis set to “hp” for this parser.See
ConfigBasefor more information.- property version: VersionNumber | None
Returns the major version number of the configuration
This is intended to help with v12 vs. v15 differences. It identifies the version by looking for a line like “version 5.20, Release 1513P81”.
- Returns:
Version number (i.e. 5.20) or None if the version cannot be found
- class utils.networkparse.networkparse.parse.ConfigIOS(config_content: str)
Bases:
ConfigBaseParses Cisco IOS-style configuration into common config format
Supported command output:
show running-configshow running-config allshow startup-config
nameis set to “ios” for this parser.See
ConfigBasefor more information.- can_have_children(line: ConfigLine | str)
Checks if the given line is allowed to have child lines
- property version: VersionNumber | None
Returns the version number of the configuration
This is intended to help with v12 vs. v15 differences. It identifies the version by looking for a line like “version 12.2”.
- Returns:
Version number (i.e. 12 or 15) or None if the version cannot be found
- Return type:
Optional[VersionNumber]
- class utils.networkparse.networkparse.parse.ConfigJunos(config_content: str)
Bases:
ConfigBaseParses a Juniper OS (Junos)-style configuration into common config format
Supported command outputs are:
show configurationsave
nameis set to “junos” for this parser.See
ConfigBasefor more information.
- class utils.networkparse.networkparse.parse.ConfigNXOS(config_content: str)
Bases:
ConfigIOSParses Cisco NX-OS-style configuration into common config format
Currently this parser completely defers to
ConfigIOS.See
ConfigIOSfor more information.
- class utils.networkparse.networkparse.parse.OneIndexedList(*args)
Bases:
list1-index based list
Used internally for original_lines
- exception utils.networkparse.networkparse.parse.UnknownConfigError
Bases:
ExceptionThe correct configuration type could not be determined
- class utils.networkparse.networkparse.parse.VersionNumber(major: int, minor: int = 0, revision: int = 0)
Bases:
objectFirmware version number
- classmethod from_string(string) VersionNumber
Given a string containing a release number, returns a VersionNumber
Supported formats:
1.2.3
1.2
1.2(3)
Version string may appear anywhere in the given string.
- Parameters:
string – String to attempt to parse
- Returns:
VersionNumber with major, minor, and (if available) revision number set
- Return type:
VersionNumber
- major: int
- minor: int = 0
- revision: int = 0
- utils.networkparse.networkparse.parse.automatic(config_content: str, *, include: List[str] = None, fallback: ConfigBase | None = None) ConfigBase
Based on the given configuration, guess the best parser.
Currently, this function can guess the following (ordered base on reliability)
ASA: reliable
FortiOS: probably reliable
JunOS: probably reliable
IOS: may catch non-IOS items, but is last thing checked
NX: untested, might work
- Parameters:
include – Only check the given types. Types are “asa”, “ios”, “nxos”, “fortios”, “hp”, and “junos”. By default all options will be checked. The names used match with the
nameof eachConfigBasesubclass.fallback – If no match is found, use the specified class as a parser. This should be an object, not an instance (i.e.,
parse.ConfigIOSvs.parse.ConfigIOS()).
- Raises:
UnknownConfigError – The correct configuration type could not be determined
- Returns:
Parsed configuration
- Return type:
ConfigBase
- utils.networkparse.networkparse.parse.line_parse_checker(line)
Checks if the given line parses fully–ie, all quotes are closed