Python2.7 Automatic Virtual Machine Creation

Source: Internet
Author: User
Python2.7 automatically creates a virtual machine. One or more virtual machines (the IP address is the virtual machine name) [1]. [Code] [Python] Code jump to [1] #-*-coding: cp936-*-# jk409 on, the script importos and OS used to copy files in batches and modify virtual machine configuration files was written in python2.7. path, shutilimportre, timeclassmain: def _ I

Python2.7 automatically creates a virtual machine. One or more virtual machines (the IP address is the virtual machine name) [1]. [Code] [Python] Code jump to [1] #-*-coding: cp936-*-# jk409 on, the script for copying files and modifying virtual machine configuration files in batches written in python2.7 was used to import OS, OS. path, shutilimport re, timeclass main: def _ I

Python2.7 automatically creates one or more virtual machines (the IP address is the virtual machine name)

[1]. [Code][Python] Code jump to [1]
#-*-Coding: cp936-*-# jk409 on February 27, import OS, OS. path, shutilimport re, timeclass 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. pa Th. dirname (self. dst_file) def copy_file (self): if (OS. path. exists (self. dst_dir) = False): OS. makedirs (self. dst_dir) print 'to 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 ') Does T 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, 'vm created successfully! 'While 1: print ''' 1. create a single Virtual Machine 2. create multiple VMS 3. exit (quit) ''' chioce = raw_input ("your choice [1/2/3]:") if chioce = '3' or chioce = 'quit ': exit (0) if chioce = '': print 'is incorrect. Please enter another one ...... '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 Start IP address and the last segment of the end 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 ()

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.