ParserModule¶
ParserModule(object)
This class represents the base object, from which other (vendor specific classes) inherit. This class provides basic set of functions which handle parsing of raw text outputs based on Python’s re module and regex strings provided by Patterns class.
The core idea behind ParserModule is that the complexity of various command outputs differ. Some outputs, such as ‘show mac address-table’ are simple enough to be handled by single regex pattern. On the other hand, some outputs, such as ‘show interfaces’ are way more complex and would require a very long and complex regex patterns to match. Also, every little change in the output would cause this pattern not to match. To avoid the usage of overly complex regex patterns, the parsing can be divided into two steps, or levels. In the first level, the text output is ‘pre-processed’, for example the output of show interfaces is splitted to list of strings, where each string represents a single interface. In the second level, each of these strings can be processed using multiple patterns, resulting in final dictionary representing the interface. For some outputs, these two levels can “overlap”, meaning that the first level can already return a dictionary, but some of the keys need to be ‘post-processed’ by the second level. A great example of such command is ‘show vlan brief’, where the first level returns a list of dictionaries with keys [“vlan_id”, “name”, “ports”]. The value of “`”ports”` (which is a string of all ports assigned to this VLAN) can be further processed by second level, which converts this string to a list of individual ports.
Base Functions
match_single_pattern(self, text, pattern)- This function tries to match a single regexpatternon giventextstring. This function operates in two ‘modes’: with or without named groups in regex pattern. Ifpatterncontains at least one named group (for exampler"^(?P<name_of_the_group>.*)"), the function will return list of dictionaries, where each dictionary has “name_of_the_group” as key and whatever the.*matched as value. If pattern does not contain any named group, list of strings is returned, each string being one match of the pattern (basicallyre.findall(pattern=pattern, string=text)).update_entry(self, orig_entry, new_entry)- This function simply updates dictorig_entrywith dictnew_entry. The changes are made only for keys, that are not in theorig_entryor those, which value isNone. The updated dictionary is returned.match_multi_pattern(self, text, patterns)- This function uses multiple regexpatternsto match against giventext. At the beginning, a new dictionary is created with named groups of ALL the patterns as keys and valuesNone. Each time one of the patterns matches, the resulting dictionary is updated._level_zero(self, text, patterns)- This function tries to match a pattern from patterns until a match is found. Then based on theself.match_single_pattern(**kwargs)returns either list of strings, or list of dictionaries. This function is used either for matching ‘simple’ outputs or for pre-processing (and post-processing) of more complex outputs._level_one(self, text, command)- Given the command variable (which represents the command used to get output), it determines the level of the command and fetches corresponding patterns fromself.patterns["level0"][command](an instance ofPatternsclass). If the level is 0, it simply returns the output ofself._level_zero(text, patterns). If the level is 1, it continues to process individual entries returned by_level_zerobased on patterns fromself.patterns["level1"][command]. Return a list of dictionaries.autoparse(self, text, command)- The main entry point for parsing, given just thetextoutput andcommandit determines the proper way to parse the output and returns result.
-
class
ParserModule(device_type, DEBUG=False)¶ Bases:
objectThis class provides necessary functions for parsing plaintext output of network devices. Uses patterns from
PatternsLibfor specified device type. The outputs are usually lists of dictionaries, which contain keys based on name groups of used regex patterns.Parameters: - device_type (str) – String representation of device type, such as cisco_ios
- DEBUG (bool) – Enables/disables debugging output
-
_level_one(text, command)¶ This function handles parsing of more complex plaintext outputs. First, the output of
_level_zero()function is retrieved and then further parsed.Parameters: - text (str) – Plaintex output of given
command, which will be parsed - command (str) – Command string used to generate the output
Returns: List of dictionaries
- text (str) – Plaintex output of given
-
_level_zero(text, patterns)¶ This function handles parsing of less complex plaintext outputs, which can be parsed in one step.
Parameters: - text (str) – Plaintex output which will be parsed
- patterns – List of compiled regex patterns, which are used to parse the
text
Returns: List dics (if
patternscontain named groups) or list of strings (if they don’t)
-
autoparse(text, command)¶ The main entry point for parsing, given just the
textoutput andcommandit determines the proper way to parse the output and returns result.Parameters: - text (str) – Text output to be processed
- command (str) – Command used to generate
textoutput. Based on this parameter, correct regex patterns are selected.
Returns: List of found entities, usually list of dictionaries
-
command_mapping(command)¶ This function determines the max_level of command - based on level of complexity of the output, the parsing is processed in 1 or 2 steps. Used for autoparse
Parameters: command (str) – Command used to generate the output, eg. show vlan brief Returns: (str) Highest level of specified command (top PatternsLib Key)
-
match_multi_pattern(text, patterns)¶ This functions tries to match multiple regex
patternsagainst giventext.Parameters: - text (str) – Text output to be processed
- patterns (lst) – List of
recompiled patterns for parsing.
Returns: Dictionary with names of all groups from all
patternsas keys, with matching strings as values.
-
match_single_pattern(text, pattern)¶ This function tries to match given regex
patternagainst giventext. Ifpatterncontains named groups, list of dictionaries with these groups as keys is returned. Ifpatterndoes not contain any named groups, list of matching strings is returned.Parameters: - text (str) –
- pattern –
recompiled regex pattern
Returns: List of matches, either dictionaries or strings
-
update_entry(orig_entry, new_entry)¶ This function simply updates dictionary
orig_entrywith keys and values fromnew_entry. OnlyNonevalues inorig_entryare updated. Keys with valueNonefromnew_entryare also used in order to ensure coherent output.Parameters: - orig_entry (dict) – Original dictionary to be updated based on entries in
new_entry - new_entry (dict) – New dictionary containing new data which should be added to
orig_entry
Returns: Updated dictionary
- orig_entry (dict) – Original dictionary to be updated based on entries in
-
class
CiscoIOSParser(DEBUG=False)¶ Bases:
nuaal.Parsers.Parser.ParserModuleChild class of ParserModule designed for cisco_ios device type.
-
trunk_parser(text)¶ Function specifically designed for parsing output of show interfaces trunk command.
Parameters: text (str) – Plaintext output of show interfaces trunk command. Returns: List of dictionaries representing trunk interfaces.
-
vlanGroup_check(vlanGroup)¶
-