VMware Batch Auto-delete virtual machine script, can set whitelist

Source: Internet
Author: User

Recently, because of the need to re-build the disaster recovery environment, before the deployment of nearly 380 virtual machines, 85% of which needs to be recycled, some virtual machines need to be retained (very annoying ah ... ), the business unit provides the IP of the virtual machine that needs to be retained, and then retains about 60 or so (it's annoying), manual recycling ... this is not my style, come on, the script is Restless (https://mianbao.cn.com/thread-358-1-1.html):

import osimport timeimport pprintfrom pysphere import viproperty from  pysphere.vi_task import vitaskfrom connect import vcenter_connection as  Confrom pysphere.resources import vimservice_services as vifrom pysphere.vi_ Virtual_machine import vivirtualmachines = con () Class vm_del ():         def __init__ (self,s):         self._ _s = s        self.__vm = none         self.__ip_path = os.path.join (Os.path.dirname (__file__), ' Ip.txt ')             def getvminfo (self):         prope = [' guest.ipaddress ', ' name ', ' Summary.runtime.powerState ',]          dict_key = {' guest.ipaddress ': ' IP ', ' name ': ' Name ', ' summary.runtime.powerState ': ' Power '}        props = s._retrieve_properties_traversal ( Property_names=prope, obj_type= ' Virtualmachine ')                  vm = {}        one _vm = dict ()                  for item in props:             for p in item. Propset:                one_vm [Dict_key.get (P.name)] = p.val             vm[one_vm.get (' name ')] = {' IP ': one_vm.get (' IP '), ' power ': one_vm.get (' power ')} &nbSp;      print vm        return  vm            def getipwhitelist (self):         ip_list = list ()          with open (Self.__ip_path)  as file:             all_ip_white_list = file.readlines ()              [ip_list.append (X.strip ())  for x in all_ip_white_list]             return ip_list         DEF POWERDOWNVM (SELF,VM):         Self.__vm = self.__s.get_vm_by_name (VM)         self.__ Vm.power_off ()   &NBsp;         def done (self):         white_list = self. Getipwhitelist ()         all_vm = self. Getvminfo ()         for x,y in all_vm.items ():             if y.get (' IP ', None)  in white_ List:                pass             elif  ' Wg_ '  in x:                 print x                 if y.get (' Power ')  ==  ' Poweredon ':              &nbsP;      self. POWERDOWNVM (x)                      print  '  |--->poeroff ..... OK '                      time.sleep (5)                      print  '  |---->del.....start '                      self. DELVM ()                      print  '  |----->del .... OK '                      time.sleep (Ten)                 else:                     print  '%s power status is %s '  %  (X, y.get (' power '))         print white_list             DEF DELVM (self):         request = vi. Destroy_taskrequestmsg ()          _this = request.new__ This (Self.__vm._mor)          _this.set_attribute_type (self.__vm._ Mor.get_attribute_type ())          request.set_element__this (_this )         ret = s._proxy. Destroy_task (Request) ._returnval                          #Wait  for the task to  finish        task = vitask (ret, s)                   status =  Task.wait_for_state ([Task. State_success, task. State_error])          if status == task. state_success:            print  " |----- ->vm successfully deleted from disk "          elif status == task. state_error:             print  "ERROR  REMOVING VM: ",  task.get_error_message ()               if __name__ ==&nbsP; " __main__ ":     vm_del = vm_del (s)     vm_del. Done ()

The experimental script, very coarse, will then be optimized, the effect is as follows, the script output:
650) this.width=650; "id=" aimg_728 "src=" https://mianbao.cn.com/data/attachment/forum/201704/17/ 135507vpj8g8gt1gwtjjcp.png "class=" Zoom "width=" "alt=" 135507vpj8g8gt1gwtjjcp.png "/>"
VC Task Display:
650) this.width=650; "id=" aimg_729 "src=" https://mianbao.cn.com/data/attachment/forum/201704/17/ 135556rt00fpppnfwc9wph.png "class=" Zoom "width=" "alt=" 135556rt00fpppnfwc9wph.png "/>"

This article is from the "Bread" blog, be sure to keep this source http://mianbao.blog.51cto.com/12784768/1920384

VMware Batch Auto-delete virtual machine script, can set whitelist

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.