ciscoconfparse.models_cisco

class ciscoconfparse.models_cisco.TrackingInterface(group, interface, decrement, weighting=None)

Implement a TrackingInterface() object for Cisco IOS HSRP, GLBP and VRRP

__annotations__ = {}
__eq__(other)

Return True or False based on whether this Tracking interface is equal to the other instance; both instances must be a TrackingInterface() instance to compare True

__getattr__(attr)
__gt__(val)

Return self>value.

__hash__()

Return a hash value based on the Tracking interface group, interface name and decrement / weighting.

__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
property decrement

Return an integer with the TrackingInterface() decrement or None if there is no decrement.

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
feature = 'tracking_interface'
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 interface
property interface_name

Return the interface name as a string, or return None

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()

property name
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 tracking_interface_group

Return an integer with the TrackingInterface() Group

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
property weighting

Return the weighting (ref GLBP) integer value.

class ciscoconfparse.models_cisco.HSRPInterfaceGroup(group, parent)

A HSRP Interface Group object

__annotations__ = {}
__eq__(other)

Return True if this HSRPInterfaceGroup() is equal to the other instance or False if the other instance is not an HSRPInterfaceGroup().

__getattr__(attr)
__gt__(val)

Return self>value.

__hash__()

Return a hash for this HSRPInterfaceGroup() instance.

__len__()
__lt__(val)

Return self<value.

__repr__()

Return repr(self).

__str__()

Return a string representation of this HSRPInterfaceGroup() instance.

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()

property authentication_md5_keychain

Return the string text of the MD5 key-chain if this HSRP group is configured with an MD5 authentication key-chain; otherwise return an empty string.

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
feature = 'hsrp'
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_glbp_tracking_interfaces()

Get a list of unique GLBP tracked interfaces. This may never be supported by HSRPInterfaceGroup()

get_hsrp_tracking_interfaces()

Return a list of HSRP TrackingInterface() interfaces for this HSRPInterfaceGroup()

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.

get_vrrp_tracking_interfaces()

Get a list of unique VRRP tracked interfaces. This may never be supported by HSRPInterfaceGroup()

property group

Return the integer HSRP Group for this HSRPInterfaceGroup() instance.

property has_authentication_md5

Return True if the router is configured with an MD5 key-chain; otherwise return False.

has_child_with(linespec, all_children=False)
property has_children
property has_hsrp_track
property has_ipv6

Return a boolean for whether this interface is configured with an IPv6 HSRP address

property hash_children

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

property hello_timer

Return the configured integer HSRP hello timer, or the HSRP default of 3 if there is no explicit hello timer.

property hold_timer

Return the configured integer HSRP hold timer, or the HSRP default of 10 if there is no explicit hold timer.

property hsrp_authentication_cleartext
property hsrp_group

Return the integer HSRP group number for this HSRPInterfaceGroup() instance.

indent = 0
property index

Alias index to linenum

insert_after(**kwargs)
insert_before(**kwargs)
property interface_name

Return the string interface name of the interface owning this HSRP group instance.

property interface_tracking

Return a list of HSRP TrackingInterface() objects for this HSRPInterfaceGroup()

property ioscfg

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

property ip

Return the string IPv4 HSRP address for this HSRP group

property ipv4

Return the string IPv4 HSRP address for this HSRP group

property ipv6

Return the string IPv6 HSRP address for this HSRP group

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
property preempt

Return True if the router is configured to preempt for this HSRP Group; otherwise return False.

property preempt_delay

Return the configured integer HSRP preempt delay, or 0 if there is none.

property priority

Return the configured integer HSRP priority, or the HSRP default of 100 if there is no explicit HSRP priority configured.

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 use_bia

Return True if the router is configured with standby use-bia; standby use-bia helps avoid instability when introducing new HSRP routers. Return False if the router is not configured with standby use-bia.

property verbose
property version

Return the configured integer HSRP version, or the HSRP default of 1 if there is no explicit HSRP version configured.

class ciscoconfparse.models_cisco.IOSCfgLine(*args, **kwargs)

An object for a parsed IOS-style configuration line. IOSCfgLine objects contain references to other parent and child IOSCfgLine objects.

Parameters:
linestr

A string containing a text copy of the IOS configuration line. CiscoConfParse will automatically identify the parent and children (if any) when it parses the configuration.

comment_delimiterstr

A string which is considered a comment for the configuration format. Since this is for Cisco IOS-style configurations, it defaults to !.

Returns:
An instance of IOSCfgLine.

Notes

Originally, IOSCfgLine objects were only intended for advanced ciscoconfparse users. As of ciscoconfparse version 0.9.10, all users are strongly encouraged to prefer the methods directly on IOSCfgLine objects. Ultimately, if you write scripts which call methods on IOSCfgLine objects, your scripts will be much more efficient than if you stick strictly to the classic CiscoConfParse methods.

Attributes

text

Get the self.text attribute

linenum

(int) The line number of this configuration statement in the original config; default is -1 when first initialized.

parent

((IOSCfgLine())) The parent of this object; defaults to self.

children

(list) A list of IOSCfgLine() objects which are children of this object.

child_indent

(int) An integer with the indentation of this object’s children

indent

(int) An integer with the indentation of this object’s text oldest_ancestor (bool): A boolean indicating whether this is the oldest ancestor in a family

is_comment

(bool) A boolean indicating whether this is a comment

Accept an IOS line number and initialize family relationship attributes

__annotations__ = {}
__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 intf_in_portchannel

Return a boolean indicating whether this port is configured in a port-channel

Returns:
bool
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_ethernet_intf

Returns a boolean (True or False) to answer whether this IOSCfgLine is an ethernet interface. Any ethernet interface (10M through 10G) is considered an ethernet interface.

Returns:
bool

Examples

This example illustrates use of the method.

>>> from ciscoconfparse import CiscoConfParse
>>> config = [
...     '!',
...     'interface FastEthernet1/0',
...     ' ip address 1.1.1.1 255.255.255.252',
...     '!',
...     'interface ATM2/0',
...     ' no ip address',
...     '!',
...     'interface ATM2/0.100 point-to-point',
...     ' ip address 1.1.1.5 255.255.255.252',
...     ' pvc 0/100',
...     '  vbr-nrt 704 704',
...     '!',
...     ]
>>> parse = CiscoConfParse(config)
>>> obj = parse.find_objects('^interface\sFast')[0]
>>> obj.is_ethernet_intf
True
>>> obj = parse.find_objects('^interface\sATM')[0]
>>> obj.is_ethernet_intf
False
>>>
property is_intf

Returns a boolean (True or False) to answer whether this IOSCfgLine is an interface; subinterfaces also return True.

Returns:
bool

Examples

This example illustrates use of the method.

>>> from ciscoconfparse import CiscoConfParse
>>> config = [
...     '!',
...     'interface Serial1/0',
...     ' ip address 1.1.1.1 255.255.255.252',
...     '!',
...     'interface ATM2/0',
...     ' no ip address',
...     '!',
...     'interface ATM2/0.100 point-to-point',
...     ' ip address 1.1.1.5 255.255.255.252',
...     ' pvc 0/100',
...     '  vbr-nrt 704 704',
...     '!',
...     ]
>>> parse = CiscoConfParse(config)
>>> obj = parse.find_objects('^interface\sSerial')[0]
>>> obj.is_intf
True
>>> obj = parse.find_objects('^interface\sATM')[0]
>>> obj.is_intf
True
>>>
property is_loopback_intf

Returns a boolean (True or False) to answer whether this IOSCfgLine is a loopback interface.

Returns:
bool

Examples

This example illustrates use of the method.

>>> from ciscoconfparse import CiscoConfParse
>>> config = [
...     '!',
...     'interface FastEthernet1/0',
...     ' ip address 1.1.1.1 255.255.255.252',
...     '!',
...     'interface Loopback0',
...     ' ip address 1.1.1.5 255.255.255.255',
...     '!',
...     ]
>>> parse = CiscoConfParse(config)
>>> obj = parse.find_objects(r'^interface\sFast')[0]
>>> obj.is_loopback_intf
False
>>> obj = parse.find_objects(r'^interface\sLoop')[0]
>>> obj.is_loopback_intf
True
>>>
classmethod is_object_for(all_lines, line, re=<module 're' from '/usr/lib/python3.11/re/__init__.py'>)

Return True if this object should be used for a given configuration line; otherwise return False

classmethod is_object_for_aaa_accounting(line)

Return True if this is an object for aaa accounting. Be sure to reject ‘aaa new-model’

classmethod is_object_for_aaa_authentication(line)

Return True if this is an object for aaa authentication. Be sure to reject ‘aaa new-model’

classmethod is_object_for_aaa_authorization(line)

Return True if this is an object for aaa authorization. Be sure to reject ‘aaa new-model’

classmethod is_object_for_hostname(line)
classmethod is_object_for_interface(line)
classmethod is_object_for_ip_route(line)
classmethod is_object_for_ipv6_route(line)
property is_parent
property is_portchannel_intf

Return a boolean indicating whether this port is a port-channel intf

Returns:
bool
property is_subintf

Returns a boolean (True or False) to answer whether this IOSCfgLine is a subinterface.

Returns:
bool

Examples

This example illustrates use of the method.

>>> from ciscoconfparse import CiscoConfParse
>>> config = [
...     '!',
...     'interface Serial1/0',
...     ' ip address 1.1.1.1 255.255.255.252',
...     '!',
...     'interface ATM2/0',
...     ' no ip address',
...     '!',
...     'interface ATM2/0.100 point-to-point',
...     ' ip address 1.1.1.5 255.255.255.252',
...     ' pvc 0/100',
...     '  vbr-nrt 704 704',
...     '!',
...     ]
>>> parse = CiscoConfParse(config)
>>> obj = parse.find_objects(r'^interface\sSerial')[0]
>>> obj.is_subintf
False
>>> obj = parse.find_objects(r'^interface\sATM')[0]
>>> obj.is_subintf
True
>>>
property is_switchport

subclasses will override this method

property is_virtual_intf
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
property portchannel_number

Return an integer for the port-channel which it’s configured in. Return -1 if it’s not configured in a port-channel

Returns:
bool
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
class ciscoconfparse.models_cisco.IOSIntfLine(*args, **kwargs)

Accept an IOS line number and initialize family relationship attributes

Warning

All IOSIntfLine methods are still considered beta-quality, until this notice is removed. The behavior of APIs on this object could change at any time.

__annotations__ = {}
__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).

property abbvs

A python set of valid abbreviations (lowercased) for the interface

property access_vlan

Return an integer with the access vlan number. Return 1, if the switchport has no explicit vlan configured; return 0 if the port isn’t a switchport

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
default_ipv4_addr_object = None
default_ipv6_addr_object = None
delete(**kwargs)
delete_children_matching(**kwargs)
property description

Return the current interface description string.

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.

property has_autonegotiation
has_child_with(linespec, all_children=False)
property has_children
property has_dtp
property has_hsrp_authentication_md5
property has_hsrp_preempt
property has_hsrp_track
property has_hsrp_usebia
property has_ip_accessgroup_in
property has_ip_accessgroup_out
property has_ip_helper_addresses

Return a True if the intf has helper-addresses; False if not

property has_ip_hsrp
property has_ip_pim_dense_mode
property has_ip_pim_sparse_mode
property has_ip_pim_sparsedense_mode
property has_ip_secondary

Return an boolean for whether this interface has IPv4 secondary addresses

property has_ipv4_accessgroup_in
property has_ipv4_accessgroup_out
property has_mac_accessgroup_in
property has_mac_accessgroup_out
property has_manual_carrierdelay

Return a python boolean for whether carrier delay is manually configured on the interface

property has_manual_clock_rate
property has_manual_disable_cdp
property has_manual_duplex
property has_manual_ip_mtu
property has_manual_mpls_mtu
property has_manual_mtu
property has_manual_speed
property has_manual_switch_access
property has_manual_switch_trunk
property has_manual_switch_trunk_encap
property has_mpls
property has_no_icmp_redirects
property has_no_icmp_unreachables
property has_no_ip_proxyarp

Return a boolean for whether no ip proxy-arp is configured on the interface.

Returns:
bool

Examples

This example illustrates use of the method.

>>> from ciscoconfparse.ccp_util import IPv4Obj
>>> from ciscoconfparse import CiscoConfParse
>>> config = [
...     '!',
...     'interface FastEthernet1/0',
...     ' ip address 1.1.1.1 255.255.255.252',
...     ' no ip proxy-arp',
...     '!',
...     ]
>>> parse = CiscoConfParse(config, factory=True)
>>> obj = parse.find_objects('^interface\sFast')[0]
>>> obj.has_no_ip_proxyarp
True
>>>
property has_no_ipv4

Return an ccp_util.IPv4Obj object representing the subnet on this interface; if there is no address, return ccp_util.IPv4Obj()

property has_switch_portsecurity
property has_switch_stormcontrol
property has_vrf
property has_xconnect
property hash_children

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

property hsrp_authentication_cleartext
property hsrp_authentication_md5_keychain
property hsrp_group
property hsrp_hello_timer
property hsrp_hold_timer
property hsrp_interfaces

Return the list of configured HSRPInterfaceGroup() instances

property hsrp_ip_addr
property hsrp_ip_mask
property hsrp_priority
property hsrp_track
in_ipv4_subnet(ipv4network=None, strict=False)

Accept an argument for the IPv4Obj to be considered, and return a boolean for whether this interface is within the requested IPv4Obj.

Parameters:
ipv4networkIPv4Obj

An object to compare against IP addresses configured on this IOSIntfLine object.

Returns:
bool

If there is an ip address, or None if there is no ip address.

Examples

This example illustrates use of the method.

>>> from ciscoconfparse.ccp_util import IPv4Obj
>>> from ciscoconfparse import CiscoConfParse
>>> config = [
...     '!',
...     'interface Serial1/0',
...     ' ip address 1.1.1.1 255.255.255.252',
...     '!',
...     'interface ATM2/0',
...     ' no ip address',
...     '!',
...     'interface ATM2/0.100 point-to-point',
...     ' ip address 1.1.1.5 255.255.255.252',
...     ' pvc 0/100',
...     '  vbr-nrt 704 704',
...     '!',
...     ]
>>> parse = CiscoConfParse(config, factory=True)
>>> obj = parse.find_objects('^interface\sSerial')[0]
>>> obj
<IOSIntfLine # 1 'Serial1/0' primary_ipv4: '1.1.1.1/30'>
>>> obj.in_ipv4_subnet(IPv4Obj('1.1.1.0/24', strict=False))
True
>>> obj.in_ipv4_subnet(IPv4Obj('2.1.1.0/24', strict=False))
False
>>>
in_ipv4_subnets(subnets=None)

Accept a set or list of ccp_util.IPv4Obj objects, and return a boolean for whether this interface is within the requested subnets.

indent = 0
property index

Alias index to linenum

insert_after(**kwargs)
insert_before(**kwargs)
property interface_number

Return a string representing the card, slot, port for this interface. If you call interface_number on GigabitEthernet2/25.100, you’ll get this python string: ‘2/25’. If you call interface_number on GigabitEthernet2/0/25.100 you’ll get this python string ‘2/0/25’. This method strips all subinterface information in the returned value.

Returns:
str

Warning

interface_number should silently fail (returning an empty python string) if the interface doesn’t parse correctly

Examples

This example illustrates use of the method.

>>> from ciscoconfparse import CiscoConfParse
>>> config = [
...     '!',
...     'interface FastEthernet1/0',
...     ' ip address 1.1.1.1 255.255.255.252',
...     '!',
...     'interface ATM2/0',
...     ' no ip address',
...     '!',
...     'interface ATM2/0.100 point-to-point',
...     ' ip address 1.1.1.5 255.255.255.252',
...     ' pvc 0/100',
...     '  vbr-nrt 704 704',
...     '!',
...     ]
>>> parse = CiscoConfParse(config, factory=True)
>>> obj = parse.find_objects('^interface\sFast')[0]
>>> obj.interface_number
'1/0'
>>> obj = parse.find_objects('^interface\sATM')[-1]
>>> obj.interface_number
'2/0'
>>>
property interface_object

Return a CiscoIOSInterface() instance for this interface

Returns:
CiscoIOSInterface

The interface name as a CiscoIOSInterface() instance, or ‘’ if the object is not an interface. The CiscoIOSInterface instance can be transparently cast as a string into a typical Cisco IOS name.

property intf_in_portchannel

Return a boolean indicating whether this port is configured in a port-channel

Returns:
bool
property ioscfg

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

property ip

Return an ccp_util.IPv4Obj object representing the IPv4 address on this interface; if there is no address, return ccp_util.IPv4Obj()

property ip_accessgroup_in
property ip_accessgroup_out
property ip_addr
property ip_helper_addresses

Return a list of dicts with IP helper-addresses. Each helper-address is in a dictionary. The dictionary is in this format:

Examples

>>> from ciscoconfparse import CiscoConfParse
>>> config = [
...     '!',
...     'interface FastEthernet1/1',
...     ' ip address 1.1.1.1 255.255.255.0',
...     ' ip helper-address 172.16.20.12',
...     ' ip helper-address 172.19.185.91',
...     '!',
...     ]
>>> parse = CiscoConfParse(config)
>>> obj = parse.find_objects('^interface\sFastEthernet1/1$')[0]
>>> obj.ip_helper_addresses
[{'addr': '172.16.20.12', 'vrf': '', 'global': False}, {'addr': '172.19.185.91', 'vrf': '', 'global': False}]
>>>
property ip_network_object
property ip_secondary_addresses

Return a set of IPv4 secondary addresses (as strings)

property ip_secondary_networks

Return a set of IPv4 secondary addresses / prefixlen

property ipv4

Return an ccp_util.IPv4Obj object representing the IPv4 address on this interface; if there is no address, return ccp_util.IPv4Obj()

property ipv4_accessgroup_in
property ipv4_accessgroup_out
property ipv4_addr

Return a string with the interface’s IPv4 address, or ‘’ if there is none

property ipv4_addr_object

Return a ccp_util.IPv4Obj object representing the address on this interface; if there is no address, return IPv4Obj()

property ipv4_masklength

Return an integer with the interface’s IPv4 mask length, or 0 if there is no IP address on the interace

property ipv4_netmask

Return a string with the interface’s IPv4 netmask, or ‘’ if there is none

property ipv4_network_object

Return an ccp_util.IPv4Obj object representing the subnet on this interface; if there is no address, return ccp_util.IPv4Obj()

property ipv6_addr

Return a string with the interface’s IPv6 address, or ‘’ if there is none

property ipv6_addr_object

Return a ccp_util.IPv6Obj object representing the address on this interface; if there is no address, return IPv6Obj()

property ipv6_masklength

Return an integer with the interface’s IPv6 mask length, or 0 if there is no IP address on the interace

is_abbreviated_as(val)

Test whether val is a good abbreviation for the interface

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_ethernet_intf

Returns a boolean (True or False) to answer whether this IOSCfgLine is an ethernet interface. Any ethernet interface (10M through 10G) is considered an ethernet interface.

Returns:
bool

Examples

This example illustrates use of the method.

>>> from ciscoconfparse import CiscoConfParse
>>> config = [
...     '!',
...     'interface FastEthernet1/0',
...     ' ip address 1.1.1.1 255.255.255.252',
...     '!',
...     'interface ATM2/0',
...     ' no ip address',
...     '!',
...     'interface ATM2/0.100 point-to-point',
...     ' ip address 1.1.1.5 255.255.255.252',
...     ' pvc 0/100',
...     '  vbr-nrt 704 704',
...     '!',
...     ]
>>> parse = CiscoConfParse(config)
>>> obj = parse.find_objects('^interface\sFast')[0]
>>> obj.is_ethernet_intf
True
>>> obj = parse.find_objects('^interface\sATM')[0]
>>> obj.is_ethernet_intf
False
>>>
property is_intf

Returns a boolean (True or False) to answer whether this IOSCfgLine is an interface; subinterfaces also return True.

Returns:
bool

Examples

This example illustrates use of the method.

>>> from ciscoconfparse import CiscoConfParse
>>> config = [
...     '!',
...     'interface Serial1/0',
...     ' ip address 1.1.1.1 255.255.255.252',
...     '!',
...     'interface ATM2/0',
...     ' no ip address',
...     '!',
...     'interface ATM2/0.100 point-to-point',
...     ' ip address 1.1.1.5 255.255.255.252',
...     ' pvc 0/100',
...     '  vbr-nrt 704 704',
...     '!',
...     ]
>>> parse = CiscoConfParse(config)
>>> obj = parse.find_objects('^interface\sSerial')[0]
>>> obj.is_intf
True
>>> obj = parse.find_objects('^interface\sATM')[0]
>>> obj.is_intf
True
>>>
property is_loopback_intf

Returns a boolean (True or False) to answer whether this IOSCfgLine is a loopback interface.

Returns:
bool

Examples

This example illustrates use of the method.

>>> from ciscoconfparse import CiscoConfParse
>>> config = [
...     '!',
...     'interface FastEthernet1/0',
...     ' ip address 1.1.1.1 255.255.255.252',
...     '!',
...     'interface Loopback0',
...     ' ip address 1.1.1.5 255.255.255.255',
...     '!',
...     ]
>>> parse = CiscoConfParse(config)
>>> obj = parse.find_objects(r'^interface\sFast')[0]
>>> obj.is_loopback_intf
False
>>> obj = parse.find_objects(r'^interface\sLoop')[0]
>>> obj.is_loopback_intf
True
>>>
classmethod is_object_for(all_lines, line, re=<module 're' from '/usr/lib/python3.11/re/__init__.py'>)

Return True if this object should be used for a given configuration line; otherwise return False

classmethod is_object_for_aaa_accounting(line)

Return True if this is an object for aaa accounting. Be sure to reject ‘aaa new-model’

classmethod is_object_for_aaa_authentication(line)

Return True if this is an object for aaa authentication. Be sure to reject ‘aaa new-model’

classmethod is_object_for_aaa_authorization(line)

Return True if this is an object for aaa authorization. Be sure to reject ‘aaa new-model’

classmethod is_object_for_hostname(line)
classmethod is_object_for_interface(line)
classmethod is_object_for_ip_route(line)
classmethod is_object_for_ipv6_route(line)
property is_parent
property is_portchannel_intf

Return a boolean indicating whether this port is a port-channel intf

Returns:
bool
property is_shutdown
property is_subintf

Returns a boolean (True or False) to answer whether this IOSCfgLine is a subinterface.

Returns:
bool

Examples

This example illustrates use of the method.

>>> from ciscoconfparse import CiscoConfParse
>>> config = [
...     '!',
...     'interface Serial1/0',
...     ' ip address 1.1.1.1 255.255.255.252',
...     '!',
...     'interface ATM2/0',
...     ' no ip address',
...     '!',
...     'interface ATM2/0.100 point-to-point',
...     ' ip address 1.1.1.5 255.255.255.252',
...     ' pvc 0/100',
...     '  vbr-nrt 704 704',
...     '!',
...     ]
>>> parse = CiscoConfParse(config)
>>> obj = parse.find_objects(r'^interface\sSerial')[0]
>>> obj.is_subintf
False
>>> obj = parse.find_objects(r'^interface\sATM')[0]
>>> obj.is_subintf
True
>>>
property is_switchport

subclasses will override this method

property is_virtual_intf
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()

property mac_accessgroup_in
property mac_accessgroup_out
property manual_arp_timeout

Return an integer with the current interface ARP timeout, if there isn’t one set, return 0. If there is no IP address, return -1

property manual_bandwidth
property manual_carrierdelay

Return the manual carrier delay (in seconds) of the interface as a python float. If there is no explicit carrier delay, return 0.0

property manual_clock_rate

Return the clock rate of the interface as a python integer. If there is no explicit clock rate, return 0

property manual_delay
property manual_duplex
property manual_encapsulation
property manual_holdqueue_in

Return the current hold-queue in depth, if default return 0

property manual_holdqueue_out

Return the current hold-queue out depth, if default return 0

property manual_ip_mtu
property manual_mpls_mtu
property manual_mtu

Returns a integer value for the manual MTU configured on an IOSIntfLine object. Interfaces without a manual MTU configuration return 0.

Returns:
int

Examples

This example illustrates use of the method.

>>> from ciscoconfparse import CiscoConfParse
>>> config = [
...     '!',
...     'interface FastEthernet1/0',
...     ' ip address 1.1.1.1 255.255.255.252',
...     '!',
...     'interface ATM2/0',
...     ' mtu 4470',
...     ' no ip address',
...     '!',
...     'interface ATM2/0.100 point-to-point',
...     ' ip address 1.1.1.5 255.255.255.252',
...     ' pvc 0/100',
...     '  vbr-nrt 704 704',
...     '!',
...     ]
>>> parse = CiscoConfParse(config, factory=True)
>>> obj = parse.find_objects('^interface\sFast')[0]
>>> obj.manual_mtu
0
>>> obj = parse.find_objects('^interface\sATM')[0]
>>> obj.manual_mtu
4470
>>>
property manual_speed
property manual_switch_trunk_encap
property name

Return the interface name as a string, such as ‘GigabitEthernet0/1’

Returns:
str

The interface name as a string instance, or ‘’ if the object is not an interface.

Examples

This example illustrates use of the method.

>>> from ciscoconfparse import CiscoConfParse
>>> config = [
...     '!',
...     'interface FastEthernet1/0',
...     ' ip address 1.1.1.1 255.255.255.252',
...     '!',
...     'interface ATM2/0',
...     ' no ip address',
...     '!',
...     'interface ATM2/0.100 point-to-point',
...     ' ip address 1.1.1.5 255.255.255.252',
...     ' pvc 0/100',
...     '  vbr-nrt 704 704',
...     '!',
...     ]
>>> parse = CiscoConfParse(config, factory=True)
>>> obj = parse.find_objects('^interface\sFast')[0]
>>> obj.name
'FastEthernet1/0'
>>> obj = parse.find_objects('^interface\sATM')[0]
>>> obj.name
'ATM2/0'
>>> obj = parse.find_objects('^interface\sATM')[1]
>>> obj.name
'ATM2/0.100'
>>>
property native_vlan

Return an integer with the native vlan number. Return 1, if the switchport has no explicit native vlan configured; return 0 if the port isn’t a switchport

property ordinal_list

Return a tuple of numbers representing card, slot, port for this interface. If you call ordinal_list on GigabitEthernet2/25.100, you’ll get this python tuple of integers: (2, 25). If you call ordinal_list on GigabitEthernet2/0/25.100 you’ll get this python list of integers: (2, 0, 25). This method strips all subinterface information in the returned value.

Returns:
tuple

A tuple of port numbers as integers.

Warning

ordinal_list should silently fail (returning an empty python list) if the interface doesn’t parse correctly

Examples

This example illustrates use of the method.

>>> from ciscoconfparse import CiscoConfParse
>>> config = [
...     '!',
...     'interface FastEthernet1/0',
...     ' ip address 1.1.1.1 255.255.255.252',
...     '!',
...     'interface ATM2/0',
...     ' no ip address',
...     '!',
...     'interface ATM2/0.100 point-to-point',
...     ' ip address 1.1.1.5 255.255.255.252',
...     ' pvc 0/100',
...     '  vbr-nrt 704 704',
...     '!',
...     ]
>>> parse = CiscoConfParse(config, factory=True)
>>> obj = parse.find_objects('^interface\sFast')[0]
>>> obj.ordinal_list
(1, 0)
>>> obj = parse.find_objects('^interface\sATM')[0]
>>> obj.ordinal_list
(2, 0)
>>>
parent = None
property port

Return the interface’s port number

Returns:
int

The interface number.

Examples

This example illustrates use of the method.

>>> from ciscoconfparse import CiscoConfParse
>>> config = [
...     '!',
...     'interface FastEthernet1/0',
...     ' ip address 1.1.1.1 255.255.255.252',
...     '!',
...     'interface ATM2/0',
...     ' no ip address',
...     '!',
...     'interface ATM2/0.100 point-to-point',
...     ' ip address 1.1.1.5 255.255.255.252',
...     ' pvc 0/100',
...     '  vbr-nrt 704 704',
...     '!',
...     ]
>>> parse = CiscoConfParse(config, factory=True)
>>> obj = parse.find_objects('^interface\sFast')[0]
>>> obj.port
0
>>> obj = parse.find_objects('^interface\sATM')[0]
>>> obj.port
0
>>>
property port_type

Return Loopback, ATM, GigabitEthernet, Virtual-Template, etc…

Returns:
str

The port type.

Examples

This example illustrates use of the method.

>>> from ciscoconfparse import CiscoConfParse
>>> config = [
...     '!',
...     'interface FastEthernet1/0',
...     ' ip address 1.1.1.1 255.255.255.252',
...     '!',
...     'interface ATM2/0',
...     ' no ip address',
...     '!',
...     'interface ATM2/0.100 point-to-point',
...     ' ip address 1.1.1.5 255.255.255.252',
...     ' pvc 0/100',
...     '  vbr-nrt 704 704',
...     '!',
...     ]
>>> parse = CiscoConfParse(config, factory=True)
>>> obj = parse.find_objects('^interface\sFast')[0]
>>> obj.port_type
'FastEthernet'
>>> obj = parse.find_objects('^interface\sATM')[0]
>>> obj.port_type
'ATM'
>>>
property portchannel_number

Return an integer for the port-channel which it’s configured in. Return -1 if it’s not configured in a port-channel

Returns:
bool
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(atomic=True)
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 subinterface_number

Return a string representing the card, slot, port for this interface or subinterface. If you call subinterface_number on GigabitEthernet2/25.100, you’ll get this python string: ‘2/25.100’. If you call interface_number on GigabitEthernet2/0/25 you’ll get this python string ‘2/0/25’. This method strips all subinterface information in the returned value.

Returns:
str

Warning

subinterface_number should silently fail (returning an empty python string) if the interface doesn’t parse correctly

Examples

This example illustrates use of the method.

>>> from ciscoconfparse import CiscoConfParse
>>> config = [
...     '!',
...     'interface FastEthernet1/0',
...     ' ip address 1.1.1.1 255.255.255.252',
...     '!',
...     'interface ATM2/0',
...     ' no ip address',
...     '!',
...     'interface ATM2/0.100 point-to-point',
...     ' ip address 1.1.1.5 255.255.255.252',
...     ' pvc 0/100',
...     '  vbr-nrt 704 704',
...     '!',
...     ]
>>> parse = CiscoConfParse(config, factory=True)
>>> obj = parse.find_objects('^interface\sFast')[0]
>>> obj.subinterface_number
'1/0'
>>> obj = parse.find_objects('^interface\sATM')[-1]
>>> obj.subinterface_number
'2/0.100'
>>>
property text

Get the self.text attribute

property trunk_vlans_allowed

Return a CiscoRange() with the list of allowed vlan numbers (as int). Return 0 if the port isn’t a switchport in trunk mode

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
property vrf
property xconnect_vc