Last active
October 24, 2021 18:17
-
-
Save sky-joker/795268894415038c3d487c9e269d9bf8 to your computer and use it in GitHub Desktop.
VMwareのHTML Console SDKで使用するWebSocketのURLを生成するツール
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
from __future__ import absolute_import, division, print_function | |
from pyVim.connect import SmartConnect, Disconnect | |
from pyVmomi import vim | |
from getpass import getpass | |
import ssl | |
import atexit | |
import argparse | |
import sys | |
def options(): | |
parser = argparse.ArgumentParser(prog='vmware_get_console_url.py', | |
add_help=True, | |
description='Tool to generate web console url for HTML console sdk.') | |
parser.add_argument('--host', '-vc', | |
type=str, required=True, | |
help='The host vCenter or ESXi(IP is fine)') | |
parser.add_argument('--username', '-u', | |
type=str, default='[email protected]', | |
help='login username') | |
parser.add_argument('--password', '-p', | |
type=str, | |
help='login password') | |
parser.add_argument('--target', '-t', | |
type=str, required=True, | |
help='target vm name') | |
args = parser.parse_args() | |
if not(args.password): | |
args.password = getpass(prompt='Password: ') | |
return args | |
def main(): | |
args = options() | |
context = None | |
if hasattr(ssl, '_create_unverified_context'): | |
context = ssl._create_unverified_context() | |
try: | |
si = SmartConnect(host=args.host, | |
user=args.username, | |
pwd=args.password, | |
sslContext=context) | |
atexit.register(Disconnect, si) | |
except Exception as e: | |
print('Error: %s' % e.msg) | |
sys.exit(1) | |
content = si.content | |
vms = content.viewManager.CreateContainerView(content.rootFolder, | |
[vim.VirtualMachine], | |
True) | |
vm_obj = '' | |
if vms: | |
for vm in vms.view: | |
if vm.name == args.target: | |
vm_obj = vm | |
break | |
if vm_obj: | |
ticket_info = vm_obj.AcquireTicket("webmks") | |
host = ticket_info.host | |
port = ticket_info.port | |
ticket = ticket_info.ticket | |
if host: | |
print('wss://%s:%s/ticket/%s' % (host, port, ticket)) | |
else: | |
print('wss://%s:%s/ticket/%s' % (args.host, port, ticket)) | |
else: | |
print('Error: %s not found' % args.target) | |
sys.exit(1) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment