Пример #1
0
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') == []
Пример #2
0
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
Пример #3
0
def testVal_object_group_network_01():
    """Test object group network results"""
    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]

    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')
    ]
    result_correct_02 = [
        '1.1.2.20', '1.1.2.1', '1.1.2.2', '1.1.2.0/255.255.255.0'
    ]
    # 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
Пример #4
0
def testValues_banner_delimiter_05():
    # Test multiple banners
    CONFIG = [
        '!', 'banner motd ^', '    trivial banner1 here ^', 'banner exec ^',
        '    trivial banner2 here ^', 'end'
    ]
    parse = CiscoConfParse(CONFIG)
    bannerobj = parse.find_objects('^banner\smotd')[0]
    BANNER_LINE_NUMBER = 1
    assert bannerobj.linenum == BANNER_LINE_NUMBER
    for obj in bannerobj.children:
        assert obj.parent.linenum == BANNER_LINE_NUMBER

    bannerobj = parse.find_objects('^banner\sexec')[0]
    BANNER_LINE_NUMBER = 3
    assert bannerobj.linenum == BANNER_LINE_NUMBER
    for obj in bannerobj.children:
        assert obj.parent.linenum == BANNER_LINE_NUMBER
Пример #5
0
def testValues_banner_delimiter_04():
    # Test multiple banner delimiters on different lines
    CONFIG = ['!', 'banner motd ^', '    trivial banner here ^^', 'end']
    parse = CiscoConfParse(CONFIG)
    bannerobj = parse.find_objects('^banner')[0]
    BANNER_LINE_NUMBER = 1
    assert bannerobj.linenum == BANNER_LINE_NUMBER
    for obj in bannerobj.children:
        assert obj.parent.linenum == BANNER_LINE_NUMBER
Пример #6
0
def testVal_ipv4_addr():
    conf = [
        '!',
        'interface Ethernet0/0',
        ' nameif OUTSIDE',
        ' ip address 198.101.172.106 255.255.255.128 standby 198.101.172.107',
        '!',
        'interface Ethernet0/1',
        ' nameif INSIDE',
        ' ip address 192.0.2.254 255.255.255.0',
        '!',
    ]
    cfg_factory = CiscoConfParse(conf, factory=True, syntax='asa')

    obj = cfg_factory.find_objects(r'^interface\sEthernet0\/0$')[0]
    # Ensure obj.ipv4_addr is set correctly
    assert obj.ipv4_addr == '198.101.172.106'
    assert obj.ipv4_standby_addr == '198.101.172.107'

    obj = cfg_factory.find_objects(r'^interface\sEthernet0\/1$')[0]
    assert obj.ipv4_addr == '192.0.2.254'
Пример #7
0
def testValues_banner_delete_02():
    # Ensure multiline banners are correctly deleted
    #
    # Check for Github issue #37
    CONFIG = [
        '!', 'interface GigabitEthernet0/0',
        ' ip address 192.0.2.1 255.255.255.0', 'banner motd ^',
        '    trivial banner1 here ^', 'interface GigabitEthernet0/1',
        ' 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') == []

    # Github issue #37 assigned Gi0/1's child to Gi0/0 after deleting
    #  the banner motd line...
    for obj in parse.find_objects('^interface'):
        assert len(obj.children) == 1
Пример #8
0
def testVal_object_group_service_03():
    ## This can only be configured as an object group after a host / network
    conf = [
        '!',
        'object-group service APP03_svc tcp-udp',
        ' port-object eq domain',
        '!',
    ]
    cfg_factory = CiscoConfParse(conf, factory=True, syntax='asa')
    obj = cfg_factory.find_objects(r'object-group\sservice')[0]
    ## Test whether the proper port objects are returned
    results_correct = [
        L4Object(port_spec='eq 53', protocol='tcp', syntax='asa'),
        L4Object(port_spec='eq 53', protocol='udp', syntax='asa')
    ]
    assert (obj.name == 'APP03_svc')
    assert (obj.ports == results_correct)
    assert (obj.L4Objects_are_directional is False)
    assert (obj.protocol_type == 'tcp-udp')
Пример #9
0
def testVal_object_group_service_01():
    ## This can only be configured as protocol object-group
    conf = [
        '!',
        'object-group service APP01_svc',
        ' service-object tcp destination smtp',
        ' service-object tcp destination https',
        '!',
    ]
    cfg_factory = CiscoConfParse(conf, factory=True, syntax='asa')
    obj = cfg_factory.find_objects(r'object-group\sservice')[0]
    result_correct = [
        L4Object(protocol='tcp', port_spec='eq 25', syntax='asa'),
        L4Object(protocol='tcp', port_spec='eq 443', syntax='asa')
    ]
    assert (obj.name == 'APP01_svc')
    assert (obj.ports == result_correct)
    assert (obj.L4Objects_are_directional is True)
    assert (obj.protocol_type == '')
Пример #10
0
def testValues_find_objects_delete_01():
    """Test whether IOSCfgLine.delete() recurses through children correctly"""
    config = [
        '!',
        'interface Serial1/0',
        ' encapsulation ppp',
        ' ip address 1.1.1.1 255.255.255.252',
        ' no ip proxy-arp',
        '!',
        'interface Serial1/1',
        ' encapsulation ppp',
        ' ip address 1.1.1.5 255.255.255.252',
        '!',
    ]
    result_correct = ['!', '!', '!']
    cfg = CiscoConfParse(config)
    for intf in cfg.find_objects(r'^interface'):
        # Delete all the interface objects
        intf.delete(recurse=True)  # recurse=True is the default
    test_result = cfg.ioscfg
    assert result_correct == test_result
Пример #11
0
def testVal_object_group_service_02():
    ## This can only be configured as an object group after a host / network
    conf = [
        '!',
        'object-group service APP02_svc tcp',
        ' port-object eq smtp',
        ' port-object eq https',
        ' port-object range 8080 8081',
        '!',
    ]
    cfg_factory = CiscoConfParse(conf, factory=True, syntax='asa')
    obj = cfg_factory.find_objects(r'object-group\sservice')[0]
    result_correct = [
        L4Object(protocol='tcp', port_spec='eq 25', syntax='asa'),
        L4Object(protocol='tcp', port_spec='eq 443', syntax='asa'),
        L4Object(protocol='tcp', port_spec='range 8080 8081', syntax='asa')
    ]
    assert (obj.name == 'APP02_svc')
    assert (obj.ports == result_correct)
    assert (obj.L4Objects_are_directional is False)
    assert (obj.protocol_type == 'tcp')