1 # -*- coding:utf-8 -*-
2 import os
3 import optparse
4 import sys
5 import re
6
7
8 def createfile(path, dirname):
9 allfile = os.listdir(path)
10 os.chdir(path)
11 if dirname not in allfile:
12 os.mkdir(dirname)
13 return dirname
14
15
16 def createsessionfile(hostname, path, templatefilename, filename, dirname):
17 os.chdir(path)
18 sep = os.sep
19 hostid = re.compile(r'(Hostname).*')
20 reip = re.compile(r'\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b')
21 filepath = dirname + sep + filename + '.ini'
22 dirnamefiles = os.listdir(dirname)
23 if filename not in dirnamefiles:
24 with open(templatefilename, "r") as fp:
25 filecontent = fp.read()
26 hostip = hostid.search(filecontent).group()
27 ip = reip.search(hostip).group()
28 newfilecontent = filecontent.replace(ip, hostname)
29 wf = open(filepath, 'w')
30 wf.write(newfilecontent)
31 wf.close()
32
33
34 def createiplist(ip1, ip2):
35 ip1last = int(ip1.split('.')[-1])
36 ip2last = int(ip2.split('.')[-1])
37 ipbefore = '.'.join(ip1.split('.')[:-1])
38 iplist = []
39 if ip1last <= ip2last:
40 for iplast in range(ip1last, ip2last+1):
41 ip = ipbefore + '.' + str(iplast)
42 iplist.append(ip)
43 return iplist
44
45
46 def ipgroup(iplist, number, n):
47 ipgroupdict = {}
48 count = len(iplist) + 1
49 group = count / number
50 for i in range(group):
51 base = i * number
52 tmpip = []
53 for index in range(number):
54 tmpip.append(iplist[index + base])
55 groupname = "s" + str(n)
56 n += 1
57 ipgroupdict[groupname] = tmpip
58 return ipgroupdict
59
60
61 def createsessionname(mp, reapp, **ipdict):
62
63 for k, v in ipdict.iteritems():
64 tmplist = []
65 for index in range(len(v)):
66 iplast = '.'.join(v[index].split('.')[-2:])
67 if index == 0:
68 tmplist.append((v[index],iplast+"-MC"))
69 elif index > 0 and index < mp:
70 tmplist.append((v[index],iplast+"-MA"))
71 elif index >= len(v) - reapp:
72 tmplist.append((v[index],iplast+"RePE"))
73 else:
74 tmplist.append((v[index],iplast+"PE"))
75 ipdict[k] = tmplist
76 return ipdict
77
78
79 def base_pare():
80 usage = 'Usage: %prog [options] arg1 arg2 ...'
81 pares = optparse.OptionParser(usage, version='%prog 1.0')
82 pares.add_option("-a", '--ip1', dest='ip1', help='The upper bound of the IP address range')
83 pares.add_option('-b', '--ip2', dest='ip2', help='The lower bound of the IP address range')
84 pares.add_option('-f', '--file', dest='templatefilename', metavar='FILE',help='write output to FILE')
85 pares.add_option('-p', '--path', dest='path', help='Generate file path')
86 pares.add_option('-g', '--group', dest='group', help='Directory name')
87 pares.add_option('-n', '--number', dest='number', help='The number of each group')
88 pares.add_option('-m', '--mp', dest='mp', help='Mp the number of')
89 pares.add_option('-r', '--reprice', dest='reprice', help='The number of reprice')
90 return pares
91
92
93 if __name__ == '__main__':
94 pares = base_pare()
95 options, args = pares.parse_args()
96 if len(sys.argv) <= 1:
97 print "usage:autosession.py -h"
98 exit()
99 ip1 = options.ip1
100 ip2 = options.ip2
101 templatefilename = options.templatefilenam