Skip to content

Instantly share code, notes, and snippets.

@hartsock
Created December 22, 2014 22:08
Show Gist options
  • Save hartsock/87e99b92f56a5e303dce to your computer and use it in GitHub Desktop.
Save hartsock/87e99b92f56a5e303dce to your computer and use it in GitHub Desktop.
A quick and dirty script to get all the hosts in a vCenter.
#!/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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment