用python(pyvmomi)管理vmwareESXi/vCenter
環境
開發環境:Ubuntu 18.04 + python3.7
以下環境經過測試都可以使用:centos 6/7 + python3.4/3.6/3.7
和windows 7/2016 +python3.4/3.6/3.7
安裝必要模組pyvmomi
pip install pyvmomi
如果是linux系統有pip2和pip3 兩個版本,需要使用
pip3 install pyvmomi
程式碼
host=`ESXi 機器ip`
user=`root`
password=`密碼`
port=443#埠
context = None
if hasattr(ssl, `_create_unverified_context`):
context = ssl._create_unverified_context()
si = SmartConnect(host=host,user=user,pwd=password,port=port,sslContext=context)
if not si:
print("帳號密碼有問題")
atexit.register(Disconnect, si)
###################################################################################################
def WaitForTasks(tasks, si):#任務
pc = si.content.propertyCollector
taskList = [str(task) for task in tasks]
objSpecs = [vmodl.query.PropertyCollector.ObjectSpec(obj=task) for task in tasks]
propSpec = vmodl.query.PropertyCollector.PropertySpec(type=vim.Task,pathSet=[], all=True)
filterSpec = vmodl.query.PropertyCollector.FilterSpec()
filterSpec.objectSet = objSpecs
filterSpec.propSet = [propSpec]
filter = pc.CreateFilter(filterSpec, True)
try:
version, state = None, None
while len(taskList):
update = pc.WaitForUpdates(version)
for filterSet in update.filterSet:
for objSet in filterSet.objectSet:
task = objSet.obj
for change in objSet.changeSet:
if change.name == `info`:
state = change.val.state
elif change.name == `info.state`:
state = change.val
else:
continue
if not str(task) in taskList:
continue
if state == vim.TaskInfo.State.success:
taskList.remove(str(task))
elif state == vim.TaskInfo.State.error:
raise task.info.error
version = update.version
finally:
if filter:
filter.Destroy()
def getvmname():#列印所有虛擬機器名
content = si.RetrieveContent()
for child in content.rootFolder.childEntity:
if hasattr(child, `vmFolder`):
datacenter = child
vmFolder = datacenter.vmFolder
vmList = vmFolder.childEntity
for vm in vmList:
print(vm.summary.config.name)
print(vm.name)
def getvmstatus(vm_name):#獲取虛擬機器狀態
content = si.RetrieveContent()
for child in content.rootFolder.childEntity:
if hasattr(child, `vmFolder`):
datacenter = child
vmFolder = datacenter.vmFolder
vmList = vmFolder.childEntity
for vm in vmList:
if vm.summary.config.name == vm_name :
print(vm.summary.runtime.powerState)
def poweronvm(vm_name):#開啟虛擬機器
content = si.content
objView = content.viewManager.CreateContainerView(content.rootFolder,[vim.VirtualMachine],True)
vmList = objView.view
objView.Destroy()
tasks = [vm.PowerOn() for vm in vmList if vm.name in vm_name]
print(tasks)
WaitForTasks(tasks, si)
print("虛擬機器啟動成功")
def get_vmsnapshotinfo(vm_name):#檢視快照資訊
content = si.RetrieveContent()
container = content.viewManager.CreateContainerView(content.rootFolder, [vim.VirtualMachine], True)
for c in container.view:
if c.name == vm_name:
snapshots = c.snapshot.rootSnapshotList
return snapshots
def get_vmlist():#獲取虛擬機器列表
content = si.RetrieveContent()
for child in content.rootFolder.childEntity:
if hasattr(child, `vmFolder`):
datacenter = child
vmFolder = datacenter.vmFolder
vmList = vmFolder.childEntity
return vmList
def re_snapshot(vm_name,snapshot_name):##恢復快照
for vm in get_vmlist():
pass
#print(vm.name)
if vm.name == vm_name:
snapshots = vm.snapshot.rootSnapshotList
for snapshot in snapshots:
print(snapshot.name)
if snapshot_name == snapshot.name:
snap_obj = snapshot.snapshot
print ("恢復快照", snapshot.name)
task = [snap_obj.RevertToSnapshot_Task()]
WaitForTasks(task, si)
#re_snapshot("虛擬機器名","快照名") #恢復快照
#print(get_vmsnapshotinfo("虛擬機器名")) #獲取虛擬機器快照資訊
#print(get_vmsnapshotinfo("虛擬機器名"))#檢視虛擬機器快照
#poweronvm("虛擬機器名") #開啟虛擬機器
其它功能我閒了就增加
如果有其它著急需求或者使用方法可以回帖尋求幫助
相關文章
- 實現多臺EXSI主機集中管理——安裝VMware vCenter
- 【VMware vCenter】使用Reduced Downtime Update (RDU)升級更新vCenter Server。Server
- 【VMware vCenter】使用vSphere Diagnostic Tool(VDT)診斷工具檢查vCenter Server。Server
- vCenter Server使用筆記Server筆記
- VMware vCenter Site Recovery Manager VCP5.1部署實施與管理
- vcenter6.7生產環境叢集部署及應用
- 【VMware vCenter】連線和使用vCenter Server嵌入式vPostgres資料庫。Server資料庫
- VMware vCenter Server 6.7U3v 釋出下載 - ESXi 集中管理軟體Server
- VMware vCenter Server 6.7U3w 釋出下載 - ESXi 集中管理軟體Server
- 【VMware vCenter】在不重啟的情況下重置vCenter Server的root密碼。Server密碼
- vCenter Server 所需要的埠Server
- vCenter6.7 儲存配置
- vCenter6.7 部署安裝
- vCenter6.7 叢集配置
- 【VMware vCenter】vCenter Server 各版本證書有效期及過期解決辦法。Server
- 【VMware vCenter】VMware vCenter Server(VCSA) 5.5 版本證書過期問題處理過程。Server
- vCenter Server Appliance 所需要的埠ServerAPP
- Vmware Vcenter 任意檔案讀取漏洞
- vCenter6.7 分散式網路配置分散式
- VMware vSphere vCenter ServerAppliance 7.0安裝配置ServerAPP
- python上下文管理器closing的應用Python
- 【VMware vCenter】使用cmsso-util命令進行連結、刪除、修改多個vCenter Server(VCSA)的SSO域。Server
- 使用shell 方式對 vcenter 進行補丁升級
- 使用web client對 vcenter 進行補丁升級Webclient
- python-物件管理Python物件
- Python 管理哪家強?Python
- python的包管理Python
- 教你如何運用python實現學生資訊管理系統Python
- Python深入 Python的記憶體管理Python記憶體
- python版本管理(python環境隔離)Python
- 使用vCenter對ESXi主機進行補丁升級
- 實戰生產環境vCenter HA配置(VCSA6.5)
- 【VMware vCenter】升級到 vCenter Server 8.0 U3b 後 vSphere Client 出現卡死和不響應等問題的解決方法。Serverclient
- Python多環境管理Python
- virtualenv管理Python環境Python
- Python程式管理神器——SupervisorPython
- Python IO檔案管理Python
- conda管理python環境Python