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.
Time to learn how to "Google" and manage your VMware and clouds in a fast and secureHTML5 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.