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 from import 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 .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 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' )
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, 'The VM has been created successfully! '
while 1 :
print '''
1. Create a single Virtual Machine
2. Create multiple VMS
3. quit (quit)
'''
chioce = raw_input ( "Your choice [1/2/3]:" )
if chioce = = '3' or chioce = = 'quit' :
exit( 0 )
if chioce = = '':
print 'Input error. Please enter another ......'
time.sleep( 2 )
continue ;
sysos = raw_input ( 'Select the system you want to follow: [ipvs2003/centos6]:' )
if chioce = = '1' :
while 1 :
ip = raw_input ( 'Enter the 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 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 ]
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() |