Python2.7 automatically creates virtual machines and python2.7 virtual machines
Python2.7 automatically creates one or more virtual machines (the IP address is the virtual machine name)
[1].
[Code][Python] Code jump to [1]?
| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889 |
# -*- coding: cp936 -*-# Jk409 script for copying files and modifying virtual machine configuration files in batches written in python2.7 fromimport os,os.path,shutil import re,time class 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.nameself.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)defcopy_file(self):if(os.path.exists(self.dst_dir)==False): os.makedirs(self.dst_dir)print'Start creating a VM. 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')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')exceptException as err: print(err) else:printself.dst_dir,'is Exists !'defmode_file(self):#self.dst_file.replace('93.101',self.name)f=file(self.src_file+'.vmx','r')f1=open(self.dst_file+'.vmx','w')forf2 in f.readlines(): f1.write(f2.replace(self.src_name,self.name))#print(f2.replace('93.101',self.name))f1.close() f.close()printself.name,'The VM has been created successfully! 'while1: print''' 1. Create a single Virtual Machine2. Create multiple VMS3. quit (quit)'''chioce=raw_input("Your choice [1/2/3]:")ifchioce =='3' or chioce =='quit': exit(0)ifchioce =='': print'Input error. Please enter another ......'time.sleep(2)continue;sysos=raw_input('Select the system you want to follow: [ipvs2003/centos6]:')ifchioce =='1': while1: ip=raw_input('Enter the Virtual Machine name :')name= ip ifip =='quit': break;i=int(ip.split('.')[1])ifi%2== 1: drive='E';ifi%2== 0: drive='F';main(name,drive,sysos).copy_file()main(name,drive,sysos).mode_file()ifchioce =='2': ip=raw_input('Enter the second segment of the starting IP address and the last segment of the ending IP Address [*. *-*]:')ip00=ip.split('-')[0]ip01=ip00.split('.')[0]ip02=ip00.split('.')[1]printip02 ip03=ip.split('-')[1]fori in range(int(ip02),int(ip03),1):ifi%2== 1: name=ip01+'.'+str(i)drive='E'ifi%2== 0: name=ip01+'.'+str(i)drive='F'printname main(name,drive,sysos).copy_file()main(name,drive,sysos).mode_file() |