首页 > 代码库 > python2.7自动创建虚拟机
python2.7自动创建虚拟机
python2.7自动创建虚拟机,单台、多台(IP为虚拟机名)
[1].[代码] [Python]代码跳至 [1]
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 | # -*- coding: cp936 -*- #jk409 于2014-8-27用python2.7编写的批量复制文件和修改虚拟机配置文件的脚本 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 ‘开始创建虚拟机,请稍等......‘
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, ‘虚拟机已创建成功!‘
while 1 :
print ‘‘‘
1.创建单个虚拟机
2.创建多个虚拟机
3.退出(quit)
‘‘‘
chioce = raw_input ( "您的选择[1/2/3]:" )
if chioce = = ‘3‘ or chioce = = ‘quit‘ :
exit( 0 )
if chioce = = ‘‘:
print ‘输入错误,请重新输入......‘
time.sleep( 2 )
continue ;
sysos = raw_input ( ‘请选择你要按照的系统:[windos2003/centos6]:‘ )
if chioce = = ‘1‘ :
while 1 :
ip = raw_input ( ‘请输入虚拟机名称:‘ )
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 ( ‘请输入开始IP地址的后2段以及结束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自动创建虚拟机
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。