Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def testValues_insert_before_atomic_01(self):
inputs = ['interface GigabitEthernet4/1',
'interface GigabitEthernet4/2',
'interface GigabitEthernet4/3',
'interface GigabitEthernet4/4',
'interface GigabitEthernet4/5',
'interface GigabitEthernet4/6',
'interface GigabitEthernet4/7',
'interface GigabitEthernet4/8',
]
cfg = CiscoConfParse(self.c01, factory=False)
for idx, linespec in enumerate(inputs):
test_result = cfg.insert_before(linespec, 'default '+linespec,
exactmatch=True, atomic=True)
result_correct = [inputs[idx]]
self.assertEqual(result_correct, test_result)
def testVal_IOSIntfLine_ipv4_masklength(self):
cfg = CiscoConfParse(self.c01, factory=True)
result_correct = {
'interface Serial 1/0': 30,
'interface Serial 1/1': 31,
'interface GigabitEthernet4/1': 0,
'interface GigabitEthernet4/2': 0,
'interface GigabitEthernet4/3': 0,
'interface GigabitEthernet4/4': 0,
'interface GigabitEthernet4/5': 0,
'interface GigabitEthernet4/6': 0,
'interface GigabitEthernet4/7': 0,
'interface GigabitEthernet4/8.120': 24,
'interface ATM5/0/0': 0,
'interface ATM5/0/0.32 point-to-point': 30,
'interface ATM5/0/1': 0,
}
test_result = dict()
def testVal_object_group_network(self):
conf = ['!',
'name 1.1.2.20 loghost01',
'!',
'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',
'!',]
cfg_factory = CiscoConfParse(conf, factory=True, syntax='asa')
obj = cfg_factory.find_objects(r'object-group\snetwork')[0]
# Ensure obj.name is set correctly
self.assertEqual(obj.name, "INSIDE_addrs")
result_correct = [IPv4Obj('1.1.2.20/32'), IPv4Obj('1.1.2.1/32'),
IPv4Obj('1.1.2.2/32'), IPv4Obj('1.1.2.0/24')]
self.assertEqual(obj.networks, result_correct)
sys.exit(0)
#--------------------------------------------------------------------------
# TODO Argument error handling
#--------------------------------------------------------------------------
if not len(args) > 2:
optparser.print_help()
sys.exit(-1)
parentspec = args[0]
childspec = args[1]
configfile = args[2]
cfg = CiscoConfParse(configfile)
if options.invertmatch:
result = find_parent_w_child(cfg, parentspec, childspec, invertmatch=True)
else:
result = find_parent_w_child(cfg, parentspec, childspec)
if options.aftercontext:
result = find_all_children(cfg, result)
output_lines(result)
sys.exit(0)
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 = []
def _get_interface_cfg(self, interface):
ioscfg = self._get_running_config()
parse = ciscoconfparse.CiscoConfParse(ioscfg)
return parse.find_children('interface ' + interface)
def cisco_conf_parse_parents(parent, child, config):
"""
Use CiscoConfParse to find parent lines that contain a specific child line.
:param parent: The parent line to search for
:param child: The child line required under the given parent
:param config: The device running/startup config
"""
if type(config) == str:
config = config.splitlines()
parse = CiscoConfParse(config)
cfg_obj = parse.find_parents_w_child(parent, child)
return cfg_obj
def update_conf_dict(element_remove, config_file, element_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 k,v in element_remove.items():
element_type_new = element_type + " " + k
for i in v:
for obj in parse.find_objects(r"^%s %s" % (element_type_new, i)):
obj.delete(r"^%s %s" % (element_type_new, i))
return generate_conf(parse)
def main():
'''
Using ciscoconfparse find the crypto maps that are not using AES (based-on
the transform set name). Print these entries and their corresponding
transform set name.
'''
cisco_file = 'cisco_ipsec.txt'
cisco_cfg = CiscoConfParse(cisco_file)
crypto_maps = cisco_cfg.find_objects_wo_child(parentspec=r'crypto map CRYPTO',
childspec=r'AES')
print "\nCrypto maps not using AES:"
for entry in crypto_maps:
for child in entry.children:
if 'transform' in child.text:
match = re.search(r"set transform-set (.*)$", child.text)
encryption = match.group(1)
print " {0} >>> {1}".format(entry.text.strip(), encryption)
print
def _get_static_route_cfg(self):
ioscfg = self._get_running_config()
parse = ciscoconfparse.CiscoConfParse(ioscfg)
return parse.find_lines('ip route')