vmwarevsphereapi(pyVmomi)
1. 简介
pyvmomi是⼀款 VMware vSphere API的python sdk,可以通过它管理ESX, ESXi和Vcenter。它提供了⾮常完全的信息和操作,如虚拟机信息、存储信息、物理设备信息、虚拟机克隆、虚拟机关机等等。
2. 安装使⽤
安装⽅式
pip install pyVmomi
3.使⽤⽰例
import ssl
import atexit
from pyVmomi import vim
from pyVim import connect
def print_vm_info(virtual_machine):
summary = virtual_machine.summary
print("keys: ", dir(summary))
print("Name : ", fig.name)
print("Template : ", plate)
print("Path : ", fig.vmPathName)
print("Guest : ", fig.guestFullName)
print("Instance UUID : ", fig.instanceUuid)
print("Bios UUID : ", fig.uuid)
annotation = fig.annotation
if annotation:
print("Annotation : ", annotation)
print("State : ", summary.runtime.powerState)
if summary.guest is not None:
ip_address = summary.guest.ipAddress
tools_version = lsStatus
if tools_version is not None:
print("VMware-tools : ", tools_version)
else:
print("VMware-tools : None")
if ip_address:
print("IP : ", ip_address)
else:
print("IP : None")
if summary.runtime.question:
print("Question : ", summary.)
print(" ")
# 配置ssl
sslcontext = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
sslcontext.verify_mode = ssl.CERT_NONE
# 连接到vcenter
service_instance = connect.SmartConnect(host="192.168.199.150", port=443, user="root", pwd="123123", sslContext=sslcontext) ister(connect.Disconnect, service_instance)
content = service_instance.RetrieveContent()
# 获取执⾏信息
container = Folder
viewType = [vim.VirtualMachine]
recursive = True
containerView = content.viewManager.CreateContainerView(container, viewType, recursive)
children = containerView.view
if not children:
print("no virtual_machine")
for child in children:
print_vm_info(child)
注: 通过vcenter的api接⼝⽂档可以获取更加详细和完整的信息
以下内容为通过python的dir()获取VirtualMachine对象取得的
VirtualMachine的属性有
[‘AcquireMksTicket’, ‘AcquireTicket’, ‘Answer’, ‘AnswerVM’, ‘ApplyEvcMode’, ‘ApplyEvcModeVM_Task’,‘Array’, ‘AttachDisk’, ‘AttachDisk_Task’, ‘CheckCustomizationSpec’, ‘Clone’, ‘CloneVM_Task’,
‘ConsolidateDisks’, ‘ConsolidateVMDisks_Task’, ‘CreateScreenshot’, ‘CreateScreenshot_Task’,
‘CreateSecondary’, ‘CreateSecondaryEx’, ‘CreateSecondaryVMEx_Task’, ‘CreateSecondaryVM_Task’,‘CreateSnapshot’, ‘CreateSnapshotEx’, ‘CreateSnapshotEx_Task’, ‘CreateSnapshot_Task’, ‘CryptoUnlock’,‘CryptoUnlock_Task’, ‘Customize’, ‘CustomizeVM_Task’, ‘DefragmentAllDisks’, ‘Destroy’,
‘Destroy_Task’, ‘DetachDisk’, ‘DetachDisk_Task’, ‘DisableSecondary’, ‘DisableSecondaryVM_Task’,‘EnableSecondary’, ‘EnableSecondaryVM_Task’, ‘EstimateStorageForConsolidateSnapshots_Task’,
‘EstimateStorageRequirementForConsolidate’, ‘ExportVm’, ‘ExtractOvfEnvironment’, ‘InstantClone’,
‘InstantClone_Task’, ‘MakePrimary’, ‘MakePrimaryVM_Task’, ‘MarkAsTemplate’, ‘MarkAsVirtualMachine’,‘Migrate’, ‘MigrateVM_Task’, ‘MountToolsInstaller’, ‘PowerOff’, ‘PowerOffVM_Task’, ‘PowerOn’,‘PowerOnVM_Task’, ‘PromoteDisks’, ‘PromoteDisks_Task’, ‘PutUsbScanCodes’,
‘QueryChangedDiskAreas’, ‘QueryFaultToleranceCompatibility’, ‘QueryFaultToleranceCompatibilityEx’,
‘QueryUnownedFiles’, ‘RebootGuest’, ‘ReconfigVM_Task’, ‘Reconfigure’, ‘RefreshStorageInfo’,
‘Reload’, ‘ReloadFromPath’, ‘Relocate’, ‘RelocateVM_Task’, ‘RemoveAllSnapshots’,
‘RemoveAllSnapshots_Task’, ‘Rename’, ‘Rename_Task’, ‘Reset’, ‘ResetGuestInformation’,
‘ResetVM_Task’, ‘RevertToCurrentSnapshot’, ‘RevertToCurrentSnapshot_Task’, ‘SendNMI’,
‘SetCustomValue’, ‘SetDisplayTopology’, ‘SetScreenResolution’, ‘ShutdownGuest’, ‘StandbyGuest’,‘StartRecording’, ‘StartRecording_Task’, ‘StartReplaying’, ‘StartReplaying_Task’, ‘StopRecording’,
‘StopRecording_Task’, ‘StopReplaying’, ‘StopReplaying_Task’, ‘Suspend’, ‘SuspendVM_Task’,
‘Terminate’, ‘TerminateFaultTolerantVM’, ‘TerminateFaultTolerantVM_Task’, ‘TerminateVM’,
‘TurnOffFaultTolerance’, ‘TurnOffFaultToleranceForVM_Task’, ‘UnmountToolsInstaller’, ‘Unregister’,
python虚拟机‘UnregisterVM’, ‘UpgradeTools’, ‘UpgradeTools_Task’, ‘UpgradeVM_Task’, ‘UpgradeVirtualHardware’,‘_GetMethodInfo’, ‘_GetMethodList’, ‘_GetMoId’, ‘_GetPropertyInfo’, ‘_GetPropertyList’,
‘_GetServerGuid’, ‘_GetStub’, ‘_InvokeAccessor’, ‘_InvokeMethod’, ‘class’, ‘delattr’, ‘dict’, ‘dir’,‘doc’, ‘eq’, ‘format’, ‘ge’, ‘getattribute’, ‘gt’, ‘hash’, ‘init’, ‘init_subclass’, ‘le’, ‘lt’,
‘module’, ‘ne’, ‘new’, ‘reduce’, ‘reduce_ex’, ‘repr’, ‘setattr’, ‘sizeof’, ‘str’, ‘subclasshook’,‘weakref’, ‘_methodInfo’, ‘_moId’, ‘_propInfo’, ‘_propList’, ‘_serverGuid’, ‘_stub’, ‘_version’,‘_wsdlName’, ‘alarmActionsEnabled’, ‘availableField’, ‘capability’, ‘config’, ‘configIssue’,
‘configStatus’, ‘customValue’, ‘datastore’, ‘declaredAlarmState’, ‘disabledMethod’, ‘effectiveRole’,‘environmentBrowser’, ‘guest’, ‘guestHeartbeatStatus’, ‘layout’, ‘layoutEx’, ‘name’, ‘network’,‘overallStatus’, ‘parent’, ‘parentVApp’, ‘permission’, ‘recentTask’,
‘reloadVirtualMachineFromPath_Task’, ‘resourceConfig’, ‘resourcePool’, ‘rootSnapshot’, ‘runtime’,‘set
CustomValue’, ‘snapshot’, ‘storage’, ‘summary’, ‘tag’, ‘triggeredAlarmState’, ‘value’]
summary的属性有
‘config’, ‘customValue’, ‘dynamicProperty’, ‘dynamicType’, ‘guest’, ‘overallStatus’, ‘quickStats’,‘runtime’,‘storage’, ‘vm’
当viewType为HostSystem时,summary的属性有
‘config’, ‘currentEVCModeKey’, ‘customValue’, ‘dynamicProperty’, ‘dynamicType’, ‘gateway’,
‘hardware’, ‘host’, ‘managementServerIp’, ‘maxEVCModeKey’, ‘overallStatus’, ‘quickStats’,
‘rebootRequired’, ‘runtime’, ‘tpmAttestation’
⽰例结果
以下结果⾮来⾃上述代码
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论