Exemple #1
0
def dealABC(fwFile, ipPlans):
    ips = []

    for ipPlan in ipPlans:
        ipPlan['vlan'] = str(ipPlan['vlan'])
        if ipPlan['vlan'].upper().strip() == 'CORE_TO_AR1':
            ips.append(
                '.'.join(ipPlan['ipStart'].split('-')[0].split('.')[:-1]) +
                '.' + str(
                    int(ipPlan['ipStart'].split('-')[0].split('.')[-1].strip())
                    + 1))
        if ipPlan['vlan'].upper().strip() == 'CORE_TO_AR2':
            ips.append(
                '.'.join(ipPlan['ipStart'].split('-')[0].split('.')[:-1]) +
                '.' + str(
                    int(ipPlan['ipStart'].split('-')[0].split('.')[-1].strip())
                    + 1))
        if str(ipPlan['vlan']).strip() == '7' or str(
                ipPlan['vlan']).strip() == '8':
            Flag78 = True
    # print(ips)
    sysname = fwFile['sysname']
    file = fwFile['fwFile']
    with open(sysname + '.txt', 'w') as f:
        string = ''
        with openConfigFile.OpenConfigFile().open(
                file, mode='r', pwd='1qaz@WSX3EDc95511'.encode('utf-8')) as f1:
            for line in f1:
                line = line.decode('gbk', 'ignore')
                string += line
                if line.strip() == '#':
                    if re.search('sysname[\s\S]*?', string):
                        string = re.sub('sysname[\s\S]*?\n',
                                        'sysname ' + sysname + '\n', string)
                    if re.search('unicast-server', string):
                        ntps = re.findall('unicast-server[\s\S]*?\n', string)
                        # print(ntps)
                        for i in range(len(ips)):
                            string = re.sub(ntps[i],
                                            'unicast-server ' + ips[i] + '\n',
                                            string)
                    if re.search('interfaceeth-trunk1',
                                 re.sub(' ', '', string.lower())):
                        string = re.sub(
                            'ip\s*address[\s\S]*?\n', 'ip address ' +
                            '.'.join(ipPlans[8]['ipStart'].split('-')[0].split(
                                '.')[:-1]) + '.' + str(
                                    int(ipPlans[8]['ipStart'].split('-')
                                        [0].split('.')[-1].strip()) + 1) +
                            ' ' + str(int(ipPlans[8]['mark'])) + '\n', string)
                    if re.search('interfaceeth-trunk2',
                                 re.sub(' ', '', string.lower())):
                        string = re.sub(
                            'ip\s*address[\s\S]*?\n', 'ip address ' +
                            '.'.join(ipPlans[9]['ipStart'].split('-')[0].split(
                                '.')[:-1]) + '.' + str(
                                    int(ipPlans[9]['ipStart'].split('-')
                                        [0].split('.')[-1].strip()) + 1) +
                            ' ' + str(int(ipPlans[9]['mark'])) + '\n', string)
                    if re.search('network', string):
                        string = re.sub(
                            'network[\s\S]*?\n',
                            'network ' + ipPlans[8]['segment'] + '  ' +
                            wildcardMark[str(int(ipPlans[8]['mark']))] + '\n',
                            string)
                    if re.search('route-static', string):
                        routes = re.findall('route-static[\S\s]*?\n', string)
                        descriptions = [
                            ' description Wife  \n',
                            ' description Live&Training\n',
                            ' description Pc \n', ' description Secure \n',
                            ' description Other  \n',
                            ' description SW&AP-Manage \n'
                        ]
                        for i in range(len(routes)):
                            string = re.sub(
                                routes[i], 'route-static ' +
                                ipPlans[i]['ipStart'].split('-')[0] + ' ' +
                                str(int(ipPlans[i]['mark'])) + ' ' +
                                ipPlans[9]['ipStart'] + descriptions[i],
                                string)
                    if re.search('//[\s\S]*?\n', string) and not re.search(
                            '默认账号和密码查询网站', string):
                        string = re.sub('//[\s\S]*?\n', '\n', string)
                    f.write(re.sub('\r', '', string))
                    string = ''
        if string.strip() != '' and string.strip()[-1] != '#':
            if re.search('//[\s\S]*?\n',
                         string) and not re.search('默认账号和密码查询网站', string):
                string = re.sub('//[\s\S]*?\n', '\n', string)
            f.write(re.sub('\r', '', string))
Exemple #2
0
def dealSw(swFile, ipPlans, swportConnect, dns1, dns2, option, excelPath):
    # vlan78
    DNS = dns1 + ' ' + dns2
    vlan78Info, other = vlan78(ipPlans, DNS)
    sysname = swFile['sysname']
    file = swFile['swFile']
    portInfos = swPortInfos(swportConnect, other)
    with open(os.path.dirname(excelPath) + '/' + sysname + '.txt', 'w') as f5:
        with openConfigFile.OpenConfigFile().open(file, mode='r', pwd='1qaz@WSX3EDc95511'.encode('utf-8')) as f:
            string = ''
            for line in f:
                line = line.decode('gbk', 'ignore')
                string += line
                if line.strip() == '#':
                    if re.search('sysname[\s\S]*?\n', string):
                        string = re.sub('sysname[\s\S]*?\n', 'sysname ' + sysname + '\n', string)
                    if re.search('dhcpserverdns-list', re.sub(' ', '', string.lower())):
                        string = re.sub('dns-list[\s\S]*?\n', 'dns-list  ' + DNS + '\n', string)
                    if re.search('interfacevlanif2\s', re.sub(' ', '', string.lower())):
                        temp = re.search('(ip\s*address[\s\S]*?\n)', string).groups()[0]
                        string = re.sub(temp, 'ip address ' + ipPlans[0]['ipStart'] + ' ' + str(
                            int(ipPlans[0]['mark'])) + '\n', string)
                        temp = re.search('(server\s*ip-range[\s\S]*?\n)', string).groups()[0]
                        string = re.sub(temp, 'server ip-range ' + '.'.join(
                            ipPlans[0]['ipStart'].split('.')[:-1]) + '.' + str(
                            int(ipPlans[0]['ipStart'].split('.')[-1].strip()) + 1) + '  ' +
                                        ipPlans[0]['ipEnd'] + '\n', string)
                    if re.search('interfacevlanif3\s', re.sub(' ', '', string.lower())):
                        temp = re.search('(ip\s*address[\s\S]*?\n)', string).groups()[0]
                        string = re.sub(temp, 'ip address ' + ipPlans[1]['ipStart'] + ' ' + str(
                            int(ipPlans[1]['mark'])) + '\n', string)
                        temp = re.search('(server\s*ip-range[\s\S]*?\n)', string).groups()[0]
                        string = re.sub(temp, 'server ip-range ' + '.'.join(
                            ipPlans[1]['ipStart'].split('.')[:-1]) + '.' + str(
                            int(ipPlans[1]['ipStart'].split('.')[-1].strip()) + 1) + '  ' +
                                        ipPlans[1]['ipEnd'] + '\n', string)
                    if re.search('interfacevlanif4\s', re.sub(' ', '', string.lower())):
                        temp = re.search('(ip\s*address[\s\S]*?\n)', string).groups()[0]
                        string = re.sub(temp, 'ip address ' + ipPlans[2]['ipStart'] + ' ' + str(
                            int(ipPlans[2]['mark'])) + '\n', string)
                        temp = re.search('(server\s*ip-range[\s\S]*?\n)', string).groups()[0]
                        string = re.sub(temp, 'server ip-range ' + '.'.join(
                            ipPlans[2]['ipStart'].split('.')[:-1]) + '.' + str(
                            int(ipPlans[2]['ipStart'].split('.')[-1].strip()) + 1) + '  ' +
                                        ipPlans[2]['ipEnd'] + '\n', string)
                        # print(string)
                    if re.search('interfacevlanif5\s', re.sub(' ', '', string.lower())):
                        temp = re.search('(ip\s*address[\s\S]*?\n)', string).groups()[0]
                        string = re.sub(temp, 'ip address ' + ipPlans[3]['ipStart'] + ' ' + str(
                            int(ipPlans[3]['mark'])) + '\n', string)
                        temp = re.search('(server\s*ip-range[\s\S]*?\n)', string).groups()[0]
                        string = re.sub(temp, 'server ip-range ' + '.'.join(
                            ipPlans[3]['ipStart'].split('.')[:-1]) + '.' + str(
                            int(ipPlans[3]['ipStart'].split('.')[-1].strip()) + 1) + '  ' +
                                        ipPlans[3]['ipEnd'] + '\n', string)
                    #     # print(string)
                    if re.search('interfacevlanif6\s', re.sub(' ', '', string.lower())):
                        temp = re.search('(ip\s*address[\s\S]*?\n)', string).groups()[0]
                        string = re.sub(temp, 'ip address ' + ipPlans[4]['ipStart'] + ' ' + str(
                            int(ipPlans[4]['mark'])) + '\n', string)
                        temp = re.search('(server\s*ip-range[\s\S]*?\n)', string).groups()[0]
                        string = re.sub(temp, 'server ip-range ' + '.'.join(
                            ipPlans[4]['ipStart'].split('.')[:-1]) + '.' + str(
                            int(ipPlans[4]['ipStart'].split('.')[-1].strip()) + 1) + '  ' +
                                        ipPlans[4]['ipEnd'] + '\n', string)
                    if re.search('interfacevlanif4094', re.sub(' ', '', string.lower())):
                        temp = re.search('(ip\s*address[\s\S]*?\n)', string).groups()[0]
                        string = re.sub(temp, 'ip address ' + '.'.join(ipPlans[5]['ipStart'].split('.')[:-1]) + '.' + str(int(ipPlans[5]['ipStart'].split('.')[-1].strip())) + ' ' + str(
                            int(ipPlans[5]['mark'])) + '\n', string)
                        temp = re.search('(server\s*ip-range[\s\S]*?\n)', string).groups()[0]
                        string = re.sub(temp, 'server ip-range ' + '.'.join(ipPlans[5]['ipStart'].split('.')[:-1]) + '.' + str(int(ipPlans[5]['ipStart'].split('.')[-1].strip()) + 1 + int(ipPlans[5]['reservedNumber'])) + '  ' + ipPlans[5]['ipEnd'] + '\n', string)
                        temp = re.search('(dhcp\s*server\s*option[\s\S]*?\n)', string).groups()[0]
                        string = re.sub(temp, option + '\n', string)
                    if re.search('interfaceeth-trunk126\s', re.sub(' ', '', string.lower())):
                        temp = re.search('(ip\s*address[\s\S]*?\n)', string).groups()[0]
                        string = re.sub(temp, 'ip address ' + ipPlans[8]['ipStart'] + ' ' + str(
                            int(ipPlans[8]['mark'])) + '\n', string)
                    if re.search('interfaceeth-trunk127\s', re.sub(' ', '', string.lower())):
                        temp = re.search('(ip\s*address[\s\S]*?\n)', string).groups()[0]
                        string = re.sub(temp, 'ip address ' + ipPlans[9]['ipStart'] + ' ' + str(
                            int(ipPlans[9]['mark'])) + '\n', string)
                    if re.search('interfacegigabitethernet1/0/20', re.sub(' ', '', string.lower())) and re.search(
                            'address', string.lower()):
                        temp = re.search('(ip\s*address[\s\S]*?\n)', string).groups()[0]
                        string = re.sub(temp, 'ip address ' + ipPlans[6]['ipStart'] + ' ' + str(
                            int(ipPlans[6]['mark'])) + '\n', string)
                    #     # print(string)
                    if re.search('interfacegigabitethernet2/0/20', re.sub(' ', '', string.lower())) and re.search(
                            'address', string.lower()):
                        temp = re.search('(ip\s*address[\s\S]*?\n)', string).groups()[0]
                        string = re.sub(temp, 'ip address ' + ipPlans[7]['ipStart'] + ' ' + str(
                            int(ipPlans[7]['mark'])) + '\n', string)
                    if re.search('network', re.sub(' ', '', string.lower())):
                        networks = re.findall('network[\s\S]*?\n', string)
                        for i in range(len(networks)):
                            string = re.sub(networks[i], '  network ' + ipPlans[6 + i]['segment'] + '  0.0.0.7 \n', string)
                    if re.search('route-static\s*vpn-instance', string):
                        temp = re.findall('(ip\s*route-static\s*[\s\S]*?\n)', string)
                        string = re.sub(temp[0], 'ip route-static vpn-instance LAN 0.0.0.0 0.0.0.0 ' + '.'.join(
                            ipPlans[6]['ipStart'].split('.')[:-1]) + '.' + str(int(ipPlans[6]['ipStart'].split('.')[-1].strip()) + 1) + ' preference 100 track nqa admin To_R1\n', string)
                        string = re.sub(temp[1], 'ip route-static vpn-instance LAN 0.0.0.0 0.0.0.0 ' + '.'.join(
                            ipPlans[7]['ipStart'].split('.')[:-1]) + '.' + str(int(ipPlans[7]['ipStart'].split('.')[-1].strip()) + 1) + ' preference 100 track nqa admin To_R2\n', string)
                        try:
                            string = re.sub(temp[2], 'ip route-static vpn-instance LAN 0.0.0.0 0.0.0.0 ' + '.'.join(
                                ipPlans[9]['ipStart'].split('.')[:-1]) + '.' + str(int(ipPlans[9]['ipStart'].split('.')[-1].strip()) + 1) + ' track nqa admin To_FW \n',string)
                        except:
                            pass
                    if re.search('nexthop', string):
                        temp = re.search('nexthop([\s\S]*?)\n', string).groups()[0]
                        string = re.sub(temp, ' ' + '.'.join(ipPlans[8]['ipStart'].split('.')[:-1]) + '.' + str(
                            int(ipPlans[8]['ipStart'].split('.')[-1].strip()) + 1) + '  track-nqa admin To_FW_PBR\n', string)
                    if re.search('unicast-server', string):
                        temp = re.findall('unicast-server[\s\S]*?\n', string)
                        print(temp)
                        string = re.sub(temp[0], ' unicast-server ' + '.'.join(ipPlans[6]['ipStart'].split('.')[:-1]) + '.' + str(int(ipPlans[6]['ipStart'].split('.')[-1].strip()) + 1) + ' vpn-instance WAN\n',string)
                        string = re.sub(temp[1], ' unicast-server ' + '.'.join(
                            ipPlans[7]['ipStart'].split('.')[:-1]) + '.' + str(int(ipPlans[7]['ipStart'].split('.')[-1].strip()) + 1) + ' vpn-instance WAN\n',string)
                    if re.search('destination-address', string):
                        if re.search('To_FW\s', string):
                            temp = re.search('destination-address\s*ipv4([\s\S]*?)\n', string).groups()[0]
                            string = re.sub(temp,'  ' + '.'.join(ipPlans[8]['ipStart'].split('.')[:-1]) + '.' + str(int(ipPlans[8]['ipStart'].split('.')[-1].strip()) + 1), string)
                            temp = re.search('source-address\s*ipv4([\s\S]*?)\n', string).groups()[0]
                            string = re.sub(temp, '  ' + ipPlans[9]['ipStart'].strip(), string)
                        if re.search('To_R1\s', string):
                            temp = re.search('destination-address\s*ipv4([\s\S]*?)\n', string).groups()[0]
                            string = re.sub(temp, '  ' + '.'.join(
                                ipPlans[6]['ipStart'].split('.')[:-1]) + '.' + str(
                                int(ipPlans[6]['ipStart'].split('.')[-1].strip()) + 1), string)
                            temp = re.search('source-address\s*ipv4([\s\S]*?)\n', string).groups()[0]
                            string = re.sub(temp, '  ' + ipPlans[6]['ipStart'], string)
                        if re.search('To_R2\s', string):
                            temp = re.search('destination-address\s*ipv4([\s\S]*?)\n', string).groups()[0]
                            string = re.sub(temp, '  ' + '.'.join(
                                ipPlans[7]['ipStart'].split('.')[:-1]) + '.' + str(
                                int(ipPlans[7]['ipStart'].split('.')[-1].strip()) + 1), string)
                            temp = re.search('source-address\s*ipv4([\s\S]*?)\n', string).groups()[0]
                            string = re.sub(temp, '  ' + ipPlans[7]['ipStart'], string)
                        if re.search('To_FW_PBR\s', string):
                            temp = re.search('destination-address\s*ipv4([\s\S]*?)\n', string).groups()[0]
                            string = re.sub(temp,'  ' + '.'.join(ipPlans[9]['ipStart'].split('.')[:-1]) + '.' + str(int(ipPlans[9]['ipStart'].split('.')[-1].strip()) + 1), string)
                            temp = re.search('source-address\s*ipv4([\s\S]*?)\n', string).groups()[0]
                            string = re.sub(temp, '  ' + ipPlans[8]['ipStart'], string)
                    if re.search('port\s*trunk\s*allow-pass\s*vlan', string):
                        string = re.sub('port\s*trunk\s*allow-pass\s*vlan[\s\S]*?\n', other + '\n', string)
                    if re.search('三网合一配置', string):
                        f5.write(re.sub('\r', '', vlan78Info))
                        break
                    if re.search('NTP配置', string):
                        string = portInfos + string
                    if re.search('//[\s\S]*?\n', string) and not re.search('默认账号和密码查询网站', string) and not re.search('用户配置', string):
                        string = re.sub('//[\s\S]*?\n', '\n', string)
                    f5.write(re.sub('\r', '', string))
                    string = ''
            if string.strip() != '' and string.strip()[-1] != '#':
                if re.search('//[\s\S]*?\n', string) and not re.search('默认账号和密码查询网站', string) and not re.search('用户配置', string):
                    string = re.sub('//[\s\S]*?\n', '\n', string)
                f5.write(re.sub('\r', '', string))
Exemple #3
0
def dealDE(fwFile, ipPlans, fwportConnect, dns1, dns2, option):
    ips = []
    Flag78 = False
    vlan78Info = ''
    for ipPlan in ipPlans:
        ipPlan['vlan'] = str(ipPlan['vlan'])
        if ipPlan['vlan'].upper().strip() == 'CORE_TO_AR1':
            ips.append(
                '.'.join(ipPlan['ipStart'].split('-')[0].split('.')[:-1]) +
                '.' + str(
                    int(ipPlan['ipStart'].split('-')[0].split('.')[-1].strip())
                    + 1))
        if ipPlan['vlan'].upper().strip() == 'CORE_TO_AR2':
            ips.append(
                '.'.join(ipPlan['ipStart'].split('-')[0].split('.')[:-1]) +
                '.' + str(
                    int(ipPlan['ipStart'].split('-')[0].split('.')[-1].strip())
                    + 1))
        if str(ipPlan['vlan']).strip() == '7' or str(
                ipPlan['vlan']).strip() == '8':
            Flag78 = True
    # for ipPlan in ipPlans:
    #     print(ipPlan)
    fwPortInfos = ''
    firewall = 'firewall zone trust   \n'
    firewall += ' add interface vlanif2\n' + ' add interface vlanif3\n' + ' add interface vlanif4\n'
    firewall += ' add interface vlanif5\n' + ' add interface vlanif6\n' + ' add interface vlanif4094\n'
    for fwPort in fwportConnect:
        if fwPort['0'] != '' and fwPort['1'] != '' and fwPort['2'] != '':
            firewall += ' add interface  ' + fwPort['2'] + '\n'
            fwPortInfos += 'interface ' + fwPort['2'] + '\n'
            fwPortInfos += ' description To-LAN-AccessSwitch-Eth_Trunk' + fwPort[
                '2'].strip().split('k')[-1] + '\n'
            fwPortInfos += ''' portswitch
 service-manage ping permit
 service-manage ssh permit
 service-manage snmp permit\n'''
            if fwPort['mad'] != '':
                fwPortInfos += ' mad relay\n'
            fwPortInfos += '#\n'
            fwPortInfos += 'interface ' + fwPort['0'] + '\n'
            fwPortInfos += ' description To-AccessSwitch-' + '-'.join(
                fwPort['3']['sysname'].split('-')
                [-3:]) + '-Gi' + fwPort['3']['0'].split(
                    't')[-1] + '__From-CoreSwitch-01-Gi' + fwPort['0'].split(
                        't')[-1] + '\n'
            # fwPortInfos += ' description To-AccessSwitch-01-Gi0/0/X_From-FW-01-Gi' + fwPort['0'].split('t')[-1] + '\n'
            fwPortInfos += ' portswitch\n'
            fwPortInfos += ' eth-trunk ' + fwPort['2'].split('k')[-1] + '\n#\n'
            fwPortInfos += 'interface ' + fwPort['1'] + '\n'
            fwPortInfos += ' description To-AccessSwitch-' + '-'.join(
                fwPort['3']['sysname'].split('-')
                [-3:]) + '-Gi' + fwPort['3']['1'].split(
                    't')[-1] + '__From-CoreSwitch-02-Gi' + fwPort['1'].split(
                        't')[-1] + '\n'
            fwPortInfos += ' portswitch\n'
            fwPortInfos += ' eth-trunk ' + fwPort['2'].split('k')[-1] + '\n#\n'
        elif fwPort['0'] != '' and fwPort['1'] == '' and fwPort['2'] == '':
            firewall += ' add interface  ' + fwPort['0'] + '\n'
            fwPortInfos += 'interface ' + fwPort['0'] + '\n'
            fwPortInfos += ' description To-AccessSwitch-' + '-'.join(
                fwPort['3']['sysname'].split('-')
                [-3:]) + '-Gi' + fwPort['3']['0'].split(
                    't')[-1] + '__From-CoreSwitch-01-Gi' + fwPort['0'].split(
                        't')[-1] + '\n'
            fwPortInfos += ''' portswitch
 service-manage ping permit
 service-manage ssh permit
 service-manage snmp permit\n'''
            if fwPort['mad'] != '':
                fwPortInfos += ' mad relay\n'
            fwPortInfos += '#\n'
        elif fwPort['0'] == '' and fwPort['1'] != '' and fwPort['2'] == '':
            firewall += ' add interface  ' + fwPort['1'] + '\n'
            fwPortInfos += 'interface ' + fwPort['1'] + '\n'
            if fwPort['3']['0'] != '':
                fwPortInfos += ' description To-AccessSwitch-' + '-'.join(
                    fwPort['3']['sysname'].split('-')[-3:]
                ) + '-Gi' + fwPort['3']['0'].split(
                    't')[-1] + '__From-CoreSwitch-01-Gi' + fwPort['1'].split(
                        't')[-1] + '\n'
            else:
                fwPortInfos += ' description To-AccessSwitch-' + '-'.join(
                    fwPort['3']['sysname'].split('-')[-3:]
                ) + '-Gi' + fwPort['3']['1'].split(
                    't')[-1] + '__From-CoreSwitch-01-Gi' + fwPort['1'].split(
                        't')[-1] + '\n'
            fwPortInfos += ''' portswitch
 service-manage ping permit
 service-manage ssh permit
 service-manage snmp permit\n'''
            if fwPort['mad'] != '':
                fwPortInfos += ' mad relay\n'
            fwPortInfos += '#\n'
    firewall += '#\n'
    # print(fwPortInfos)
    # print(firewall)
    DNS = dns1 + ' ' + dns2
    if Flag78:
        vlan78Info, other = vlan78(ipPlans, DNS)
    sysname = fwFile['sysname']
    file = fwFile['fwFile']
    # print(file)
    with open(sysname + '.txt', 'w') as f:
        string = ''
        with openConfigFile.OpenConfigFile().open(
                file, mode='r', pwd='1qaz@WSX3EDc95511'.encode('utf-8')) as f1:
            for line in f1:
                line = line.decode('gbk', 'ignore')
                string += line
                if line.strip() == '#':
                    if re.search('dhcpserverdns-list',
                                 re.sub(' ', '', string.lower())):
                        string = re.sub('dns-list[\s\S]*?\n',
                                        'dns-list  ' + DNS + '\n', string)
                    if re.search('sysname[\s\S]*?', string):
                        string = re.sub('sysname[\s\S]*?\n',
                                        'sysname ' + sysname + '\n', string)
                    if re.search('unicast-server', string):
                        ntps = re.findall('unicast-server[\s\S]*?\n', string)
                        for i in range(len(ntps)):
                            string = re.sub(
                                ntps[i], 'unicast-server ' +
                                '.'.join(ipPlans[8 + i]['ipStart'].split('-')
                                         [0].split('.')[:-1]) + '.' +
                                str(
                                    int(ipPlans[8 + i]['ipStart'].split('-')
                                        [0].split('.')[-1].strip()) + 1) +
                                '\n', string)
                    if re.search('network', string):
                        networks = re.findall('network[\s\S]*?\n', string)
                        for i in range(len(networks)):
                            temp = ipPlans[
                                6 + i]['segment'] + ' ' + wildcardMark[str(
                                    int(ipPlans[6 + i]['mark']))]
                            string = re.sub(networks[i],
                                            'network ' + temp + '\n', string)
                    if re.search('interfacevlanif2',
                                 re.sub(' ', '', string.lower())):
                        # print (string)
                        temp = re.search('(ip\s*address[\s\S]*?\n)',
                                         string).groups()[0]
                        string = re.sub(
                            temp, 'ip address ' + ipPlans[0]['ipStart'] + ' ' +
                            str(int(ipPlans[0]['mark'])) + '\n', string)
                        try:
                            temp = re.search(
                                '(dhcp\s*server\s*gateway-list[\s\S]*?\n)',
                                string).groups()[0]
                            string = re.sub(
                                temp, 'dhcp server gateway-list  ' +
                                ipPlans[0]['ipStart'] + '\n', string)
                        except:
                            pass
                        temp = re.search('(server\s*ip-range[\s\S]*?\n)',
                                         string).groups()[0]
                        string = re.sub(
                            temp, 'server ip-range ' +
                            '.'.join(ipPlans[0]['ipStart'].split('.')[:-1]) +
                            '.' + str(
                                int(ipPlans[0]['ipStart'].split('.')
                                    [-1].strip()) + 1) + '  ' +
                            ipPlans[0]['ipEnd'] + '\n', string)
                    #     # print(string)
                    if re.search('interfacevlanif3',
                                 re.sub(' ', '', string.lower())):
                        # print (string)
                        temp = re.search('(ip\s*address[\s\S]*?\n)',
                                         string).groups()[0]
                        string = re.sub(
                            temp, 'ip address ' + ipPlans[1]['ipStart'] + ' ' +
                            str(int(ipPlans[1]['mark'])) + '\n', string)
                        try:
                            temp = re.search(
                                '(dhcp\s*server\s*gateway-list[\s\S]*?\n)',
                                string).groups()[0]
                            string = re.sub(
                                temp, 'dhcp server gateway-list  ' +
                                ipPlans[1]['ipStart'] + '\n', string)
                        except:
                            pass
                        temp = re.search('(server\s*ip-range[\s\S]*?\n)',
                                         string).groups()[0]
                        string = re.sub(
                            temp, 'server ip-range ' +
                            '.'.join(ipPlans[1]['ipStart'].split('.')[:-1]) +
                            '.' + str(
                                int(ipPlans[1]['ipStart'].split('.')
                                    [-1].strip()) + 1) + '  ' +
                            ipPlans[1]['ipEnd'] + '\n', string)

                    if re.search('interfacevlanif4',
                                 re.sub(' ', '',
                                        string.lower())) and not re.search(
                                            'interfacevlanif409',
                                            re.sub(' ', '', string.lower())):
                        temp = re.search('(ip\s*address[\s\S]*?\n)',
                                         string).groups()[0]
                        string = re.sub(
                            temp, 'ip address ' + ipPlans[2]['ipStart'] + ' ' +
                            str(int(ipPlans[2]['mark'])) + '\n', string)
                        temp = re.search('(server\s*ip-range[\s\S]*?\n)',
                                         string).groups()[0]
                        string = re.sub(
                            temp, 'server ip-range ' +
                            '.'.join(ipPlans[2]['ipStart'].split('.')[:-1]) +
                            '.' + str(
                                int(ipPlans[2]['ipStart'].split('.')
                                    [-1].strip()) + 1) + '  ' +
                            ipPlans[2]['ipEnd'] + '\n', string)
                        try:
                            temp = re.search(
                                '(dhcp\s*server\s*gateway-list[\s\S]*?\n)',
                                string).groups()[0]
                            string = re.sub(
                                temp, 'dhcp server gateway-list  ' +
                                ipPlans[2]['ipStart'] + '\n', string)
                        except:
                            pass
                    if re.search('interfacevlanif5',
                                 re.sub(' ', '', string.lower())):
                        temp = re.search('(ip\s*address[\s\S]*?\n)',
                                         string).groups()[0]
                        string = re.sub(
                            temp, 'ip address ' + ipPlans[3]['ipStart'] + ' ' +
                            str(int(ipPlans[3]['mark'])) + '\n', string)
                        temp = re.search('(server\s*ip-range[\s\S]*?\n)',
                                         string).groups()[0]
                        string = re.sub(
                            temp, 'server ip-range ' +
                            '.'.join(ipPlans[3]['ipStart'].split('.')[:-1]) +
                            '.' + str(
                                int(ipPlans[3]['ipStart'].split('.')
                                    [-1].strip()) + 1) + '  ' +
                            ipPlans[3]['ipEnd'] + '\n', string)
                        try:
                            temp = re.search(
                                '(dhcp\s*server\s*gateway-list[\s\S]*?\n)',
                                string).groups()[0]
                            string = re.sub(
                                temp, 'dhcp server gateway-list  ' +
                                ipPlans[3]['ipStart'] + '\n', string)
                        except:
                            pass
                    if re.search('interfacevlanif6',
                                 re.sub(' ', '', string.lower())):
                        temp = re.search('(ip\s*address[\s\S]*?\n)',
                                         string).groups()[0]
                        string = re.sub(
                            temp, 'ip address ' + ipPlans[4]['ipStart'] + ' ' +
                            str(int(ipPlans[4]['mark'])) + '\n', string)
                        temp = re.search('(server\s*ip-range[\s\S]*?\n)',
                                         string).groups()[0]
                        string = re.sub(
                            temp, 'server ip-range ' +
                            '.'.join(ipPlans[4]['ipStart'].split('.')[:-1]) +
                            '.' + str(
                                int(ipPlans[4]['ipStart'].split('.')
                                    [-1].strip()) + 1) + '  ' +
                            ipPlans[4]['ipEnd'] + '\n', string)
                        try:
                            temp = re.search(
                                '(dhcp\s*server\s*gateway-list[\s\S]*?\n)',
                                string).groups()[0]
                            string = re.sub(
                                temp, 'dhcp server gateway-list  ' +
                                ipPlans[4]['ipStart'] + '\n', string)
                        except:
                            pass
                    if re.search('interfacevlanif4094',
                                 re.sub(' ', '', string.lower())):
                        try:
                            temp = re.search(
                                '(dhcp\s*server\s*gateway-list[\s\S]*?\n)',
                                string).groups()[0]
                            string = re.sub(
                                temp, 'dhcp server gateway-list  ' +
                                ipPlans[5]['ipStart'] + '\n', string)
                        except:
                            pass
                        temp = re.search('(ip\s*address[\s\S]*?\n)',
                                         string).groups()[0]
                        string = re.sub(
                            temp, 'ip address ' +
                            '.'.join(ipPlans[5]['ipStart'].split('.')[:-1]) +
                            '.' + str(
                                int(ipPlans[5]['ipStart'].split('.')
                                    [-1].strip())) + ' ' +
                            str(int(ipPlans[5]['mark'])) + '\n', string)
                        temp = re.findall('dhcp\s*server\s*ip-range[\s\S]*?\n',
                                          string)
                        string = re.sub(
                            temp[0], 'dhcp server ip-range ' +
                            '.'.join(ipPlans[5]['ipStart'].split('.')[:-1]) +
                            '.' + str(
                                int(ipPlans[5]['ipStart'].split('.')[-1].strip(
                                )) + 1 + int(ipPlans[5]['reservedNumber'])) +
                            '  ' + ipPlans[5]['ipEnd'] + '\n', string)
                        # print(string)
                        temp = re.search('(dhcp\s*server\s*option[\s\S]*?\n)',
                                         string).groups()[0]
                        string = re.sub(temp, option + '\n', string)
                    if re.search('interfacegigabitethernet0/0/1',
                                 re.sub(' ', '',
                                        string.lower())) and re.search(
                                            'address', string.lower()):
                        temp = re.search('(ip\s*address[\s\S]*?\n)',
                                         string).groups()[0]
                        string = re.sub(
                            temp, 'ip address ' +
                            ipPlans[6]['ipStart'].split('-')[0] + ' ' +
                            str(int(ipPlans[9]['mark'])) + '\n', string)
                    #     # print(string)
                    if re.search('interfacegigabitethernet0/0/2',
                                 re.sub(' ', '',
                                        string.lower())) and re.search(
                                            'address', string.lower()):
                        temp = re.search('(ip\s*address[\s\S]*?\n)',
                                         string).groups()[0]
                        string = re.sub(
                            temp, 'ip address ' + ipPlans[7]['ipStart'] + ' ' +
                            str(int(ipPlans[7]['mark'])) + '\n', string)
                    if re.search('接口区域', string):
                        string = '————————————————————————\n'
                        string += '接口区域划分:\n\n'
                        string += firewall
                    if re.search('接口配置', string):
                        string += fwPortInfos
                    if re.search('//[\s\S]*?\n', string) and not re.search(
                            '默认账号和密码查询网站', string) and not re.search(
                                '用户配置', string):
                        string = re.sub('//[\s\S]*?\n', '\n', string)
                    f.write(re.sub('\r', '', string))
                    string = ''

        if string.strip() != '' and string.strip()[-1] != '#':
            if re.search('//[\s\S]*?\n', string) and not re.search(
                    '默认账号和密码查询网站', string) and not re.search('用户配置', string):
                string = re.sub('//[\s\S]*?\n', '\n', string)
            f.write(re.sub('\r', '', string))
        if Flag78:
            f.write(re.sub('\r', '', '\n' + vlan78Info))
Exemple #4
0
def dealAcs(acFiles, acportConnects, ipPlans, devices, swPortConnect,
            fwPortConnect):
    # for port in acportConnects:
    #     print(port)
    # len(acFiles)
    for i in range(len(acFiles)):
        acFile = acFiles[i]
        acportConnect = acportConnects[i]
        sysname = acFile['sysname']
        deviceClass = acFile['deviceClass']
        # print(deviceClass)
        stackNumber = '不堆叠'
        if '不堆叠' in str(devices[i]['stackNumber']):
            stackFlag = False
        else:
            stackFlag = True
            stackNumber = int(devices[i]['stackNumber'])
        if stackFlag and stackNumber > 3:
            stackInfo = stackConfig(stackNumber, sysname, deviceClass)
        file = acFile['acFile']
        manageIp = acFile['manageIp']
        portInfos = acPorts(acportConnect, manageIp, stackFlag, stackNumber,
                            swPortConnect, fwPortConnect, acportConnects)
        # print(sysname, file, manageIp)
        with open(sysname + '.txt', 'w') as f1:
            with openConfigFile.OpenConfigFile().open(
                    file, mode='r',
                    pwd='1qaz@WSX3EDc95511'.encode('utf-8')) as f:
                string = ''
                for line in f:
                    try:
                        line = line.decode('gbk')
                    except:
                        try:
                            line = line.decode('utf8')
                        except:
                            dicts = chardet.detect(line)
                            line = line.decode(dicts["encoding"])

                    string += line
                    if line.strip() == '#':
                        if re.search('sysname[\s\S]*?\n', string):
                            string = re.sub('sysname[\s\S]*?\n',
                                            'sysname ' + sysname + '\n',
                                            string)
                        if re.search('interfacevlanif4094',
                                     re.sub(' ', '', string.lower())):
                            # print(manageIp)
                            string = re.sub(
                                'ip\s*address[\s\S]*?\n',
                                'ip address ' + manageIp + ' ' +
                                str(ipPlans[5]['mark']) + '\n', string)
                        if re.search('unicast-server', string):
                            string = re.sub(
                                'ntp-service\s*unicast-server[\s\S]*?\n',
                                'ntp-service unicast-server ' +
                                ipPlans[5]['ipStart'] + '\n', string)
                        if re.search('route-static', string):
                            string = re.sub(
                                'ip\s*route-static\s*[\s\S]*?\n',
                                'ip route-static  0.0.0.0 0.0.0.0 ' +
                                ipPlans[5]['ipStart'] +
                                ' description To-CoreSwitch-MGMT\n', string)
                        if re.search('//[\s\S]*?\n', string) and not re.search(
                                '默认账号和密码查询网站', string) and not re.search(
                                    '用户配置', string):
                            string = re.sub('//[\s\S]*?\n', '\n', string)
                        f1.write(re.sub('\r', '', string))
                        string = ''

                if string.strip() != '' and string.strip()[-1] != '#':
                    if re.search('//[\s\S]*?\n', string) and not re.search(
                            '默认账号和密码查询网站', string) and not re.search(
                                '用户配置', string):
                        string = re.sub('//[\s\S]*?\n', '\n', string)
                    f1.write(re.sub('\r', '', string))

        with open(sysname + '.txt', 'r') as f:
            info = f.read()
            # print(info)
            info = re.sub('接口配置[\s\S]*?——————', '接口配置:\n\n#\n——————', info)
            if stackFlag and stackNumber > 3:
                info = re.sub('堆叠配置[\s\S]*?——————', '堆叠配置:\n\n#\n——————', info)

        with open(sysname + '.txt', 'w', encoding='utf8') as f:
            string = ''
            for line in info.split('\n'):
                string += line + '\n'
                if line.strip() == '#':
                    if re.search('接口配置', string):
                        string = portInfos
                    if re.search('堆叠配置',
                                 string) and stackFlag and stackNumber > 3:
                        string += stackInfo
                    f.write(re.sub('\r', '', string))
                    string = ''
            if string.strip() != '' and string.strip()[-1] == '#':
                f.write(re.sub('\r', '', string))