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()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值