Skip to content

Instantly share code, notes, and snippets.

@hartsock
Created December 22, 2014 22:08

Revisions

  1. Shawn Hartsock created this gist Dec 22, 2014.
    32 changes: 32 additions & 0 deletions hosts_listing.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,32 @@
    #!/usr/bin/env python

    from __future__ import print_function

    from pyVim import connect
    from pyVmomi import vim

    si = connect.SmartConnect(host='vcsa', user='my_user', pwd='my_password')
    content = si.RetrieveContent()

    host_list = []
    stack = [content.rootFolder]
    while stack:
    entity = stack.pop()
    if entity.__class__.__name__ == 'vim.HostSystem':
    host_list.append(entity)
    if hasattr(entity, 'host'):
    if isinstance(entity.host, list):
    for host in entity.host:
    host_list.append(host)
    else:
    host_list.append(entity.host)
    if hasattr(entity, 'hostFolder'):
    stack.append(entity.hostFolder)
    if hasattr(entity, 'childEntity'):
    for child in entity.childEntity:
    stack.append(child)


    print('Host systems found: ')
    for host in host_list:
    print(host.name)