2013-12-13 17:02:41 -05:00
|
|
|
#!/usr/bin/python
|
|
|
|
# VMware vSphere Python SDK
|
2014-07-13 03:02:00 -04:00
|
|
|
# Copyright (c) 2008-2014 VMware, Inc. All Rights Reserved.
|
2013-12-13 17:02:41 -05:00
|
|
|
#
|
|
|
|
# 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
|
|
|
|
"""
|
|
|
|
|
|
|
|
from optparse import OptionParser, make_option
|
|
|
|
from pyVim.connect import SmartConnect, Disconnect
|
|
|
|
from pyVmomi import vmodl
|
2013-12-18 15:35:56 -08:00
|
|
|
|
|
|
|
import argparse
|
2013-12-13 17:02:41 -05:00
|
|
|
import atexit
|
2014-01-30 21:05:10 +01:00
|
|
|
import getpass
|
2014-01-30 21:41:19 +01:00
|
|
|
import sys
|
2013-12-13 17:02:41 -05:00
|
|
|
|
2013-12-18 15:35:56 -08:00
|
|
|
|
|
|
|
def GetArgs():
|
2013-12-13 17:02:41 -05:00
|
|
|
"""
|
|
|
|
Supports the command-line arguments listed below.
|
|
|
|
"""
|
2013-12-18 15:35:56 -08:00
|
|
|
parser = argparse.ArgumentParser(description='Process args for retrieving all the Virtual Machines')
|
|
|
|
parser.add_argument('-s', '--host', required=True, action='store', help='Remote host to connect to')
|
2013-12-20 10:17:34 +01:00
|
|
|
parser.add_argument('-o', '--port', type=int, default=443, action='store', help='Port to connect on')
|
2013-12-18 15:35:56 -08:00
|
|
|
parser.add_argument('-u', '--user', required=True, action='store', help='User name to use when connecting to host')
|
2014-01-30 21:37:48 +01:00
|
|
|
parser.add_argument('-p', '--password', required=False, action='store', help='Password to use when connecting to host')
|
2013-12-18 15:35:56 -08:00
|
|
|
args = parser.parse_args()
|
|
|
|
return args
|
2013-12-13 17:02:41 -05:00
|
|
|
|
|
|
|
|
2014-01-28 18:28:01 -07:00
|
|
|
def PrintVmInfo(vm, depth=1):
|
2013-12-13 17:02:41 -05:00
|
|
|
"""
|
2014-01-28 18:28:01 -07:00
|
|
|
Print information for a particular virtual machine or recurse into a folder with depth protection
|
2013-12-13 17:02:41 -05:00
|
|
|
"""
|
2014-01-28 18:28:01 -07:00
|
|
|
maxdepth = 10
|
|
|
|
|
|
|
|
# if this is a group it will have children. if it does, recurse into them and then return
|
|
|
|
if hasattr(vm, 'childEntity'):
|
|
|
|
if depth > maxdepth:
|
|
|
|
return
|
|
|
|
vmList = vm.childEntity
|
|
|
|
for c in vmList:
|
|
|
|
PrintVmInfo(c, depth+1)
|
|
|
|
return
|
2013-12-13 17:02:41 -05:00
|
|
|
|
|
|
|
summary = vm.summary
|
2014-06-06 20:35:11 -05:00
|
|
|
print "Name : ", summary.config.name
|
|
|
|
print "Path : ", summary.config.vmPathName
|
|
|
|
print "Guest : ", summary.config.guestFullName
|
|
|
|
print "Instance UUID : ", vm.summary.config.instanceUuid
|
|
|
|
print "BIOS UUID : ", vm.summary.config.uuid
|
|
|
|
|
2013-12-13 17:02:41 -05:00
|
|
|
annotation = summary.config.annotation
|
|
|
|
if annotation != None and annotation != "":
|
2014-06-06 20:35:11 -05:00
|
|
|
print "Annotation : ", annotation
|
|
|
|
print "State : ", summary.runtime.powerState
|
2013-12-13 17:02:41 -05:00
|
|
|
if summary.guest != None:
|
|
|
|
ip = summary.guest.ipAddress
|
|
|
|
if ip != None and ip != "":
|
2014-06-06 20:35:11 -05:00
|
|
|
print "IP : ", ip
|
2013-12-13 17:02:41 -05:00
|
|
|
if summary.runtime.question != None:
|
2014-06-06 20:35:11 -05:00
|
|
|
print "Question : ", summary.runtime.question.text
|
2013-12-13 17:02:41 -05:00
|
|
|
print ""
|
|
|
|
|
|
|
|
def main():
|
|
|
|
"""
|
|
|
|
Simple command-line program for listing the virtual machines on a system.
|
|
|
|
"""
|
|
|
|
|
2013-12-18 15:35:56 -08:00
|
|
|
args = GetArgs()
|
2014-01-30 21:37:48 +01:00
|
|
|
if args.password:
|
|
|
|
password = args.password
|
|
|
|
else:
|
|
|
|
password = getpass.getpass(prompt='Enter password for host %s and user %s: ' % (args.host,args.user))
|
2014-01-30 21:05:10 +01:00
|
|
|
|
2013-12-13 17:02:41 -05:00
|
|
|
try:
|
2013-12-18 15:35:56 -08:00
|
|
|
si = None
|
|
|
|
try:
|
|
|
|
si = SmartConnect(host=args.host,
|
|
|
|
user=args.user,
|
2014-01-30 21:05:10 +01:00
|
|
|
pwd=password,
|
2013-12-18 15:35:56 -08:00
|
|
|
port=int(args.port))
|
|
|
|
except IOError, e:
|
|
|
|
pass
|
2013-12-13 17:02:41 -05:00
|
|
|
if not si:
|
2013-12-18 15:35:56 -08:00
|
|
|
print "Could not connect to the specified host using specified username and password"
|
2013-12-13 17:02:41 -05:00
|
|
|
return -1
|
|
|
|
|
|
|
|
atexit.register(Disconnect, si)
|
|
|
|
|
|
|
|
content = si.RetrieveContent()
|
|
|
|
datacenter = content.rootFolder.childEntity[0]
|
|
|
|
vmFolder = datacenter.vmFolder
|
|
|
|
vmList = vmFolder.childEntity
|
|
|
|
for vm in vmList:
|
|
|
|
PrintVmInfo(vm)
|
|
|
|
except vmodl.MethodFault, e:
|
|
|
|
print "Caught vmodl fault : " + e.msg
|
|
|
|
return -1
|
|
|
|
except Exception, e:
|
|
|
|
print "Caught exception : " + str(e)
|
|
|
|
return -1
|
|
|
|
|
|
|
|
return 0
|
|
|
|
|
|
|
|
# Start program
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|