The CRT automatically creates a session, and the CRT creates a session.

Source: Internet
Author: User

The CRT automatically creates a session, and the CRT creates a session.
Because there are too many Management Servers at work, you need to log on to multiple servers through ssh. It was a headache when I started to create a CRT to log on to the session, so I wrote a small script. I hope you can give me more instructions.

  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.ip1100     ip2 = options.ip2101     templatefilename = options.templatefilename102     path = options.path103     group = options.group104     number = options.number105     mp = options.mp106     reprice = options.reprice107     iplist = createiplist(ip1, ip2)108     ipgroup = ipgroup(iplist, int(number), int(group))109     ipdict = createsessionname(int(mp), int(reprice), **ipgroup)110     for groupname, iplists in ipdict.iteritems():111         dirname = createfile(path, groupname)112         for iptule in iplists:113             createsessionfile(iptule[0], path, templatefilename, iptule[1], dirname)
View Code
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.