Created
August 1, 2015 17:55
-
-
Save bthornto/bfce98039e845fa44e9b to your computer and use it in GitHub Desktop.
html5 console connection to a vcenter guest
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
require 'rbvmomi' | |
require 'pry-byebug' | |
require 'socket' | |
require 'openssl' | |
require 'digest/sha1' | |
require 'io/console' | |
def get_thumbprint(vc) | |
ssl_context = OpenSSL::SSL::SSLContext.new | |
tcp_client = TCPSocket.new vc, 443 | |
ssl_client = OpenSSL::SSL::SSLSocket.new tcp_client, ssl_context | |
ssl_client.connect | |
certificate = ssl_client.peer_cert | |
thumbprint = Digest::SHA1.hexdigest(certificate.to_der).upcase.scan(/../).join(':') | |
thumbprint | |
end | |
def build_url(username, password, vc, vm_name, thumbprint) | |
vim = RbVmomi::VIM.connect host: vc, user: username, password: password, :insecure => true | |
propertypaths = %w( name summary.vm) | |
vim.rootFolder.childEntity.grep(RbVmomi::VIM::Datacenter).each do |dc| | |
vm_inventory = dc.vmFolder.inventory_flat({ VirtualMachine: propertypaths}) | |
vm_inventory.select!{ |x| x.class.to_s == 'VirtualMachine'} | |
inventory = vm_inventory.to_h | |
inventory.each { |k, v| inventory[k] = v.to_hash } | |
@vmId = inventory.find{ |x| x[1]['name'] == vm_name }[1]['summary.vm'].to_s.split('"')[1] | |
break if [email protected]? | |
end | |
ticket = vim.serviceContent.sessionManager.AcquireCloneTicket | |
url = "https://#{vc}:7343/console/?vmId=#{@vmId}&vmName=#{vm_name}&host=#{vc}:443&sessionTicket=#{ticket}&thumbprint=#{thumbprint}" | |
url | |
end | |
def connect(url) | |
if RbConfig::CONFIG['host_os'] =~ /mswin|mingw|cygwin/ | |
system "start #{url}" | |
elsif RbConfig::CONFIG['host_os'] =~ /darwin/ | |
system "open \"#{url}\"" | |
elsif RbConfig::CONFIG['host_os'] =~ /linux|bsd/ | |
system "xdg-open #{url}" | |
end | |
end | |
print "Username: " | |
username = gets.chomp | |
print "Password: " | |
password = STDIN.noecho(&:gets).chomp | |
puts "" | |
print "vcenter: " | |
vc = gets.chomp | |
print "vm name: " | |
vm_name = gets.chomp | |
thumbprint = get_thumbprint(vc) | |
url = build_url(username, password, vc, vm_name, thumbprint) | |
connect(url) | |
puts "Connection initiated. URL was #{url}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment