python2.7 batch creation of virtual machines

Source: Internet
Author: User

# -*- coding: cp936 -*-#jk409   Script for bulk copying files and modifying virtual machine configuration files python2.7 in 2014-8-27 import  Os,os.path,shutilimport re,timeclass main:    def __init__ (Self,name,drive, Sysos):         self.name     = name         self.drive    = drive         self.dst_dir  = self.drive+ ': \ \ ' +self.name+ ' \ \ '          self.dst_file = self.dst_dir+self.name         self.src_name = sysos         self.src_dir  =  '. \ \ ' +self.src_name+ ' \ \ '          self.src_file  = self.src_dir+self.src_name        # Self.dir=os.path.dirname (Self.dst_file)            def copy_file (self):         if  (os.path.exists (self.dst_dir)  == false):             os.makedirs (Self.dst_dir)              print  ' Start creating virtual machines, please wait ... '              try:                  #shutil. Copytree (Self.src_dir,self.dst_dir)                  shutil.copyfile (self.src_file+ ' -0.vmdk ', Self.dst_file+ ' -0.VMDK ')                  shutil.copyfile (self.src_file+ '. vmdk ', self.dst_file+ '. vmdk ')             &Nbsp;    shutil.copyfile (self.src_file+ '. Vmxf ', self.dst_file+ '. VMXF ')                  shutil.copyfile (Self.src_file+ '. Vmsd ', self.dst_file+ '. Vmsd ')                  shutil.copyfile (self.src_file+ '. Nvram ', self.dst_file+ '. Nvram ')              except Exception as err:                 print  (ERR)           else:             print self.dst_dir, ' is exists ! '     def mode_file (self):         #self. Dst_ File.replace (' 93.101 ', self.name)         f=file (SELf.src_file+ '. Vmx ', ' R ')         f1=open (self.dst_file+ '. Vmx ', ' W ')          for f2 in f.readlines ():             f1.write (F2.replace (self.src_name,self.name))               #print (f2.replace (' 93.101 ', self.name)          f1.close ()         f.close ()          print   self.name, ' virtual machine has been created successfully! '         while 1:    print  '              1. Creating a single virtual machine               2. Creating multiple virtual machines              3. Quit (quit)     &Nbsp;    '     chioce=raw_input ("Your choice [1/2/3]:")     if  chioce ==  ' 3 '  or chioce ==  ' Quit ':         exit (0)     if chioce ==  ':         print  ' input error, please re-enter ... '         time.sleep (2)          continue;             sysos=raw_input (' Please select the system you want to follow: [WINDOS2003/CENTOS6]: ')     if chioce  ==  ' 1 ':        while 1:             ip=raw_input (' Please enter virtual machine name: ')              name = ip             if ip ==  ' Quit ':  break;            i=int ( Ip.split ('. ') [1])             if i%2  == 1:  drive= ' E ';            if i%2   == 0: drive= ' F ';             main ( Name,drive,sysos). Copy_file ()             main (name , Drive,sysos). Mode_file ()                  if chioce ==  ' 2 ':         ip=raw_input (' Enter the last 2 paragraphs of the start IP address and the end IP [*.*-*]: ')         ip00=ip.split ('-') [0]         ip01=ip00.split ('. ') [0]        ip02=ip00.split(‘.‘) [1]        print ip02         ip03=ip.split ('-') [1]        for i in range (Int ( IP02), int (IP03), 1):            if i%2   == 1:                 name=ip01+ '. ' +str (i)                 drive= ' E '                              if i%2  == 0:                 name=ip01+ '. ' +str (i)                 drive= ' F '             print name             main (Name,drive,sysos). Copy_file ()              main (Name,drive,sysos). Mode_file ()


python2.7 batch creation of virtual machines

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.