09python生成文件脚本

来源:互联网 发布:idea 测试java代码 编辑:程序博客网 时间:2024/06/06 16:03
import stringimport randomimport os#*名称 起始地址 结束地址 防回环路由 地址分配 免费ARP# 电信1 1.1.1.1     1.1.1.2         1  5*1.1.1.1*1.1.1.2 0#以“#”开头的行是注释行   #地址池导入导出配置文件   #各列说明:   #名称支持中文,长度最长支持63   #结束地址必须不小于起始地址   #防回环路由:取值为1或者0 1表示启用,0表示禁用  #免费ARP:取值为1或者0 1表示启用,0表示禁用  #地址分配:框式设备上才有配置,盒式设备上没有  templateList = ['名称', '起始地址', '结束地址', '防回环路由', '地址分配', '虚拟系统', '免费ARP']dic = {}#地址池名称,最大支持63个字符#nameList = ['我是地址池', 'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','1','2','3','4','5','6','7']name1 = ("abcdefghijklmnopqrstyvwxyz")nameList = list(name1)addpoolNum = 100 #设置产生多少条地址池#startIP = '1.1.1.' #设置起始ip,末位不用设置,自动生成#endIP = '101.1.1.' #设置结束ip,末位不用设置,自动生成loopProt = '1'  #防回环路由,0,无|1:有slot = '4'   #槽位,暂不支持多板卡addAllocation = '0' #地址分配,不用设置gratisARP = '0'  #免费arp,0:关闭|1:启用vos = 'PublicSystem'index = 0tmp = 'tmp.csv'print('input file name(.csv):')fname = input()while fname == "": print("ERROR!") breakwhile True: if os.path.exists(fname):  print("error:the file name '%s' already exists" % fname) else:  breakfor ruleNum in range(1,addpoolNum): addpoolName = ''.join(random.sample(nameList,10)) #名字  last = random.randint(1,100)  #起始ip末位 endLast = 100+random.randint(1,100) #结束ip末位 startIP = '101.1.%s.' % ruleNum #设置起始ip,末位不用设置,自动生成 endIP = '101.1.%s.' % ruleNum #设置结束ip,末位不用设置,自动生成 startIP = startIP + '%s' % last  #起始ip endIP = endIP + '%s' % endLast  #结束ip addAllocation = slot + '*' + startIP  + '*' + endIP #分配ip #fill data listattr = [addpoolName, startIP, endIP, loopProt, addAllocation, vos, gratisARP] index += 1 dic[index] = listattrfobj = open(fname, 'w')for i in templateList: if i == '免费ARP':  fobj.write(i) else:  fobj.write(i+',')fobj.write('\n')for key in dic.keys(): for item in dic[key]:  if item == '0':   fobj.write(item)  else:   fobj.write(item+',') fobj.write('\n')fobj.close()print('SUCCESS!')print('Press Enter')input() 
0 0
原创粉丝点击