-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_html5_console_https.py
133 lines (108 loc) · 3.93 KB
/
generate_html5_console_https.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
132
133
#!/usr/bin/env python
# Copyright (c) 2015 Christian Gerbrandt <derchris@derchris.eu>
#
# 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 port of William Lam's generateHTML5VMConsole.pl
Also ported SHA fingerprint fetching to Python OpenSSL library
"""
import atexit
import OpenSSL
import ssl
import sys
import time
from pyVim.connect import SmartConnect, Disconnect
from pyVmomi import vim
from tools import cli
def get_vm(content, name):
try:
name = unicode(name, 'utf-8')
except TypeError:
pass
vm = None
container = content.viewManager.CreateContainerView(
content.rootFolder, [vim.VirtualMachine], True)
for c in container.view:
print c.summary.config.name
if c.name == name:
vm = c
break
return vm
def get_args():
"""
Add VM name to args
"""
parser = cli.build_arg_parser()
parser.add_argument('-n', '--name',
required=True,
help='Name of Virtual Machine.')
parser.add_argument('-cert', '--cert-file',
required=False,
help='cert file of VC.')
args = parser.parse_args()
return cli.prompt_for_password(args)
def main():
"""
Simple command-line program to generate a URL
to open HTML5 Console in Web browser
"""
args = get_args()
print args
try:
si = SmartConnect(host=args.host,
user=args.user,
pwd=args.password,
port=int(args.port)) #,
# certFile='/root/source-code/vmware/pyvmomi-community-samples/b9-0.ca')
except Exception as e:
print 'Could not connect to vCenter host'
print repr(e)
sys.exit(1)
atexit.register(Disconnect, si)
content = si.RetrieveContent()
vm = get_vm(content, args.name)
vm_moid = vm._moId
vcenter_data = content.setting
vcenter_settings = vcenter_data.setting
# console_port = '7331'
console_port = '443'
# console_port = '9443'
for item in vcenter_settings:
key = getattr(item, 'key')
if key == 'VirtualCenter.FQDN':
vcenter_fqdn = getattr(item, 'value')
session_manager = content.sessionManager
session = session_manager.AcquireCloneTicket()
vc_cert = ssl.get_server_certificate((args.host, int(args.port)))
print 'vc_cert',vc_cert
vc_pem = OpenSSL.crypto.load_certificate(OpenSSL.crypto.FILETYPE_PEM,
vc_cert)
print 'vc_pem',vc_pem
vc_fingerprint = vc_pem.digest('sha1')
print vc_fingerprint
print "Open the following URL in your browser to access the " \
"Remote Console.\n" \
"You have 60 seconds to open the URL, or the session" \
"will be terminated.\n"
print "http://" + args.host + ":" + console_port + "/console/?vmId=" \
# print "https://" + args.host + ":" + console_port + "/console/?vmId=" \
# need login vc
# print "wss://" + args.host + ":" + console_port + "/vsphere-client/webconsole/authd?vmId=" \
print "https://" + args.host + ":" + console_port + "/vsphere-client/webconsole.html?vmId=" \
+ str(vm_moid) + "&vmName=" + args.name + "&host=" + vcenter_fqdn \
+ "&sessionTicket=" + session + "&thumbprint=" + vc_fingerprint
print "Waiting for 240 seconds, then exit"
time.sleep(240)
# Start program
if __name__ == "__main__":
main()