-
Notifications
You must be signed in to change notification settings - Fork 0
/
12-getallvms.py
131 lines (113 loc) · 4.3 KB
/
12-getallvms.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#!/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 os
import re
import sys
sys.path.append(os.path.realpath(".") + '/venv/Lib/site-packages/')
from pyVmomi import vmodl, vim
from tools import cli, service_instance
from pyVim.connect import SmartConnect, Disconnect
from tools import cli, pchelper, service_instance
from configparser import ConfigParser
config = ConfigParser()
# read user/pw from ini
fileConf = '../vc_info.ini' # File config store login info, see vc_info.ini.sample
# If file config exists:
if os.path.exists(fileConf):
config.read(fileConf)
serverDomain = config.get('config', 'server')
username = config.get('config', 'username')
password = config.get('config', 'password')
else:
serverDomain = "<enter your server domain>"
username = "<enter user, ex: [email protected]>"
password = "<enter password>"
###############################
def print_vm_info(virtual_machine):
"""
Print information for a particular virtual machine or recurse into a
folder with depth protection
"""
summary = virtual_machine.summary
print("\n")
print("------------------------")
print(summary)
print("VID : ", summary.vm)
print("Name : ", summary.config.name)
print("Template : ", summary.config.template)
print("Path : ", summary.config.vmPathName)
print("Guest : ", summary.config.guestFullName)
print("Instance UUID : ", summary.config.instanceUuid)
print("Bios UUID : ", summary.config.uuid)
annotation = summary.config.annotation
if annotation:
print("Annotation : ", annotation)
print("State : ", summary.runtime.powerState)
if summary.guest is not None:
ip_address = summary.guest.ipAddress
tools_version = summary.guest.toolsStatus
if tools_version is not None:
print("VMware-tools: ", tools_version)
else:
print("Vmware-tools: None")
if ip_address:
print("IP : ", ip_address)
else:
print("IP : None")
if summary.runtime.question is not None:
print("Question : ", summary.runtime.question.text)
print("")
def main():
"""
Simple command-line program for listing the virtual machines on a system.
"""
# parser = cli.Parser()
# parser.add_custom_argument('-f', '--find', required=False,
# action='store', help='String to match VM names')
# args = parser.get_args()
# si = service_instance.connect(args)
si = SmartConnect(host=serverDomain,
user=username,
pwd=password,
port=443, disableSslCertValidation=True)
try:
content = si.RetrieveContent()
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)
for child in children:
print_vm_info(child)
input("...")
# if args.find is None:
# print_vm_info(child)
# else:
# if pat.search(child.summary.config.name) is not None:
# print_vm_info(child)
except vmodl.MethodFault as error:
print("Caught vmodl fault : " + error.msg)
return -1
return 0
# Start program
if __name__ == "__main__":
main()