Hacking VMware Private Python API for vSphere with a Quick Sample

It’s not a secret that VMware has a private Python API or so called Python binding for vSphere API. If you haven’t heard about it before, no worry. Here is a link to Hostd General Architecture. Somehow it’s not publicly released as a product for customers or partners. Over the years, I only heard a big bank uses it for internal IT automation. But it’s super easy to get it if you want – it’s part of every ESXi installation. Just check it out at /lib/python26-visor.zip if you SSH to your ESXi box. Update: in ESXi 5.5, look at the /lib/python2.6/site-packages.

The challenge is really how to use it, especially without any documentation. After many tries and some helps from others, I finally figured out how it works and how to write Python code on top of it. At the same time, I also understand why VMware decided not to release it as a public product and kind agree on the decision. Technically, I like it better than the open source pyshere. I think it would be ideal to port vijava API to Python.

In the following part, I am going to show you a quick sample using the API. NOTE: Use it at your own risk and don’t ask VMware for support. As an API for internal use, it may also change without typical deprecation process or even notice. Realistically, it’s unlikely go away or change dramatically any time soon as there are many tools built on top of the Python API.

from pyVim import connect
 
__author__ = "Steve Jin"
 
if __name__ == "__main__":
    """demo/test code for the PyVmomi"""
 
    print "Starting to connect to ESXi ..."
    si = connect.Connect("192.168.198.201", 443, "root", "doublecloud")
 
    searcher = si.content.searchIndex
    vm = searcher.FindByInventoryPath("ha-datacenter/vm/CentOS")
 
    vmname = vm.GetName()
    print "Hello " + vmname
 
    print "disconnecting to ESXi ..."
    connect.Disconnect(si)

You can run the Python script out of an ESXi server. It would print out something like the following:

Starting to connect to ESXi ...
Hello CentOS
disconnecting to ESXi ...

The quick sample demonstrates the flow of API usage. It’s no difference from any other bindings like vSphere Java API. Because Python does not have typing and PyVmomi uses dynamic types, it’s pretty hard to discover what class/method/properties are there in the API. Once you are familiar with the way how it works, it’s pretty easy to work with.

This entry was posted in vSphere API and tagged , , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

5 Comments

  1. Dave Parsons
    Posted November 21, 2013 at 6:30 am | Permalink

    Just to note that the python26-visor.zip no longer ships in ESXi 5.5. (Or at least I couldn’t find it).

  2. Posted November 22, 2013 at 3:15 am | Permalink

    Thanks Dave,

    That is true. The code is still there but in different format and place. Check out the following folder:

    /lib/python2.6/site-packages

    You can find the pyVmomi folder there. Just zip up the whole site-packages and you will get something similar as the zip I mentioned in post.

    Good luck!

    Steve

  3. Dave Parsons
    Posted November 23, 2013 at 5:21 pm | Permalink

    Thanks, Steve.

  4. Posted December 2, 2013 at 7:35 pm | Permalink

    Hi, very interesting. I have written a python script to “document” the python API in html files.

    The script is availabe in bitbucket through http://www.divulgaciones.net/2013/12/03/vsphere-python-api/

  5. Jaffar
    Posted April 29, 2014 at 2:51 pm | Permalink

    If you want the communication to be http instead of https when using the pyvmomi api.
    First Step:
    (of course you need to change vcenter settings to allow https. Go to Vcenter. Again if it is a appliance on linux change vi /etc/vmware-vpx/proxy.xml “accessMode to httpAndHttps” and
    on windows c:\Documents and Settings\AllUsers\Application Data\VMware VirtualCenter change the proxy.xml “accessMode to httpAndHttps”)

    Second Step:
    in the Connect.py provided by github (when you get the source of pyvmomi)
    Change Smartconnect with protocol=http instead of https
    see below
    SmartConnect(protocol=’http’, host=’localhost’, port=443, user=’root’, pwd=”,
    service=”hostd”, path=”/sdk”,
    preferredApiVersions=None):

    Step 3:
    Call smart connect with port 80, And all the communication will be clear soap messages for debug

3 Trackbacks

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

  • NEED HELP?


    My consulting helps clients with virtualization and cloud computing, including VMware infrastructure automation and orchestration, vSphere management APIs, and deep product integration with hypervisors. Current training offerings include vSphere APIs training, vCenter Orchestrator training, and etc. Should you, or someone you know, need these consulting services or training, please feel free to contact me: steve __AT__ doublecloud.org.

    Me: Steve Jin, VMware vExpert who authored the VMware VI and vSphere SDK by Prentice Hall, and created the de factor open source vSphere Java API while working at VMware engineering. Companies like Cisco, EMC, NetApp, HP, Dell, VMware, are among the users of the API and other tools I developed for their products, internal IT orchestration, and test automation.