Guest User

Untitled

a guest
Oct 9th, 2018
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.62 KB | None | 0 0
  1. from pyVim.connect import SmartConnect, SmartConnectNoSSL, Disconnect
  2. from pyVmomi import vim
  3. import atexit
  4.  
  5. host = " "
  6. user = " "
  7. password = ' '
  8.  
  9. serviceInstance = SmartConnectNoSSL(host=host,
  10. user=user,
  11. pwd=password,
  12. port=443)
  13. atexit.register(Disconnect, serviceInstance)
  14. content = serviceInstance.RetrieveContent()
  15.  
  16. def get_all_objs(content, vimtype):
  17. obj = {}
  18. container = content.viewManager.CreateContainerView(content.rootFolder, vimtype, True)
  19. for managed_object_ref in container.view:
  20. obj.update({managed_object_ref: managed_object_ref.name})
  21. return obj
  22.  
  23. # dvss = get_all_objs(content, [vim.DistributedVirtualSwitch])
  24. # portgroups = get_all_objs(content, [vim.DistributedVirtualPortgroup])
  25.  
  26. children = content.rootFolder.childEntity
  27. for child in children: # Iterate though DataCenters
  28. dc = child
  29. clusters = dc.hostFolder.childEntity
  30. for cluster in clusters: # Iterate through the clusters in the DC
  31. for network in cluster.network: # Iterate through the networks in the cluster
  32. name = network.name
  33. # print name
  34. try:
  35. vlan = network.config.defaultPortConfig.vlan.vlanId
  36. if isinstance(vlan, int):
  37. count = 0
  38. for vm in network.vm:
  39. # print vm
  40. count += 1
  41. print("vmware_portgroup_connected_vms dc={0} cluster={1} portgroup={2} vlan={3} {4}".format(dc.name, cluster.name, name, vlan, count))
  42. except:
  43. print "exception"
Add Comment
Please, Sign In to add comment