本文整理汇总了Python中ciscoconfparse.CiscoConfParse.find_objects方法的典型用法代码示例。如果您正苦于以下问题:Python CiscoConfParse.find_objects方法的具体用法?Python CiscoConfParse.find_objects怎么用?Python CiscoConfParse.find_objects使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ciscoconfparse.CiscoConfParse
的用法示例。
在下文中一共展示了CiscoConfParse.find_objects方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: update_config_file_parse
# 需要导入模块: from ciscoconfparse import CiscoConfParse [as 别名]
# 或者: from ciscoconfparse.CiscoConfParse import find_objects [as 别名]
def update_config_file_parse(item_remove, config_file, type):
"""Update config file with unused object-group, ACL, and group-policy statements removed; ciscoconfparse library needed to remove child objects"""
parse = CiscoConfParse(config_file)
for i in item_remove:
if type == 'obg':
for obj in parse.find_objects(r"^object-group network %s" %i):
obj.delete(r"^object-group network %s" %i)
elif type == 'acl':
for obj in parse.find_objects(r"^access-list %s" %i):
obj.delete(r"^access-list %s" %i)
elif type == 'gp':
for obj in parse.find_objects(r"^group-policy %s" %i):
obj.delete(r"^group-policy %s" %i)
config_file_new = []
for line in parse.ioscfg:
config_file_new.append(line)
return config_file_new
示例2: testValues_parent_child_parsing_02
# 需要导入模块: from ciscoconfparse import CiscoConfParse [as 别名]
# 或者: from ciscoconfparse.CiscoConfParse import find_objects [as 别名]
def testValues_parent_child_parsing_02(self):
cfg = CiscoConfParse(self.c01)
# Expected child / parent line numbers before the insert
parent_intf_before = {
# Line 11 is Serial1/0, child is line 13
13: 11,
# Line 15 is GigabitEthernet4/1
16: 15,
17: 15,
18: 15,
19: 15,
# Line 21 is GigabitEthernet4/2
22: 21,
23: 21,
24: 21,
25: 21,
}
# Expected child / parent line numbers after the insert
# Validate line numbers *before* inserting
for obj in cfg.find_objects(''):
result_correct = parent_intf_before.get(obj.linenum, False)
if result_correct:
test_result = obj.parent.linenum
## Does this object parent's line number match?
self.assertEqual(result_correct, test_result)
# Insert lines here...
for intf_obj in cfg.find_objects('^interface\sGigabitEthernet'):
# Configured with an access vlan...
if ' switchport access vlan 100' in set(map(attrgetter('text'), intf_obj.children)):
intf_obj.insert_after(' spanning-tree portfast')
cfg.atomic()
parent_intf_after = {
# Line 11 is Serial1/0, child is line 13
13: 11,
# Line 15 is GigabitEthernet4/1
16: 15,
17: 15,
18: 15,
19: 15,
# Line 22 is GigabitEthernet4/2
23: 22,
24: 22,
25: 22,
26: 22,
}
# Validate line numbers *after* inserting
for obj in cfg.find_objects(''):
result_correct = parent_intf_after.get(obj.linenum, False)
if result_correct:
test_result = obj.parent.linenum
## Does this object parent's line number match?
self.assertEqual(result_correct, test_result)
示例3: update_config_file_parse
# 需要导入模块: from ciscoconfparse import CiscoConfParse [as 别名]
# 或者: from ciscoconfparse.CiscoConfParse import find_objects [as 别名]
def update_config_file_parse(item_remove, config_file, type):
parse = CiscoConfParse(config_file)
for i in item_remove:
if type == 'obg':
for obj in parse.find_objects(r"^object-group network %s" %i):
obj.delete(r"^object-group network %s" %i)
elif type == 'acl':
for obj in parse.find_objects(r"^access-list %s" %i):
obj.delete(r"^access-list %s" %i)
config_file_new = []
for line in parse.ioscfg:
config_file_new.append(line)
return config_file_new
示例4: testValues_banner_delete_01
# 需要导入模块: from ciscoconfparse import CiscoConfParse [as 别名]
# 或者: from ciscoconfparse.CiscoConfParse import find_objects [as 别名]
def testValues_banner_delete_01():
# Ensure multiline banners are correctly deleted
CONFIG = ['!',
'banner motd ^', ' trivial banner1 here ^',
'interface GigabitEthernet0/0',
' ip address 192.0.2.1 255.255.255.0',
'banner exec ^', ' trivial banner2 here ^',
'end']
parse = CiscoConfParse(CONFIG)
for obj in parse.find_objects('^banner'):
obj.delete()
parse.commit()
assert parse.find_objects('^banner')==[]
示例5: netcompare
# 需要导入模块: from ciscoconfparse import CiscoConfParse [as 别名]
# 或者: from ciscoconfparse.CiscoConfParse import find_objects [as 别名]
def netcompare(origin, target, vendor, config):
origin_file = CiscoConfParse(origin,
comment=config[vendor]
['CiscoConfParse_comment'],
syntax=config[vendor]
['CiscoConfParse_syntax'],
factory=False)
target_file = CiscoConfParse(target,
comment=config[vendor]
['CiscoConfParse_comment'],
syntax=config[vendor]
['CiscoConfParse_syntax'],
factory=False)
result = {}
for line_origin in origin_file.objs:
eq_lines = (target_file.find_objects(
'^' + re.escape(line_origin.text) + '$'))
for line_target in eq_lines:
if line_origin.geneology_text == line_target.geneology_text:
break
else: # Delete needed
pointer = result
index = len(line_origin.geneology_text)
for cmd in line_origin.geneology_text:
index = index - 1
if ('NO', cmd) in pointer:
break
if ('_CR', cmd) in pointer:
pointer = pointer.get(('_CR', cmd))
elif index == 0:
pointer[('NO', cmd)] = {}
pointer = pointer.get(('NO', cmd))
else:
pointer[('_CR', cmd)] = {}
pointer = pointer.get(('_CR', cmd))
for line_target in target_file.objs:
find = 0
eq_lines = (origin_file.find_objects(
'^' + re.escape(line_target.text) + '$'))
for line_origin in eq_lines:
if line_origin.geneology_text == line_target.geneology_text:
find = 1
if find == 0: # Create needed
pointer = result
for cmd in line_target.geneology_text:
if not ('_CR', cmd) in pointer:
pointer[('_CR', cmd)] = {}
pointer = pointer.get(('_CR', cmd))
return result
示例6: testVal_IOSIntfLine_in_ipv4_subnets
# 需要导入模块: from ciscoconfparse import CiscoConfParse [as 别名]
# 或者: from ciscoconfparse.CiscoConfParse import find_objects [as 别名]
def testVal_IOSIntfLine_in_ipv4_subnets(self):
cfg = CiscoConfParse(self.c01, factory=True)
result_correct = {
'interface Serial 1/0': True,
'interface Serial 1/1': True,
'interface GigabitEthernet4/1': None,
'interface GigabitEthernet4/2': None,
'interface GigabitEthernet4/3': None,
'interface GigabitEthernet4/4': None,
'interface GigabitEthernet4/5': None,
'interface GigabitEthernet4/6': None,
'interface GigabitEthernet4/7': None,
'interface GigabitEthernet4/8.120': True,
'interface ATM5/0/0': None,
'interface ATM5/0/0.32 point-to-point': True,
'interface ATM5/0/1': None,
}
test_result = dict()
## Parse all interface objects in self.c01 and check in_ipv4_subnets
test_network1 = IPv4Obj('1.1.0.0/23', strict=False)
test_network2 = IPv4Obj('1.1.2.0/23', strict=False)
networks = set([test_network1, test_network2])
for intf_obj in cfg.find_objects('^interface'):
test_result[intf_obj.text] = intf_obj.in_ipv4_subnets(networks)
self.maxDiff = None
self.assertEqual(result_correct, test_result)
示例7: testValues_find_objects_factory_01
# 需要导入模块: from ciscoconfparse import CiscoConfParse [as 别名]
# 或者: from ciscoconfparse.CiscoConfParse import find_objects [as 别名]
def testValues_find_objects_factory_01(self):
"""Test whether find_objects returns the correct objects"""
with patch('__main__.IOSIntfLine') as mock:
vals = [('interface GigabitEthernet4/1', 15),
('interface GigabitEthernet4/2', 21),
('interface GigabitEthernet4/3', 27),
('interface GigabitEthernet4/4', 32),
('interface GigabitEthernet4/5', 35),
('interface GigabitEthernet4/6', 39),
('interface GigabitEthernet4/7', 43),
('interface GigabitEthernet4/8', 47),
]
## Build fake IOSIntfLine objects to validate unit tests...
result_correct = list()
# deepcopy a unique mock for every val with itertools.repeat()
mockobjs = map(deepcopy, repeat(mock, len(vals)))
# mock pretends to be an IOSCfgLine so we can test against it
for idx, obj in enumerate(mockobjs):
obj.text = vals[idx][0] # Check the text
obj.linenum = vals[idx][1] # Check the line numbers
# append the fake IOSIntfLine object to result_correct
result_correct.append(obj)
cfg = CiscoConfParse(self.c01, factory=True)
test_result = cfg.find_objects('^interface GigabitEther')
for idx, test_result_object in enumerate(test_result):
# Check line numbers
self.assertEqual(result_correct[idx].linenum,
test_result_object.linenum)
# Check text
self.assertEqual(result_correct[idx].text,
test_result_object.text)
示例8: testValues_find_objects_replace_01
# 需要导入模块: from ciscoconfparse import CiscoConfParse [as 别名]
# 或者: from ciscoconfparse.CiscoConfParse import find_objects [as 别名]
def testValues_find_objects_replace_01():
"""test whether find_objects we can correctly replace object values using native IOSCfgLine object methods"""
config01 = ['!',
'boot system flash slot0:c2600-adventerprisek9-mz.124-21a.bin',
'boot system flash bootflash:c2600-adventerprisek9-mz.124-21a.bin',
'!',
'interface Ethernet0/0',
' ip address 172.16.1.253 255.255.255.0',
'!',
'ip route 0.0.0.0 0.0.0.0 172.16.1.254',
'!',
'end',
]
result_correct = ['!',
'! old boot image flash slot0:c2600-adventerprisek9-mz.124-21a.bin',
'! old boot image flash bootflash:c2600-adventerprisek9-mz.124-21a.bin',
'!',
'interface Ethernet0/0',
' ip address 172.16.1.253 255.255.255.0',
'!',
'ip route 0.0.0.0 0.0.0.0 172.16.1.254',
'!',
'end',
]
cfg = CiscoConfParse(config01)
for obj in cfg.find_objects('boot system'):
obj.replace('boot system', '! old boot image')
test_result = cfg.ioscfg
assert result_correct==test_result
示例9: testVal_IOSIntfLine_ipv4_network_object
# 需要导入模块: from ciscoconfparse import CiscoConfParse [as 别名]
# 或者: from ciscoconfparse.CiscoConfParse import find_objects [as 别名]
def testVal_IOSIntfLine_ipv4_network_object(self):
cfg = CiscoConfParse(self.c01, factory=True)
result_correct = {
'interface Serial 1/0': IPv4Obj('1.1.1.0/30', strict=False),
'interface Serial 1/1': IPv4Obj('1.1.1.8/31', strict=False),
'interface GigabitEthernet4/1': IPv4Obj('127.0.0.1/32',
strict=False),
'interface GigabitEthernet4/2': IPv4Obj('127.0.0.1/32',
strict=False),
'interface GigabitEthernet4/3': IPv4Obj('127.0.0.1/32',
strict=False),
'interface GigabitEthernet4/4': IPv4Obj('127.0.0.1/32',
strict=False),
'interface GigabitEthernet4/5': IPv4Obj('127.0.0.1/32',
strict=False),
'interface GigabitEthernet4/6': IPv4Obj('127.0.0.1/32',
strict=False),
'interface GigabitEthernet4/7': IPv4Obj('127.0.0.1/32',
strict=False),
'interface GigabitEthernet4/8.120': IPv4Obj('1.1.2.0/24',
strict=False),
'interface ATM5/0/0': IPv4Obj('127.0.0.1/32', strict=False),
'interface ATM5/0/0.32 point-to-point': IPv4Obj('1.1.1.4/30',
strict=False),
'interface ATM5/0/1': IPv4Obj('127.0.0.1/32', strict=False),
}
test_result = dict()
## Parse all interface objects in self.c01 and check ipv4_network_object
for intf_obj in cfg.find_objects('^interface'):
test_result[intf_obj.text] = intf_obj.ipv4_network_object
self.assertEqual(result_correct, test_result)
示例10: testValues_IOSIntfLine_find_objects_factory_02
# 需要导入模块: from ciscoconfparse import CiscoConfParse [as 别名]
# 或者: from ciscoconfparse.CiscoConfParse import find_objects [as 别名]
def testValues_IOSIntfLine_find_objects_factory_02(self):
"""test whether find_objects() returns correct IOSIntfLine objects and tests IOSIntfLine methods"""
with patch('__main__.IOSIntfLine') as mock:
# the mock pretends to be an IOSCfgLine so we can test against it
result_correct01 = mock
result_correct01.linenum = 12
result_correct01.text = 'interface Serial 2/0'
result_correct01.classname = 'IOSIntfLine'
result_correct01.ipv4_addr_object = IPv4Network('1.1.1.1/30')
result_correct02 = self.c01_insert_serial_replace
cfg = CiscoConfParse(self.c01, factory=True)
# Insert a line above the IOSIntfLine object
cfg.insert_before('interface Serial 1/0', 'default interface Serial 1/0')
# Replace text in the IOSIntfLine object
cfg.replace_lines('interface Serial 1/0', 'interface Serial 2/0', exactmatch=False)
test_result01 = cfg.find_objects('^interface Serial 2/0')[0]
test_result02 = cfg.ioscfg
# Check attributes of the IOSIntfLine object
self.assertEqual(result_correct01.linenum, test_result01.linenum)
self.assertEqual(result_correct01.text, test_result01.text)
self.assertEqual(result_correct01.classname, test_result01.classname)
self.assertEqual(result_correct01.ipv4_addr_object,
test_result01.ipv4_addr_object)
# Ensure the text configs are exactly what we wanted
self.assertEqual(result_correct02, test_result02)
示例11: testVal_object_group_network_02
# 需要导入模块: from ciscoconfparse import CiscoConfParse [as 别名]
# 或者: from ciscoconfparse.CiscoConfParse import find_objects [as 别名]
def testVal_object_group_network_02():
"""Test recursion through a group object"""
conf = ['!',
'name 1.1.2.20 loghost01',
'name 1.2.2.20 loghost02',
'!',
'object-group network INSIDE_recurse',
' network-object host loghost02',
'object-group network INSIDE_addrs',
' network-object host loghost01',
' network-object host 1.1.2.1',
' network-object 1.1.2.2 255.255.255.255',
' network-object 1.1.2.0 255.255.255.0',
' group-object INSIDE_recurse',
'!',]
cfg_factory = CiscoConfParse(conf, factory=True, syntax='asa')
obj = cfg_factory.find_objects(r'object-group\snetwork')[1]
result_correct_01 = [IPv4Obj('1.1.2.20/32'), IPv4Obj('1.1.2.1/32'),
IPv4Obj('1.1.2.2/32'), IPv4Obj('1.1.2.0/24'), IPv4Obj('1.2.2.20/32')]
result_correct_02 = ['1.1.2.20', '1.1.2.1', '1.1.2.2',
'1.1.2.0/255.255.255.0', '1.2.2.20']
# Ensure obj.name is set correctly
assert obj.name=="INSIDE_addrs"
assert obj.networks==result_correct_01
assert obj.network_strings==result_correct_02
## Test obj.networks again to test the result_cache
assert obj.networks==result_correct_01
示例12: testValues_insert_after_atomic_factory_01
# 需要导入模块: from ciscoconfparse import CiscoConfParse [as 别名]
# 或者: from ciscoconfparse.CiscoConfParse import find_objects [as 别名]
def testValues_insert_after_atomic_factory_01(self):
"""Ensure that comments which are added, assert is_comment"""
with patch('__main__.IOSCfgLine') as mock:
# the mock pretends to be an IOSCfgLine so we can test against it
result_correct01 = mock
result_correct01.linenum = 16
result_correct01.text = ' ! TODO: some note to self'
result_correct01.classname = 'IOSCfgLine'
result_correct01.is_comment = True
result_correct02 = [
'interface GigabitEthernet4/1',
' ! TODO: some note to self',
' switchport',
' switchport access vlan 100',
' switchport voice vlan 150',
' power inline static max 7000',
]
linespec = 'interface GigabitEthernet4/1'
cfg = CiscoConfParse(self.c01, factory=True)
cfg.insert_after(linespec, ' ! TODO: some note to self',
exactmatch=True, atomic=True)
test_result01 = cfg.find_objects('TODO')[0]
self.assertEqual(result_correct01.linenum, test_result01.linenum)
self.assertEqual(result_correct01.text, test_result01.text)
self.assertEqual(result_correct01.classname, test_result01.classname)
self.assertEqual(result_correct01.is_comment, test_result01.is_comment)
# FIXME: this fails... maybe because I don't parse comments as children correctly???
test_result02 = cfg.find_children(linespec)
self.assertEqual(result_correct02, test_result02)
示例13: get_interfaces
# 需要导入模块: from ciscoconfparse import CiscoConfParse [as 别名]
# 或者: from ciscoconfparse.CiscoConfParse import find_objects [as 别名]
def get_interfaces(device):
"""Get the Interfaces off a device"""
conf_file = conf_dir + device['Device'] + "-confg"
if device['Type'] == 'Primary' or device['Type'] == 'Standby':
try:
parse = CiscoConfParse(conf_file)
except:
return
else:
default_shut = False
if len(parse.find_objects('no shutdown')):
default_shut = True
vlan_ints = parse_vlan_interfaces(parse, device['Device'], default_shut)
for en in vlan_ints:
en['MgmtGroup'] = device['MgmtGroup']
if device['Type'] == 'Standby':
en['Standby'] = True
else:
en['Standby'] = False
interface_list.append(en)
l3_ints = parse_l3_interfaces(parse, device['Device'], default_shut)
for en in l3_ints:
en['MgmtGroup'] = device['MgmtGroup']
if device['Type'] == 'Standby':
en['Standby'] = True
else:
en['Standby'] = False
interface_list.append(en)
示例14: main
# 需要导入模块: from ciscoconfparse import CiscoConfParse [as 别名]
# 或者: from ciscoconfparse.CiscoConfParse import find_objects [as 别名]
def main():
cisco_ios = CiscoConfParse("cisco_ipsec.txt")
crypto_map = cisco_ios.find_objects(r'crypto map')
for child in crypto_map:
print child.text
for i in child.children:
print i.text
示例15: main
# 需要导入模块: from ciscoconfparse import CiscoConfParse [as 别名]
# 或者: from ciscoconfparse.CiscoConfParse import find_objects [as 别名]
def main():
cisco_cfg = CiscoConfParse("cisco_ipsec.txt")
map_list = cisco_cfg.find_objects(r"^crypto map CRYPTO")
for cryptoMap in map_list:
print cryptoMap.text
mapChildren = cryptoMap.children
for child in mapChildren:
print child.text
print '\nCrypto maps using PFS group 2:\n'
pfs2_list = cisco_cfg.find_objects_w_child(parentspec=r"^crypto map CRYPTO", childspec=r"set pfs group2")
for cryptoMap in pfs2_list:
print cryptoMap.text
print '\nCrypto maps not using AES:\n'
noaes_list = cisco_cfg.find_objects_wo_child(parentspec=r"^crypto map CRYPTO", childspec=r"set transform-set AES-SHA")
for cryptoMap in noaes_list:
print cryptoMap.text
mapChildren = cryptoMap.children
transformSetLine = mapChildren[1]
(head,transformSet) = transformSetLine.text.split('set transform-set')
print transformSet