ciscoconfparse.ccp_abc

class ciscoconfparse.ccp_abc.BaseCfgLine(all_lines=None, line='__undefined__', comment_delimiter='!', **kwargs)

Accept an IOS line number and initialize family relationship attributes

__eq__(val)

Return self==value.

__getattr__(attr)
__gt__(val)

Return self>value.

__hash__()

Return hash(self).

__len__()
__lt__(val)

Return self<value.

__repr__()

Return repr(self).

__str__()

Return str(self).

add_child(**kwargs)
add_parent(**kwargs)
add_uncfgtext(**kwargs)
property all_children
property all_parents
all_text = []
append_to_family(**kwargs)
property as_diff_dict

An internal dict which is used in HDiff()

blank_line_keep = False
build_reset_string()
calculate_line_id()

Calculate and return an integer line_id for BaseCfgLine()

The hash() of self.text is used to build a numerical identity for a given BaseCfgLine().

Do NOT cache this value. It must be recalculated when self._text changes.

child_indent = 0
property children
property classname
comment_delimiter = None
confobj = None
delete(**kwargs)
delete_children_matching(**kwargs)
property diff_id_list

Return a list of integers as a context-sensitive diff identifier.

The returned value includes line_id of all parents. The oldest ancestor / parent line_id is last in the returned list of line_id hash values.

object id integers are NOT the same between script runs.

diff_linenum = -1
diff_rendered = None
property diff_side

A diff_side getter attribute (typically used in HDiff())

property diff_word

A diff_word getter attribute (typically used in HDiff())

property dna
property family_endpoint
find_parent_for(**kwargs)
property geneology

Iterate through to the oldest ancestor of this object, and return a list of all ancestors’ objects in the direct line as well as this obj. Cousins or aunts / uncles are not returned. Note: children of this object are not returned.

property geneology_text

Iterate through to the oldest ancestor of this object, and return a list of all ancestors’ .text field for all ancestors in the direct line as well as this obj. Cousins or aunts / uncles are not returned. Note: children of this object are not returned.

get_typed_dict(regex=None, type_dict=None, default=None, debug=False)

Return a typed dict if regex is an re.Match() instance and type_dict is a dict of types. If a key in type_dict does not match, default is returned for that key.

Examples

These examples demonstrate how get_typed_dict() works.

has_child_with(linespec, all_children=False)
property has_children
property hash_children

Return a unique hash of all children (if the number of children > 0)

indent = 0
property index

Alias index to linenum

insert_after(**kwargs)
insert_before(**kwargs)
property ioscfg

Return a list with this the text of this object, and with all children in the direct line.

property is_child
is_comment = None
property is_config_line

Return a boolean for whether this is a config statement; returns False if this object is a blank line, or a comment.

property is_intf

subclasses will override this method

classmethod is_object_for(line='')
property is_parent
property is_subintf

subclasses will override this method

property is_switchport

subclasses will override this method

property line_id
property lineage

Iterate through to the oldest ancestor of this object, and return a list of all ancestors / children in the direct line. Cousins or aunts / uncles are not returned. Note: all children of this object are returned.

linenum = -1
lstrip()

Implement lstrip() on the BaseCfgLine().text; manually call CiscoConfParse().commit() after the lstrip()

parent = None
re_match(regex, group=1, default='')

Use regex to search the IOSCfgLine text and return the regular expression group, at the integer index. Parameters ———- regex : str

A string or python regular expression, which should be matched. This regular expression should contain parenthesis, which bound a match group.

groupint

An integer which specifies the desired regex group to be returned. group defaults to 1.

defaultstr

The default value to be returned, if there is no match. By default an empty string is returned if there is no match.

Returns

str

The text matched by the regular expression group; if there is no match, default is returned.

Examples

This example illustrates how you can use re_match() to store the mask of the interface which owns “1.1.1.5” in a variable called netmask. .. code-block:: python

emphasize-lines:

14

>>> from ciscoconfparse import CiscoConfParse
>>> config = [
...     '!',
...     'interface Serial1/0',
...     ' ip address 1.1.1.1 255.255.255.252',
...     '!',
...     'interface Serial1/1',
...     ' ip address 1.1.1.5 255.255.255.252',
...     '!',
...     ]
>>> parse = CiscoConfParse(config)
>>>
>>> for obj in parse.find_objects(r'ip\saddress'):
...     netmask = obj.re_match(r'1\.1\.1\.5\s(\S+)')
>>>
>>> print("The netmask is", netmask)
The netmask is 255.255.255.252
>>>
re_match_iter_typed(regex, group=1, result_type=<class 'str'>, default='', untyped_default=False, groupdict=None, recurse=True, debug=False)

Use regex to search the children of IOSCfgLine text and return the contents of the regular expression group, at the integer group index, cast as result_type; if there is no match, default is returned. Parameters ———- regex : str

A string or python compiled regular expression, which should be matched. This regular expression should contain parenthesis, which bound a match group.

groupint

An integer which specifies the desired regex group to be returned. group defaults to 1; this is only used if groupdict is None.

result_typetype

A type (typically one of: str, int, float, or IPv4Obj). All returned values are cast as result_type, which defaults to str. This is only used if groupdict is None.

defaultany

The default value to be returned, if there is no match.

recursebool

Set True if you want to search all children (children, grand children, great grand children, etc…)

untyped_defaultbool

Set True if you don’t want the default value to be typed; this is only used if groupdict is None.

groupdictdict

Set to a dict of types if you want to match on regex group names; groupdict overrides the group, result_type and untyped_default arguments.

debugbool

Set True if you want to debug re_match_iter_typed() activity

Returns:
result_type

The text matched by the regular expression group; if there is no match, default is returned. All values are cast as result_type, unless untyped_default is True.

Notes
This loops through the children (in order) and returns when the regex hits its first match.

Examples

This example illustrates how you can use re_match_iter_typed() to build an IPv4Obj() address object for each interface.

>>> import re
>>> from ciscoconfparse import CiscoConfParse
>>> from ciscoconfparse.ccp_util import IPv4Obj
>>> config = [
...     '!',
...     'interface Serial1/0',
...     ' ip address 1.1.1.1 255.255.255.252',
...     '!',
...     'interface Serial2/0',
...     ' ip address 1.1.1.5 255.255.255.252',
...     '!',
...     ]
>>> parse = CiscoConfParse(config)
>>> INTF_RE = re.compile(r'interface\s\S+')
>>> ADDR_RE = re.compile(r'ip\saddress\s(\S+\s+\S+)')
>>> for obj in parse.find_objects(INTF_RE):
...     print("{} {}".format(obj.text, obj.re_match_iter_typed(ADDR_RE, result_type=IPv4Obj)))
interface Serial1/0 <IPv4Obj 1.1.1.1/30>
interface Serial2/0 <IPv4Obj 1.1.1.5/30>
>>>
re_match_typed(regex, group=1, untyped_default=False, result_type=<class 'str'>, default='')

Use regex to search the IOSCfgLine text and return the contents of the regular expression group, at the integer group index, cast as result_type; if there is no match, default is returned. Parameters ———- regex : str

A string or python regular expression, which should be matched. This regular expression should contain parenthesis, which bound a match group.

groupint

An integer which specifies the desired regex group to be returned. group defaults to 1.

result_typetype

A type (typically one of: str, int, float, or IPv4Obj). All returned values are cast as result_type, which defaults to str.

defaultany

The default value to be returned, if there is no match.

untyped_defaultbool

Set True if you don’t want the default value to be typed

Returns

result_type

The text matched by the regular expression group; if there is no match, default is returned. All values are cast as result_type, unless untyped_default is True.

Examples

This example illustrates how you can use re_match_typed() to build an association between an interface name, and its numerical slot value. The name will be cast as str(), and the slot will be cast as int(). .. code-block:: python

emphasize-lines:

15,16,17,18,19

>>> from ciscoconfparse import CiscoConfParse
>>> config = [
...     '!',
...     'interface Serial1/0',
...     ' ip address 1.1.1.1 255.255.255.252',
...     '!',
...     'interface Serial2/0',
...     ' ip address 1.1.1.5 255.255.255.252',
...     '!',
...     ]
>>> parse = CiscoConfParse(config)
>>>
>>> slots = dict()
>>> for obj in parse.find_objects(r'^interface'):
...     name = obj.re_match_typed(regex=r'^interface\s(\S+)',
...         default='UNKNOWN')
...     slot = obj.re_match_typed(regex=r'Serial(\d+)',
...         result_type=int,
...         default=-1)
...     print("Interface {0} is in slot {1}".format(name, slot))
...
Interface Serial1/0 is in slot 1
Interface Serial2/0 is in slot 2
>>>

Search IOSCfgLine with regex

Parameters:
regexstr

A string or python regular expression, which should be matched.

defaultstr

A value which is returned if re_search() doesn’t find a match while looking for regex.

Returns
——-
str

The IOSCfgLine text which matched. If there is no match, default is returned.

re_search_children(regex, recurse=False)

Use regex to search the text contained in the children of this IOSCfgLine. Parameters ———- regex : str

A string or python regular expression, which should be matched.

recursebool

Set True if you want to search all children (children, grand children, great grand children, etc…)

Returns

list

A list of matching IOSCfgLine objects which matched. If there is no match, an empty list() is returned.

re_sub(regex, replacergx, ignore_rgx=None)

Replace all strings matching linespec with replacestr in the IOSCfgLine object; however, if the IOSCfgLine text matches ignore_rgx, then the text is not replaced. Parameters ———- regex : str

A string or python regular expression, which should be matched.

replacergxstr

A string or python regular expression, which should replace the text matched by regex.

ignore_rgxstr

A string or python regular expression; the replacement is skipped if IOSCfgLine text matches ignore_rgx. ignore_rgx defaults to None, which means no lines matching regex are skipped.

Returns:
str

The new text after replacement

Examples

This example illustrates how you can use re_sub() to replace Serial1 with Serial0 in a configuration… .. code-block:: python

emphasize-lines:

15

>>> from ciscoconfparse import CiscoConfParse
>>> config = [
...     '!',
...     'interface Serial1/0',
...     ' ip address 1.1.1.1 255.255.255.252',
...     '!',
...     'interface Serial1/1',
...     ' ip address 1.1.1.5 255.255.255.252',
...     '!',
...     ]
>>> parse = CiscoConfParse(config)
>>>
>>> for obj in parse.find_objects('Serial'):
...     print("OLD {}".format(obj.text))
...     obj.re_sub(r'Serial1', r'Serial0')
...     print("  NEW {}".format(obj.text))
OLD interface Serial1/0
  NEW interface Serial0/0
OLD interface Serial1/1
  NEW interface Serial0/1
>>>
replace(**kwargs)
reset()
rstrip()

Implement rstrip() on the BaseCfgLine().text; manually call CiscoConfParse().commit() after the rstrip()

safe_escape_curly_braces(text)

Escape curly braces in strings since they could be misunderstood as f-string or string.format() delimiters…

If BaseCfgLine receives line with curly-braces, this method can escape the curly braces so they are not mis-interpreted as python string formatting delimiters.

set_comment_bool()

Set the .is_comment attribute for this object.

property siblings
strip()

Implement strip() on the BaseCfgLine().text; manually call CiscoConfParse().commit() after the strip()

property text

Get the self.text attribute

property uncfgtext

Return a ‘best-effort’ Cisco IOS-style config to remove this configuration object.

This uncfgtext string should not be considered correct in all Cisco IOS command unconfigure cases.

property verbose