Skip to content

Instantly share code, notes, and snippets.

@blemmenes
Created October 9, 2018 14:21
Show Gist options
  • Save blemmenes/435406abfeb6c884887c07994e71139e to your computer and use it in GitHub Desktop.
Save blemmenes/435406abfeb6c884887c07994e71139e to your computer and use it in GitHub Desktop.
portgroup connected vms
from pyVim.connect import SmartConnect, SmartConnectNoSSL, Disconnect
from pyVmomi import vim
import atexit
host = " "
user = " "
password = ' '
serviceInstance = SmartConnectNoSSL(host=host,
user=user,
pwd=password,
port=443)
atexit.register(Disconnect, serviceInstance)
content = serviceInstance.RetrieveContent()
def get_all_objs(content, vimtype):
obj = {}
container = content.viewManager.CreateContainerView(content.rootFolder, vimtype, True)
for managed_object_ref in container.view:
obj.update({managed_object_ref: managed_object_ref.name})
return obj
# dvss = get_all_objs(content, [vim.DistributedVirtualSwitch])
# portgroups = get_all_objs(content, [vim.DistributedVirtualPortgroup])
children = content.rootFolder.childEntity
for child in children: # Iterate though DataCenters
dc = child
clusters = dc.hostFolder.childEntity
for cluster in clusters: # Iterate through the clusters in the DC
for network in cluster.network: # Iterate through the networks in the cluster
name = network.name
# print name
try:
vlan = network.config.defaultPortConfig.vlan.vlanId
if isinstance(vlan, int):
count = 0
for vm in network.vm:
# print vm
count += 1
print("vmware_portgroup_connected_vms dc={0} cluster={1} portgroup={2} vlan={3} {4}".format(dc.name, cluster.name, name, vlan, count))
except:
print "exception"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment