-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvmcli.py
More file actions
79 lines (64 loc) · 2.11 KB
/
vmcli.py
File metadata and controls
79 lines (64 loc) · 2.11 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
76
77
78
79
#!/usr/bin/python
"""
vCli - cli tool to manage VMWare VMs
"""
import argparse
import atexit
import getpass
import sys
import traceback
from pyVim.connect import SmartConnect, Disconnect
from database import DataBase
from logger import Logger
from vmshell import VMShell
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('--dump2db', action='store_true')
args = parser.parse_args()
return args
def main():
"""
:return:
"""
data_base = None
vm_folders = []
args = get_args()
logger = Logger().logger
logger.debug("Logger Initialized %s" % logger)
if args.dump2db:
data_base = DataBase(logger)
logger.info("SQLite DB initialized %s" % data_base)
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)
VMShell(si, args).cmdloop()
return 0
# Start program
if __name__ == "__main__":
try:
main()
except Exception as e:
traceback.print_exc()
sys.exit(1)