Home > Virtualization > Hidden esxcli Command Output Formats You Probably Don’t Know

Hidden esxcli Command Output Formats You Probably Don’t Know

December 3rd, 2013 Leave a comment Go to comments

Besides the vim-cmd command I covered earlier, there is another powerful set of commands in ESXi – esxcli. As you can find from the help of the command, it covers 10 namespaces and drills down several layers down. The typical operations with the namespaces are get, set, and list. If you are familiar with REST API, you can think of the bottom level namespaces are resources.

As there are already many coverage about esxcli command, I will only talk about something hidden and can be very helpful for your administering and scripting. I only find these hidden options while playing with VMware private python APIs. Believe or not, the esxcli command is built on top of the private Python API that I had introduced not long ago. So if you need more samples on how to code against the private python API, you can check out the esxcli script. I will show you where to find the esxcli source code.

Lost VMs or Containers? Too Many Consoles? Too Slow GUI? Time to learn how to "Google" and manage your VMware and clouds in a fast and secure HTML5 App.

~ # esxcli
Usage: esxcli [options] {namespace}+ {cmd} [cmd options]
                        Override the formatter to use for a given command. Available formatter: xml, csv, keyvalue
  --debug               Enable debug or internal use options
  --version             Display version information for the script
  -?, --help            Display usage information for the script
Available Namespaces:
  esxcli                Commands that operate on the esxcli system itself allowing users to get additional information.
  fcoe                  VMware FCOE commands.
  hardware              VMKernel hardware properties and commands for configuring hardware.
  iscsi                 VMware iSCSI commands.
  network               Operations that pertain to the maintenance of networking on an ESX host. This includes a wide variety of commands to manipulate virtual networking components (vswitch, portgroup, etc)
                        as well as local host IP, DNS and general host networking settings.
  sched                 VMKernel system properties and commands for configuring scheduling related functionality.
  software              Manage the ESXi software image and packages
  storage               VMware storage commands.
  system                VMKernel system properties and commands for configuring properties of the kernel core system.
  vm                    A small number of operations that allow a user to Control Virtual Machine operations.

~ # which esxcli
~ # ls -l /sbin/esxcli
lrwxrwxrwx 1 root root 15 Mar 23 2013 /sbin/esxcli -> /sbin/esxcli.py

As it shows clearly that the esxcli command is essentially a link for the /sbin/esxcli.py Python script. I am not going to list all the source here, but Within the esxcli.py file, you would find the following lines:

      self.formatters = {
         "xml": XmlVisitor,
         "csv": CsvVisitor,
         "keyvalue": KeyValueVisitor,
      # For testing purpose
      self.debugFormatters = {
         "python": PythonVisitor,
         "json": JsonVisitor,
         "html": HtmlOutputFormatter,
         "table": TextOutputFormatter,
         "tree": TextOutputFormatter,
         "simple": TextOutputFormatter,

The trick is to turn on the debug mode with the –debug switch in the command line. In fact, the help of the esxcli has hinted “Enable debug or internal use options,” but it does not explicitly state what these options are.

To show how different formatters work, I just use a very simple esxcli command for the version information. You can try other more complicated ones by yourself.

~ # esxcli system version get
   Product: VMware ESXi
   Version: 5.1.0
   Build: Releasebuild-1065491
   Update: 1
~ # esxcli --debug --formatter=python system version get
{"Build": "Releasebuild-1065491", "Product": "VMware ESXi", "Update": "1", "Version": "5.1.0"}
~ # esxcli --debug --formatter=json system version get
{"Build": "Releasebuild-1065491", "Product": "VMware ESXi", "Update": "1", "Version": "5.1.0"}
~ # esxcli --debug --formatter=html system version get
<tr><th class="rt">Product</th><td width="90%">VMware ESXi</td></tr>
<tr><th class="rt">Version</th><td width="90%">5.1.0</td></tr>
<tr><th class="rt">Build</th><td width="90%">Releasebuild-1065491</td></tr>
<tr><th class="rt">Update</th><td width="90%">1</td></tr>
~ # esxcli --debug --formatter=table system version get
Product      Version  Build                 Update
-----------  -------  --------------------  ------
VMware ESXi  5.1.0    Releasebuild-1065491  1
~ # esxcli --debug --formatter=tree system version get
Runtime error
~ # esxcli --debug --formatter=simple system version get
   Product: VMware ESXi
   Version: 5.1.0
   Build: Releasebuild-1065491
   Update: 1
~ # esxcli --debug --formatter=xml system version get
<?xml version="1.0" encoding="utf-8"?>
<output xmlns="http://www.vmware.com/Products/ESX/5.0/esxcli">
   <structure typeName="VersionGet">
      <field name="Build">
      <field name="Product">
         <string>VMware ESXi</string>
      <field name="Update">
      <field name="Version">
~ # esxcli --debug --formatter=csv system version get
Releasebuild-1065491,VMware ESXi,1,5.1.0,
~ # esxcli --debug --formatter=keyvalue system version get
VersionGet.Product.string=VMware ESXi

You may be wondering what is the big deal of these formats? The public formatters are mostly good enough for most use cases. I like the csv formatter a lot as it makes the parsing the output a lot easier. But remember that the order of fields may be different when using different formatters. So you cannot assume there is a standard order and always check the output carefully. The XML format may be very interesting for programming. If you remember what I introduced about the XML based interface for managing Cisco Nexus, this XML format makes possible similar APIs.

The private formmater for json would be very helpful if you have something written in JavaScript or Node.js. The Python formatter seems no difference from json one.

The HTML formmater is a very interesting one. For command line tool, you don’t need HTML at all. What could be usage? I will continue to discuss it for a powerful use case. Stay tuned.

Categories: Virtualization Tags: , ,