python2.7自动创建虚拟机
程序员文章站
2022-06-06 08:58:25
...
python2.7自动创建虚拟机,单台、多台(IP为虚拟机名) [1]. [代码] [Python]代码 跳至 [1] # -*- coding: cp936 -*-#jk409 于2014-8-27用python2.7编写的批量复制文件和修改虚拟机配置文件的脚本import os,os.path,shutilimport re,timeclass main: def __i
python2.7自动创建虚拟机,单台、多台(IP为虚拟机名)
[1].[代码] [Python]代码 跳至 [1]
# -*- 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()