Prepare the environment and
install the package:
pyVim==0.0.21
pyvmomi==6.7.1
命令:pip install pyVim==0.0.21 pyvmomi==6.7.1
Pyvmomi connects to vsphere to obtain the vcenter operation object:
# -*- coding: utf-8 -*-
from pyVim.connect import SmartConnectNoSSL
class VmManage(object):
def __init__(self, host, user, password, port, ssl):
self.host = host
self.user = user
self.pwd = password
self.port = port
self.sslContext = ssl
try:
self.client = SmartConnectNoSSL(host=host,
user=user,
pwd=password,
port=443
)
self.content = self.client.RetrieveContent()
self.result = True
except Exception as e:
self.result = False
self.message = e
if __name__ == '__main__':
ip = '192.11.11.11'
user = 'xw'
password = '123456'
port = 443
vm = VmManage(host=ip,
user=user,
password=password,
port=port, ssl=None)
if vm.result:
# 说明连接成功,可以使用vm.client等
pass
else:
print vm.message
One thing to note is that SmartConnectNoSSL is introduced here instead of SmartConnect. The difference is that the former does not require ssl certificate verification, while the latter requires. If there is no certificate verification, use the former, and if there is a certificate content, you can use the latter.
If you want to get some information about vcenter, you must first have a basic understanding of the contents of the client after logging in to vcenter.
The figure after successful login is as follows:
In my opinion, after vcenter is logged in, the content on the left shows a tree diagram.
It can be understood that the vcenter account IP, data center, and cluster are folders, and the host host and virtual machines are files. The vcenter account on the login is the root directory and there is only one. Going a
step further, get these objects and understand the attribute fields of the objects: python pyvmomi operation VMware (2)