Using pyVmomi to collect ESXi info

I have been using PowerCLI to automate multiple vSphere  tasks . PowerCLI is very powerful for admin tasks and in majority of the cases powerCLI gets the work done in automated fashion. But I always wanted to experiment with the a language specific SDK for vSphere. VMware release multiple language specific SDK’s ( Perl, python, Java, .net etc) and I decided to experiment with Python SDK’s.

What’s pyVmomi

As we know that vSphere management operations are accessible/exposed through webservice API’s . Using interfaces like Managed Object Browser( MOB) we can do same operations which we typically do through UI. Similarly using python client stubs we can interact with the managed objects exposed by the vSphere platform. pyVmomi is the client side interface which allows python programs/scripts to connect to VC/ESX and help to manage/invoke methods. More details about vSphere managed objects and data objects are available in the vSphere API documentation.

How to write first python program : Collect ESXi hardware information

Following 3 steps are minimum ( and maximum) required to get started on pyVmomi ( on either windows or linux machine)

  1. Install Python
  2. Install pip
  3. Install pyVmomi  ( using pip install pyvmomi)

Thats all. Now we are ready to write our first script.The exact same steps and more details are available at

Now that we have the required client software installed, lets try to connect to an ESXi server and collect the hardware information.
Below is a sample code , which is available at the git repo at this link also. There are easier way to get into the hostobject, rather than going through different layers ( such as content.searchIndex.FindByIP), but to begin with, we will traverse through the complete layer. The detailed object model hierarchy is available @ this link

from pyVmomi import vim
from pyVim.connect import SmartConnect, Disconnect
import argparse
import atexit

def validate_options():
    parser = argparse.ArgumentParser(description='Input parameters')
    parser.add_argument('-s', '--source_host',dest='shost',
                         help='The ESXi source host IP')
    parser.add_argument('-u', '--username',dest='username',
                         help='The ESXi username')
    parser.add_argument('-p', '--password',dest='password',
                         help='The ESXi host password')
    return args

def main():
    si = SmartConnect(host=opts.shost,user=opts.username,pwd=opts.password )
    atexit.register(Disconnect, si)
    print 'The CPU vendor is %s and the model is %s'  %(cpuobj.vendor,cpuobj.description)
    print 'The server hardware is %s %s' %(systemInfo.vendor,systemInfo.model)
    print 'The memory size is %d GB' %((memoryInfo)/(1024*1024*1024))

if __name__ == '__main__':

To invoke the script use “python <script_name> -s <host_IP> -u <username> -p <password>”.

The output will be like below :

root@host1:~# python -s '' -u 'root' -p '<password>'
The CPU vendor is amd and the model is AMD Opteron(TM) Processor 6272.
The server hardware is HP ProLiant DL385 G7.
The memory size is 31 GB.

I will follow up with another script on how to create multiple linked-Clones using pyVmomi.



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s