Home > vSphere API > Hacking VMware Private Python API for vSphere with a Quick Sample

Hacking VMware Private Python API for vSphere with a Quick Sample

November 21st, 2013 Leave a comment Go to comments

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.

Time to learn how to "Google" and manage your VMware and clouds in a fast and secure

HTML5 App

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.

Categories: vSphere API Tags: , ,
  1. Dave Parsons
    November 21st, 2013 at 06:30 | #1

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

  2. November 22nd, 2013 at 03:15 | #2

    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
    November 23rd, 2013 at 17:21 | #3

    Thanks, Steve.

  4. December 2nd, 2013 at 19:35 | #4

    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
    April 29th, 2014 at 14:51 | #5

    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

  6. Sazid
    September 9th, 2014 at 04:11 | #6

    Hi,
    Can we run this code directly on ESX.

  7. September 9th, 2014 at 14:56 | #7

    Yes. ESXi has included PyVmomi library.

  1. December 3rd, 2013 at 02:40 | #1
  2. December 22nd, 2013 at 23:58 | #2
  3. December 24th, 2013 at 12:34 | #3