Skip to content

Instantly share code, notes, and snippets.

@hartsock
Created April 13, 2015 15:09
Show Gist options
  • Save hartsock/b7c3a9b5159b137b7b2d to your computer and use it in GitHub Desktop.
Save hartsock/b7c3a9b5159b137b7b2d to your computer and use it in GitHub Desktop.
This is a quick and dirty sample that shows how to go from a VM's name to a mac address by using the inventory path (more robust than a simple name)
def test_vm_to_mac(self):
si = connect.SmartConnect(host='vcsa',
user='my_user',
pwd='my_password')
content = si.RetrieveContent()
# where the name of the VM is 'box'
vm = content.searchIndex.FindByInventoryPath("Datacenter0/vm/box")
self.assertEqual(vm.name, 'box')
nics = [dev for dev in vm.config.hardware.device
if isinstance(dev, vim.vm.device.VirtualEthernetCard)]
self.assertEqual(nics[0].macAddress, '00:50:56:82:28:7d')
@hartsock
Copy link
Author

Note the line...

nics = [dev for dev in vm.config.hardware.device
                if hasattr(dev, 'macAddress')]

... is nice because it doesn't care about data types, only MAC addresses.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment