Parsergen Module¶
- genie.parsergen._parsergen.get_os_type(device, alias=None)¶
Get the OS of the given router
- genie.parsergen._parsergen.extend_markup(marked_up_input)¶
Allows an external module to extend
parsergen
’s dictionary ofregular expressions
,regex
tags and known show commands by specifying marked-up show command text (see Core documentation)This input text is transformed, and then automatically passed to
extend
.- Parameters
marked_up_input (
str
) – Marked-up show command input
- genie.parsergen._parsergen.extend(regex_ext=None, show_cmds=None, regex_tags=None)¶
Allows an external module to extend
parsergen
’sdictionary
ofregular expressions
, known show commands andregex
tags.parsergen
keeps its masterregular expression
dictionary
in the memberparsergen._glb_regex
, its master show commanddictionary
in the memberparsergen._glb_show_commands
and its master regex tagdictionary
in the memberparsergen._glb_regex_tags
.It is possible to register CLI show commands without specifying any regular expressions, since
oper_fill_tabular
also uses this CLI tag/command repository to format its CLI commands.- Parameters
regex_ext (A
dict
ofdictionaries
ofregular expressions
keyed by OS type and regex tag) – A series of tags (keys) and their correspondingregexes
(values). Anyregex
in this dictionary may also be expressed as alist
(this feature allows the same regex tag to match multiple patterns, and is intended on enabling parsers to work correctly across multiple releases across which CLI text may have changed).show_cmds (A
dict
ofdictionaries
ofstr
keyed by OS type and generic CLI command tag) – A series of generic CLI command tags (keys) and their corresponding CLI command equivalents (values). The values may contain string formatting characters, including equal signs, as documented in How To Specify Show Commands.regex_tags (A
dict
oflists
of regex tags keyed by OS type) – A series of tags (keys). This allowsparsergen
to store the original order order the tags were declared. This parameter must be specified in order to make use of theregex_tag_fill_pattern
parameter ofoper_fill
and in order to enable the auto-attrvalpair-reordering feature offered byoper_check
.
- genie.parsergen._parsergen.isstr(s)¶
- class genie.parsergen._parsergen.CmdCache¶
Bases:
object
This class holds the CLI command cache and associated cache update logic.
- cache = {}¶
CACHE OF LAST SHOW COMMAND This dictionary contains the last received show command, indexed by device name. It contains the last received show command output from the device. It’s used to allow parsing when the refresh_cache flag is set to False.
- refresh_cache(obj, show_command)¶
Refresh the cache
- Parameters
obj (
parsergen.oper_check
orparsergen.oper_fill_tabular
object) – Object that is asking for a cache refresh.show_command (
str
ortuple
) – The show command to execute.
- genie.parsergen._parsergen.showCmdCache = <genie.parsergen._parsergen.CmdCache object>¶
This object holds the cache of previously executed show commands.
- class genie.parsergen._parsergen.oper_check(device=None, show_command=None, attrvalpairs=None, refresh_cache=False, negate=False, device_conn_alias=None, device_output=None, device_os=None)¶
Bases:
genie.predcore._predcore.Predicate
Optionally run a show command on a device, parse user-requested fields, and compare these fields against expected values.
Create an object to parse and check CLI command output.
- Parameters
device (
Device()
) – The device to run the show command on.show_command (
str
ortuple
of (str
,list
of positional formatting args,dict
of keyword formatting args)) – The key into the show command dictionary previously passed toextend
or the actual show command to issue. If the corresponding show command dictionary string contains formatting fields, specify these arguments using atuple
. Remember to useNone
as a placeholder if you are combining default and non-default fields(see How To Specify Show Commands).attrvalpairs (
list
oftuples
(str
,str
ortuple
)) – An ordered list of (attribute/value) pairs. Parsergen must have been taught the relationship between attributes and regular expressions, which is done via theextend
API. If a value is expressed as atuple
, then each member of the tuple is considered as a potential match. These pairs are reordered on behalf of the user if all attributes are present inparsergen._glb_regex_tag
(which is automatically populated viaextend_markup
and optionally populated viaextend
).refresh_cache (
bool
) – IfTrue
, refresh the cache by running the indicated show command on the device, and then parse this output. IfFalse
, parse the previously cached show command output for this device. Defaults toTrue
.negate (
bool
) – IfTrue
, negate the result (ie. treat a failure like a success). Defaults toFalse
.device_conn_alias (
str
) – If specified, assume the device was connected to under the indicated alias. Otherwise, the default alias is assumed. For details, please see http://wwwin-pyats.cisco.com/documentation/latest/connections/manager.html#connection-manager
- process_attrvalpairs(attrvalpairs, os, reorder=True)¶
Process a list of tuples of attr/value pairs into a form that parsercare can use.
- result(result)¶
- parse()¶
Perform the parsing and retrieve results.
- Returns
Returns
True
if the parsing was successful. The values passed in asNone
will be filled inparsergen.ext_dictio[device_name]
. ReturnsFalse
if the parsing failed. The user may obtain a failure analysis by callingpython:str
on the parsing object.- Return type
bool
- error(msg, line=9223372036854775807)¶
- test()¶
Override this method to actually perform the predicate test.
- dump()¶
Dump the predicate’s internal state for debugging and logging purposes.
- class genie.parsergen._parsergen.oper_fill(device=None, show_command=None, attrvalpairs=None, refresh_cache=False, regex_tag_fill_pattern='', skip=False, device_conn_alias=None, device_output=None, device_os=None)¶
Bases:
genie.parsergen._parsergen.oper_check
Optionally run a show command on a device and parse user-requested fields.
Create an object to parse CLI command output.
- Parameters
device (
Device()
) – The device to run the show command on.show_command (
str
ortuple
of (str
,list
of positional formatting args,dict
of keyword formatting args)) – The key into the show command dictionary previously passed toextend
or the actual show command to issue. If the corresponding show command dictionary string contains formatting fields, specify these arguments using atuple
. Remember to useNone
as a placeholder if you are combining default and non-default fields(see How To Specify Show Commands).attrvalpairs (
list
oftuples
(str
,str
ortuple
)) – An ordered list of attribute/value pairs. A value ofNone
requests a fill for that attribute. The very first value cannot beNone
. Parsergen must have been taught the relationship between attributes and regular expressions, which is done via theextend
API. If a value is expressed as atuple
, then each member of the tuple is considered as a potential match.refresh_cache (
bool
) – IfTrue
, refresh the cache by running the indicated show command on the device, and then parse this output. IfFalse
, parse the previously cached show command output for this device. Defaults toTrue
.regex_tag_fill_pattern (
regular expression
) – Regular expression to use to select a group of regex tags to fill. The tags will be added to the attrvalpairs list as (tag, None) in the same order as found inparsergen._glb_regex_tag
(which is automatically populated viaextend_markup
and optionally populated viaextend
).skip (
bool
) – Whether or not to ignore unmatched attributes (regex tags). IfTrue
, then ignore any attributes (regex tags) whose associatedregexes
do not match the text to parse. One reason to specify this option could be when the text to parse could contain optional content lines. Defaults toFalse
.device_conn_alias (
str
) – If specified, assume the device was connected to under the indicated alias. Otherwise, the default alias is assumed. For details, please see http://wwwin-pyats.cisco.com/documentation/latest/connections/manager.html#connection-manager
Warning
If you specify a non-
None
regex_tag_fill_pattern
then ensure none of your (attr, value) pairs have a value ofNone
, otherwise parser failure can result.Example use to obtain interface state values:
parsergen.oper_fill(R1, 'show interfaces', [ ('im.intf-name', 'GigabitEthernet0/0/0/1'), ('im.intf-status', None), ('im.intf-line-status', None), ('im.intf-mtu', None), ('im.intf-bw', None), ('im.intf-arptype', None) ], refresh_cache=True)
The values passed in as
None
will be filled inparsergen.ext_dictio[device_name]
.Note that, in case of output with multiple entries, eg datalist,
oper_fill
needs a hook in the code in order to target the desired values. For instance, in the above example, it’s using the name of the interfaces to hook into that section of the output.This class may not be the best choice to parse tabular data.
Note
Please consider using the
oper_fill_tabular
class for parsing tabular results as it is generally far easier. For output that contains both tabular and non-tabular data, it is also possible to use both mechanisms to parse different parts of the output.Example of accessing the filled in values:
>>> parsergen.ext_dictio[R1.name]['im.intf-line-status'] 'Up' >>> parsergen.ext_dictio[R1.name]['im.intf-mtu'] '1500'
- class genie.parsergen._parsergen.oper_fill_tabular(device=None, show_command=None, refresh_cache=True, header_fields=None, table_terminal_pattern=None, index=0, skip_header=None, table_title_pattern=None, right_justified=False, label_fields=None, is_run_command=False, skip_line=None, device_conn_alias=None, device_output=None, device_os=None, delimiter='')¶
Bases:
genie.parsergen.core.column_table_result_core_t
Class that parses tabular “show” command output.
Example
:show_command_output_example = ''' RP/0/0/CPU0:one#show rib clients Thu Sep 17 11:41:18.164 EDT Process Location Client ID Redist Proto bcdl_agent node0_0_CPU0 0 insync no ''' from genie.parsergen import oper_fill_tabular res = oper_fill_tabular( device=device1, show_command="show rib clients", header_fields = ["Process", "Location", "Client ID", "Redist", "Proto"]) print res.entries['bcdl_agent']['Location']
will print
node0_0_CPU0
set second_show_command_output_example ''' RP/0/0/CPU0:one#show isis topology level 1 Sat Sep 19 14:46:33.902 EDT IS-IS ring paths to IPv4 Unicast (Level-1) routers System Id Metric Next-Hop Interface SNPA one -- two 10 two Gi0/0/0/0 *PtoP* two 10 two Gi0/0/0/1 02db.ebba.ecc4 three 20 two Gi0/0/0/0 *PtoP* three 20 two Gi0/0/0/1 02db.ebba.ecc4 ''' res = oper_fill_tabular ( device=device1, show_command="show isis topology level 1", header_fields= ["System Id", "Metric", "Next-Hop", "Interface", "SNPA"], index=[0,3]) print res.entries['three']['Gi0/0/0/1']['SNPA']
will print
02db.ebba.ecc4
print res.entries['one']['']['Metric']
will print
--
Note
The tabular parsing behavior may be customized via subclassing. Please refer to Use of Subclassing with the Tabular Parser and
tabular_parser_subclass.py
for examples.Creates a results object that parses tabular “show” command output.
- Returns
Parsed results are obtained by using the column positions of the header field. The entry starts with the column of the first header character and ends just prior to the next header column.
The object returned has a dictionary named
entries
which is keyed by the entry value corresponding to the header atindex
. Each key inentries
is indexed by the correspondingheader_fields
name (orlabel_fields
name if specified).- Parameters
device (
Device()
) – The device to run the show command on.device_conn_alias (
str
) – If specified, assume the device was connected to under the indicated alias. Otherwise, the default alias is assumed. For details, please see https://developer.cisco.com/docs/pyats/ ConnectionMeta - > Connection Managershow_command (
str
ortuple
of (str
,list
of positional formatting args,dict
of keyword formatting args)) – The key into the show command dictionary previously passed toextend
or the actual show command to issue. If the corresponding show command dictionary string contains formatting fields, specify these arguments using atuple
.refresh_cache (
bool
) – IfTrue
, refresh the cache by running the indicated show command on the device again, and then parse this output. IfFalse
, parse the previously cached show command output. Defaults toTrue
.header_fields (
list
ofstr
) – List of column headings to parse. If not specified, it must be provided via a subclass viaget_init_info
.table_terminal_pattern (
str
) – If specified, this is the pattern to terminate the search for results.index (
int
orlist
ofint
) – Defaults to the first field (0). If more fields are required to guarantee uniqueness pass a list of indices inindex
and that many dictionaries will be returned keyed by the given field values at those indices.skip_header (
str
) – If specified, then the input between the header fields and this pattern are skipped before entries are looked for.table_title_pattern (
str
) – If specified, this pattern allows multiple tables to be identified. Results will be a dictionary of matches keyed ongroup('Title')
of the regex. (e.g., a pattern liker'(?P<Title>Port (\w+))'
would match"Port GigabitEthernet_0_0_0_0 ..."
and the key would be'GigabitEthernet_0_0_0_0'
).right_justified (
bool
) – IfTrue
, then the header labels are considered right justified. Defaults toFalse
.label_fields (
list
ofstr
) – If specified, then theentries
object will contain these keys instead of those specified inheader_fields
. There must be a corresponding entry inlabel_fields
for each entry inheader_fields
.skip_line (
str
) – If specified, then any line matching this pattern will be skipped.delimiter (
str
) – Table delimiter pattern that allows for columns’ borders identification.
- entries¶
A dictionary containing the results of the show command.