VMware has released pyVmomi for developers who wants to code in python. pyVmomi is a VMware SDK to manage ESXi and vCenter using python.
Using pip we can install:
pip install pyvmomi
Developers can download the pyvmomi package from python.org
https://pypi.python.org/pypi/pyvmomi
When we extract and list the package
LICENSE.txt NOTICE.txt pyVim pyvmomi.egg-info requirements.txt setup.cfg test-requirements.txt tox.ini MANIFEST.in PKG-INFO pyVmomi README.rst sample setup.py tests
Perform the below steps: python setup.py install python setup.py develop
pyVmomi package provide mainly two modules : pyVim and pyVmomi
Let’s explore pyVim. This module will be used to connect to ESXi and vSphere Center.
>>> import pyVim
>>> help(pyVim)
PACKAGE CONTENTS
connect
task
>>>import pyVim.connect
NAME
pyVim.connect – Connect to a VMOMI ServiceInstance.
>>>help(pyVim.connect)
This contents two function to connect service instance: SmartConnect and Connect
SmartConnect(protocol=’https’, host=’localhost’, port=443, user=’root’, pwd=”, service=’hostd’, path=’/sdk’, preferredApiVersions=None, keyFile=None, certFile=N
one, thumbprint=None, sslContext=None, b64token=None, mechanism=’userpass’)
Determine the most preferred API version supported by the specified server,then connect to the specified server using that API version, login and return the service instance object. Throws any exception back to caller. The service instance object is also saved in the library for easy access.Clients should modify the service parameter only when connecting to a VMOMI server other than hostd/vpxd. For both of the latter, the default value is fine.
Connect(host=’localhost’, port=443, user=’root’, pwd=”, service=’hostd’, adapter=’SOAP’, namespace=None, path=’/sdk’, version=None, keyFile=None, certFile=None,
thumbprint=None, sslContext=None, b64token=None, mechanism=’userpass’)
Connect to the specified server, login and return the service instance object. Throws any exception back to caller. The service instance object is also saved in the library for easy access. Clients should modify the service parameter only when connecting to
a VMOMI server other than hostd/vpxd. For both of the latter, the default value is fine
We can connect using SmartConnect From pyVim.connect import SmartConnect conn = SmartConnect(host=<”ESXi/vSphere Center ip”>, user = <”username”>, pwd= <”password”>) Print conn 'vim.ServiceInstance:ServiceInstance'
This is service instance type of managed object. For writing code further we can make use of vSphere mob interface: http://<vc-ip>/mob
Wrote a small code which fetch the build number and name of datecenter
#!/bin/python from pyVim.connect import SmartConnect conn = SmartConnect(host="<VC hostname>",user="<username>",pwd="<password>") print ("Multi host is supported") print (conn.capability.multiHostSupported) print ("----------------------------------------------------------") print ("VC System Current time") print (conn.CurrentTime()) print ("----------------------------------------------------------") print ("VC server build info") print (conn.content.about.version) print (conn.content.about.fullName) print (conn.content.about.build) print ("----------------------------------------------------------") datacenter = conn.content.rootFolder.childEntity print ("Name of datacenters") print ("----------------------------------------------------------") for i in datacenter: print (i.name)
# python vcconnect.py Multi host is supported True ---------------------------------------------------------- VC System Current time 2016-09-22 14:33:17. ---------------------------------------------------------- VC server build info 6.0.0 VMware vCenter Server 6.0.0 build-xxxxxx ---------------------------------------------------------- Name of datacenters ---------------------------------------------------------- test_Lab
Below are the screenshot how I traverse for Version number:
conn.content.about.version
conn = SmartConnect(host=<”ESXi/vSphere Center ip”>, user = <”username”>, pwd= <”password”>) Print conn 'vim.ServiceInstance:ServiceInstance'
conn is Service Instance Object. listing the properties and methods from mob interface.
In properties segment, I am listing the content property.This content the about property.
In about we can list version:
That’s the way below code has traversed.
print ("VC server build info") print (conn.content.about.version) print (conn.content.about.fullName) print (conn.content.about.build)