配置文件生成脚本

Zss 发表于:

有的时候需要生成一些有规律的配置文件,譬如接口,等一些配置

在web中一条条添加太慢,所以直接生产一个配置文件来替换就很快的添加需要的配置

#coding:utf-8
import os

def application():
    print '--------------------------------------------------------------------'
    print '            生成应用配置文件,记得备份旧的配置,再上传新的配置'
    print '              V8: PREOBJS文件位置:/var/self/rules/precfg'
    print '              V6:PREOBJS文件位置:/initrd/var/www/rules/precfg'
    print '--------------------------------------------------------------------'
    agreement = raw_input('   1.输入协议,输入:tcp或者udp\n')
    port_range = raw_input('   2.输入端口范围,输入:1-66生成1到66端口,输入0进入自定义批量添加端口\n')
    agreement = agreement.upper()
    if os.path.exists('PREAPPS'):
        os.remove('PREAPPS')
    index = 0
    if port_range == '0':
        port_list = []
        while 1:
            port = raw_input('                       请输入端口,输入0结束添加')
            if port == '0':
                break
            port_list.append(port)

        with open('PREAPPS', 'a') as f:
            f.write('[MAIN]\n')
            f.write('AppNum="%d"\n\n' % len(port_list))
        for i in port_list:
            with open('PREAPPS', 'a') as f:
                f.write('[APP%d]\n' % index)
                f.write('Name="Port%d"\n' % (int(i)))
                f.write('Protocol="%s"\n' % agreement)
                f.write('SPort="1-65535"\n')
                f.write('DPort="%d"\n' % (int(i)))
                f.write('TPort="%d"\n' % (int(i)))
                f.write('Action="1"\n')
                f.write('AsService="NULL_%s"\n' % agreement)
                f.write('Description=""\n')
                f.write('CKLog="0"\n')
                f.write('CmdNum="0"\n\n')
                index += 1

    else:
        start_number, end_number = int(port_range.split('-')[0]), int(port_range.split('-')[1])
        with open('PREAPPS', 'a') as f:
            f.write('[MAIN]\n')
            f.write('AppNum="%d"\n\n' % len(range(start_number, end_number + 1)))
        for i in range(start_number, end_number + 1):
            with open('PREAPPS', 'a') as f:
                f.write('[APP%d]\n' % index)
                f.write('Name="Port%d"\n' % (i))
                f.write('Protocol="%s"\n' % agreement)
                f.write('SPort="1-65535"\n')
                f.write('DPort="%d"\n' % (i))
                f.write('TPort="%d"\n' % (i))
                f.write('Action="1"\n')
                f.write('AsService="NULL_%s"\n' % agreement)
                f.write('Description=""\n')
                f.write('CKLog="0"\n')
                f.write('CmdNum="0"\n\n')
                index += 1
    print '--------------------------------------------------------------------'
    raw_input('                    文件已生成!在当前文件夹')
    print '--------------------------------------------------------------------'

def obj():
    info_list = []
    ip_list = []
    print '--------------------------------------------------------------------'
    print '            生成对象配置文件,记得备份旧的配置,再上传新的配置'
    print '              V8: PREOBJS文件位置:/var/self/rules/precfg'
    print '              V6:PREOBJS文件位置:/initrd/var/www/rules/precfg'
    print '--------------------------------------------------------------------'
    while 1:
        ip = raw_input('           请输入ip地址范围 如:192.168.111.12-224 输入0结束添加\n')
        if ip == '0':
            break
        else:
            mask = raw_input('           输入ip%s 的子网掩码 如:255.255.255.0\n' % ip.split('-')[0])
            ip_list.append((ip, mask))



    def test(ip,mask):
        for i in range(int(ip.split('-')[0].split('.')[-1]), int(ip.split('-')[1]) + 1):
            info_list.append((ip.split('-')[0].split('.')[0] + '.' + ip.split('-')[0].split('.')[1] + '.' + ip.split('-')[0].split('.')[2] + '.' + str(i),mask))

    for t in ip_list:
        test(t[0],t[1])


    if os.path.exists('PREOBJS'):
        os.remove('PREOBJS')

    for i in range(0,len(info_list)):
        with open('PREOBJS', 'a') as f:
            f.write('[OBJS%d]\n' % i)
            f.write('Name="%s"\n' % info_list[i][0])
            f.write('IP="%s"\n' % info_list[i][0])
            f.write('Mask="%s"\n' % info_list[i][1])
            f.write('MAC="00:00:00:00:00:00"\n')
            f.write('Description=""\n\n')
    with open('PREOBJS', 'a') as f:
        f.write('[MAIN]\n')
        f.write('ObjsNum="%d"\n\n' % len(info_list))

def devconfig():
    info_list = []
    ip_list = []
    print '--------------------------------------------------------------------'
    print '            生成对象配置文件,记得备份旧的配置,再上传新的配置'
    print '               V8:devconfig.cf文件位置:/var/self/rules/conf'
    print '              V6:devconfig.cf文件位置:/initrd/var/www/rules/conf'
    print '--------------------------------------------------------------------'
    direction = raw_input('           配置内外接口,INT输入:1,EXT输入:2\n')
    dev = raw_input('             配置哪个接口的ip,EXT0或者INT0输入0,EXT3或者INT3输入3\n')
    while 1:
        ip = raw_input('           请输入ip地址范围 如:192.168.111.12-224 输入0结束添加\n')
        if ip == '0':
            break
        else:
            mask = raw_input('           输入ip%s 的子网掩码 如:255.255.255.0\n' % ip.split('-')[0])
            ip_list.append((ip, mask))
    def test(ip,mask):
        for i in range(int(ip.split('-')[0].split('.')[-1]), int(ip.split('-')[1]) + 1):
            info_list.append((ip.split('-')[0].split('.')[0] + '.' + ip.split('-')[0].split('.')[1] + '.' + ip.split('-')[0].split('.')[2] + '.' + str(i),mask))
    for t in ip_list:
        test(t[0],t[1])
    if os.path.exists('devconfig.cf'):
        os.remove('devconfig.cf')
    if direction == '1':
        int_number = len(info_list)
        ext_number = 0
    else:
        ext_number = len(info_list)
        int_number = 0
    with open('devconfig.cf','a') as f:
        f.write('[DEV]\n')
        f.write('OutDIPNum=0\n')
        f.write('InDIPNum=0\n')
        f.write('OUTNET_RTNum=0\n')
        f.write('OUTNET_IPNum=%d\n'%ext_number)
        f.write('INNET_RTNum=0\n')
        f.write('INNET_IPNum=%d\n\n'%int_number)
        if direction == '1':
            f.write('[INNET]\n')
            f.write('CKPing=1\n')
            f.write('CKHideSrcIP=1\n')
            f.write('DefGW=\n')
            f.write('DefDNS=\n')
            f.write('CKSNMP=0\n')
            f.write('SNMPCtrIP=\n')
            f.write('SNMPComm=public\n')
            for i in range(0,len(info_list)):
                f.write('ID%d=%d\n'%(i,int(dev)+3))
                f.write('MYIP%d=%s\n'%(i,info_list[i][0]))
                f.write('MYMASK%d=%s\n'%(i,info_list[i][1]))
            f.write('\n[OUTNET]\n')
            f.write('CKPing=1\n')
            f.write('CKHideSrcIP=1\n')
            f.write('DefGW=\n')
            f.write('DefDNS=\n')
            f.write('CKSNMP=0\n')
            f.write('SNMPCtrIP=\n')
            f.write('SNMPComm=public\n')
        else:
            f.write('[OUTNET]\n')
            f.write('CKPing=1\n')
            f.write('CKHideSrcIP=1\n')
            f.write('DefGW=\n')
            f.write('DefDNS=\n')
            f.write('CKSNMP=0\n')
            f.write('SNMPCtrIP=\n')
            f.write('SNMPComm=public\n')
            for i in range(0,len(info_list)):
                f.write('ID%d=%d\n'%(i,int(dev)+3))
                f.write('MYIP%d=%s\n'%(i,info_list[i][0]))
                f.write('MYMASK%d=%s\n'%(i,info_list[i][1]))
            f.write('\n[INNET]\n')
            f.write('CKPing=1\n')
            f.write('CKHideSrcIP=1\n')
            f.write('DefGW=\n')
            f.write('DefDNS=\n')
            f.write('CKSNMP=0\n')
            f.write('SNMPCtrIP=\n')
            f.write('SNMPComm=public\n')

def bonding():
    info_list = []
    ip_list = []
    print '--------------------------------------------------------------------'
    print '            生成对象配置文件,记得备份旧的配置,再上传新的配置'
    print '               V8:bonding.cf文件位置:/var/self/rules/conf'
    print '              V6:dbonding.cf文件位置:/initrd/var/www/rules/conf'
    print '--------------------------------------------------------------------'
    direction = raw_input('           配置内外接口,INT输入:1,EXT输入:2\n')
    while 1:
        ip = raw_input('           请输入ip地址范围 如:192.168.111.12-224 输入0结束添加\n')
        if ip == '0':
            break
        else:
            mask = raw_input('           输入ip%s 的子网掩码 如:255.255.255.0\n' % ip.split('-')[0])
            ip_list.append((ip, mask))

    def test(ip, mask):
        for i in range(int(ip.split('-')[0].split('.')[-1]), int(ip.split('-')[1]) + 1):
            info_list.append((ip.split('-')[0].split('.')[0] + '.' + ip.split('-')[0].split('.')[1] + '.' +
                              ip.split('-')[0].split('.')[2] + '.' + str(i), mask))

    for t in ip_list:
        test(t[0], t[1])
    if os.path.exists('bonding.cf'):
        os.remove('bonding.cf')
    if direction == '1':
        int_number = len(info_list)
        ext_number = 0
    else:
        ext_number = len(info_list)
        int_number = 0
    with open('bonding.cf','a') as f:
        f.write('[INNET]\n')
        f.write('Bond=1\n')
        f.write('IPNum=%d\n'%int_number)
        f.write('BondType=4\n')
        f.write('DevNum=2\n')
        f.write('Dev0=6\n')
        f.write('Dev1=7\n')
        for i in range(0, len(info_list)):
            f.write('IPAddr%d=%s\n' % (i, info_list[i][0]))
            f.write('MaskAddr%d=%s\n' % (i, info_list[i][1]))
        f.write('\n')
        f.write('[OUTNET]\n')
        f.write('Bond=0')



if __name__ == '__main__':
    print '--------------------------------------------------------------------'
    print '                         配置文件生成脚本'
    print '                      选择需要生成哪个配置文件'
    print '                      1.对象'
    print '                      2.应用'
    print '                      3.接口IP'
    print '--------------------------------------------------------------------'
    choice = raw_input('输入选择:\n')
    if choice == '1':
        obj()
    elif choice =='2':
        application()
    elif choice == '3':
        devconfig()
    elif choice == '4':
        bonding()
    else:pass
    raw_input('生成完成!')