-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvmtree.py
More file actions
75 lines (61 loc) · 2.15 KB
/
vmtree.py
File metadata and controls
75 lines (61 loc) · 2.15 KB
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
#!/bin/usr/python
"""
Prints VMWare Datacenter as tree
author: samuelsh (c)
date: 2017
"""
import argparse
import atexit
import getpass
import traceback
import sys
from pyVim.connect import SmartConnect, Disconnect
from logger import Logger
from vmutils import VmUtils
def get_args():
"""
Supports the command-line arguments listed below.
"""
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')
parser.add_argument('-o', '--port', type=int, default=443, action='store',
help='Port to connect on')
parser.add_argument('-u', '--user', required=True, action='store',
help='User name to use when connecting to host')
parser.add_argument('-p', '--password', required=False, action='store',
help='Password to use when connecting to host')
parser.add_argument('-v', '--view', choices=['vms', 'hosts'],
help="Preferred view: VMs and Templates/Hosts and Clusters", required=True)
parser.add_argument('-d', '--folders_only', help="Print folders only", action="store_true")
args = parser.parse_args()
return args
def main():
"""
"""
args = get_args()
logger = Logger().logger
logger.debug("Logger Initialized %s" % logger)
if args.password:
password = args.password
else:
password = getpass.getpass(prompt='Enter password for host %s and '
'user %s: ' % (args.host, args.user))
si = SmartConnect(host=args.host,
user=args.user,
pwd=password)
if not si:
logger.error("Could not connect to the specified host using specified "
"username and password")
return -1
atexit.register(Disconnect, si)
VmUtils.print_vm_tree_vms(args, si)
return 0
# Start program
if __name__ == "__main__":
try:
main()
except Exception as e:
traceback.print_exc()
sys.exit(1)