Skip to content

Instantly share code, notes, and snippets.

@yfauser
Created August 15, 2016 09:57
Show Gist options
  • Save yfauser/82e2ff66e1f1656d6bfd22ed1bd10ec0 to your computer and use it in GitHub Desktop.
Save yfauser/82e2ff66e1f1656d6bfd22ed1bd10ec0 to your computer and use it in GitHub Desktop.
Tool to retrieve the external Id for a vnic using an opaque network
#!/usr/bin/env python
# coding=utf-8
#
# Copyright © 2015-2016 VMware, Inc. All Rights Reserved.
#
# Licensed under the X11 (MIT) (the “License”) set forth below;
#
# you may not use this file except in compliance with the License. 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. Permission is hereby granted, free of charge, to any person obtaining a copy of this
# software and associated documentation files (the "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
# Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all copies or substantial portions of
# the Software.
#
# "THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
# INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.”
from pyVim import connect
from pyVmomi import vim
import ssl
import atexit
import argparse
__author__ = 'yfauser'
def get_vnic_by_mac(vm, mac):
for device in vm.config.hardware.device:
if isinstance(device, vim.vm.device.VirtualEthernetCard):
if device.macAddress == mac:
return device
return None
def get_vnic_external_id(vm, vnic_mac):
vnic = get_vnic_by_mac(vm, vnic_mac)
if not vnic:
return None
nicspec = vim.vm.device.VirtualDeviceSpec()
nicspec.device = vnic
return nicspec.device.externalId
def connect_to_api(vchost, user='root', pwd='vmware'):
if hasattr(ssl, 'SSLContext'):
context = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
context.verify_mode = ssl.CERT_NONE
else:
context = None
if context:
service_instance = connect.SmartConnect(host=vchost, user=user, pwd=pwd, sslContext=context)
else:
service_instance = connect.SmartConnect(host=vchost, user=user, pwd=pwd)
atexit.register(connect.Disconnect, service_instance)
return service_instance.RetrieveContent()
def main():
parser = argparse.ArgumentParser(description='Tool for vnic external Id retrieval')
parser.add_argument('vc', help='The vCenter IP or hostname')
parser.add_argument('vc_user', help='The vCenter username')
parser.add_argument('vc_password', help='The vCenter password')
parser.add_argument('vm_ip', help='The IP address of a VM (note: VMTools is required for VC to know the IP)')
parser.add_argument('vnic_mac', help='The MAC address of a vnic on the VM in the '
'following notation: "xx:xx:xx:xx:xx:xx"')
args = parser.parse_args()
content = connect_to_api(args.vc, user=args.vc_user, pwd=args.vc_password)
vm_mo = content.searchIndex.FindByIp(None, args.vm_ip, True)
ext_id = get_vnic_external_id(vm_mo, args.vnic_mac)
print 'The external Id for this the vnic with MAC {} on VM {} is: {}'.format(args.vnic_mac, args.vm_ip, ext_id)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment