Skip to content

Instantly share code, notes, and snippets.

@amdei
Created September 23, 2022 22:04
Show Gist options
  • Save amdei/d3660f8e49383417e3284b0abdd3dc71 to your computer and use it in GitHub Desktop.
Save amdei/d3660f8e49383417e3284b0abdd3dc71 to your computer and use it in GitHub Desktop.
List all VMs on Datacenter with mapping their NICs to DVS
#!/usr/bin/env python
# VMware vSphere Python SDK
# Copyright (c) 2008-2021 VMware, Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
Python program for listing the VMs on an ESX / vCenter host
"""
import re
from pyVmomi import vmodl, vim
from tools import cli, service_instance, pchelper
def get_netdev_pg_name(pg_key, dvs_pg):
pg_name = 'WTF!?!'
pgn = None
for dvs_name in dvs_pg.keys():
pgn = next(pg for pg in dvs_pg[dvs_name] if pg.key == pg_key)
if pgn is not None:
pgn = dvs_name + '/' + pgn.name
break
if pgn is not None:
pg_name = pgn
return pg_name
def print_vm_info(virtual_machine, dvs_pg):
"""
Print information for a particular virtual machine or recurse into a
folder with depth protection
"""
#print(virtual_machine.config.hardware.device)
summary = virtual_machine.summary
print("VM Name : ", summary.config.name)
print("Path : ", summary.config.vmPathName)
print("Guest : ", summary.config.guestFullName)
dev_ether = vim.vm.device.VirtualEthernetCard
net_b_info = vim.vm.device.VirtualEthernetCard.NetworkBackingInfo
dvp_b_info = vim.vm.device.VirtualEthernetCard.DistributedVirtualPortBackingInfo
for dev in virtual_machine.config.hardware.device:
if isinstance(dev, dev_ether):
connected_to = 'WTF???'
if isinstance(dev.backing, net_b_info):
connected_to = dev.backing.deviceName
elif isinstance(dev.backing, dvp_b_info):
# connected_to = dev.backing.port.portgroupKey
connected_to = get_netdev_pg_name(dev.backing.port.portgroupKey, dvs_pg)
else:
print('Add handling of ', type(dev.backing), '!')
print(dev.deviceInfo.label, "->", connected_to)
print("")
def get_all_dvs(content, datacenter):
dvs_lists = pchelper.get_all_obj(
content, [vim.DistributedVirtualSwitch], folder=datacenter.networkFolder)
return list(dvs_lists)
def main():
"""
Simple command-line program for listing the virtual machines on a system.
"""
parser = cli.Parser()
parser.add_required_arguments(cli.Argument.DATACENTER_NAME)
parser.add_custom_argument('-f', '--find', required=False,
action='store', help='String to match VM names')
parser.add_custom_argument('-w', '--sort', required=False,
action='store_true', help='Sort VM names alphabetically')
args = parser.get_args()
si = service_instance.connect(args)
try:
content = si.RetrieveContent()
datacenter = pchelper.get_obj(content, [vim.Datacenter], args.datacenter_name)
if datacenter is None:
print("Failed to find the datacenter %s" % args.datacenter_name)
return 3
dvs_list = get_all_dvs(content, datacenter)
dvs_pg = {}
for dvs in dvs_list:
dvs_pg[dvs.name] = list(dvs.portgroup)
container = content.rootFolder # starting point to look into
view_type = [vim.VirtualMachine] # object types to look for
recursive = True # whether we should look into it recursively
container_view = content.viewManager.CreateContainerView(
container, view_type, recursive)
children = container_view.view
if args.find is not None:
pat = re.compile(args.find, re.IGNORECASE)
children = list(filter(lambda child: pat.search(child.summary.config.name), children))
if args.sort is not None:
children.sort(key = lambda child: child.summary.config.name)
print('Datacenter Name'.ljust(40) + ' :', args.datacenter_name)
for child in children:
print_vm_info(child, dvs_pg)
except vmodl.MethodFault as error:
print("Caught vmodl fault : " + error.msg)
return -1
return 0
# Start program
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment