ciscoconfparse.models_cisco.IOSCfgLine Object

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
textstr

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

(str) A string containing the parsed IOS configuration statement

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

__eq__(val)

Return self==value.

__gt__(val)

Return self>value.

__hash__()

Return hash(self).

__lt__(val)

Return self<value.

__metaclass__

alias of abc.ABCMeta

__repr__()

Return repr(self).

__str__()

Return str(self).

add_child(childobj)

Add references to childobj, on this object

add_parent(parentobj)

Add a reference to parentobj, on this object

add_uncfgtext(unconftext)

unconftext is defined during special method calls. Do not assume it is automatically populated.

append_to_family(insertstr, indent=- 1, auto_indent_width=1, auto_indent=False)

Append an IOSCfgLine object with insertstr as a child at the bottom of the current configuration family.

Parameters
insertstrstr

A string which contains the text configuration to be apppended.

indentint

The amount of indentation to use for the child line; by default, the number of left spaces provided with insertstr are respected. However, you can manually set the indent level when indent>0. This option will be ignored, if auto_indent is True.

auto_indent_widthint

Amount of whitespace to automatically indent

auto_indentbool

Automatically indent the child to auto_indent_width

Returns
str

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

Examples

This example illustrates how you can use append_to_family() to add a carrier-delay to each interface.

>>> 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'^interface'):
...     obj.append_to_family(' carrier-delay msec 500')
>>>
>>> for line in parse.ioscfg:
...     print(line)
...
!
interface Serial1/0
 ip address 1.1.1.1 255.255.255.252
 carrier-delay msec 500
!
interface Serial1/1
 ip address 1.1.1.5 255.255.255.252
 carrier-delay msec 500
!
>>>
delete(recurse=True)

Delete this object. By default, if a parent object is deleted, the child objects are also deleted; this happens because recurse defaults True.

delete_children_matching(linespec)

Delete any child IOSCfgLine objects which match linespec.

Parameters
linespecstr

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

Returns
list

A list of IOSCfgLine objects which were deleted.

Examples

This example illustrates how you can use delete_children_matching() to delete any description on an interface.

>>> from ciscoconfparse import CiscoConfParse
>>> config = [
...     '!',
...     'interface Serial1/0',
...     ' description Some lame description',
...     ' ip address 1.1.1.1 255.255.255.252',
...     '!',
...     'interface Serial1/1',
...     ' description Another lame description',
...     ' ip address 1.1.1.5 255.255.255.252',
...     '!',
...     ]
>>> parse = CiscoConfParse(config)
>>>
>>> for obj in parse.find_objects(r'^interface'):
...     obj.delete_children_matching(r'description')
>>>
>>> for line in parse.ioscfg:
...     print(line)
...
!
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
!
>>>
property geneology

Iterate through to the oldest ancestor of this object, and return a list of 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.

property geneology_text

Iterate through to the oldest ancestor of this object, and return a list of 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.

property hash_children

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

insert_after()
insert_before()
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_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
>>>
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 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.

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
regexstr

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.

>>> 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, recurse=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
regexstr

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.

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.

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

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
regexstr

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

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

Use regex to search this IOSCfgLine’s text.

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
regexstr

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
regexstr

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…

>>> 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(linespec, replacestr, 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. The replace() method is simply an alias to the re_sub() method.

Parameters
linespecstr

A string or python regular expression, which should be matched

replacestrstr

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

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 linespec are skipped.

Returns
str

The new text after replacement

Examples

This example illustrates how you can use replace() to replace Serial1 with Serial0 in a configuration…

>>> 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.replace(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
>>>