Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require 'rbvmomi'
- class String
- # Tokenizes a string, split on spaces and considers anything in single/doublequotes as one token
- # Requires a string, returns an array of strings
- def tokenize
- self.
- split(/\s(?=(?:[^'"]|'[^']*'|"[^"]*")*$)/).
- select {|s| not s.empty? }.
- map {|s| s.gsub(/(^ +)|( +$)|(^["']+)|(["']+$)/,'')}
- end
- end
- module Findvms
- # Find a VM (including Folders) in a Datacenter
- # Requires
- # :name => string to search for
- # Optional
- # :folder => RbVmomi::VIM::Folder, RbVmomi::VIM::Datacenter object to search within
- #
- # Returns Array of RbVmomi::VIM::VirtualMachine objects or nil
- #
- def find_vms(args = {})
- a = {
- :name => "",
- :folder => (defined?(self.vmFolder) ? self.vmFolder : nil ||
- defined?(self.rootFolder) ? self.rootFolder : nil ||
- self
- )
- }.merge(args)
- name = a[:name]
- folder = a[:folder]
- found = []
- folder.childEntity.each do |x|
- case x.class.to_s
- when "Folder"
- # puts "Searching Folder #{x.name}"
- ret = find_vms(:name => name, :folder => x)
- if ! ret.nil?
- ret.each do |r|
- if r.name.index(name)
- found << r
- # puts "Found"
- end
- end
- end
- when "VirtualMachine"
- # puts "Searching VM #{x.name}"
- if x.name.index(name)
- found << x
- end
- when "Datacenter"
- # puts "Searching DC #{x.name}"
- found += find_vms(:name => name, :folder => x.vmFolder)
- else
- # puts "# Unrecognized Entity " + x.class.to_s
- end
- end
- return found
- end
- end
- RbVmomi::VIM::Folder.class_eval do
- include Findvms
- end
- RbVmomi::VIM::Datacenter.class_eval do
- include Findvms
- end
- RbVmomi::VIM.class_eval do
- include Findvms
- end
- RbVmomi::VIM::GuestOperationsManager.class_eval do
- # Execute Program In Guest with output
- # Requires
- # :vm => RbVmomi::VIM::VirtualMachine
- # :local_user => string
- # :local_pass => string
- # :command => string
- # Outputs Hash
- # :exitCode => int return code of executed program
- # :stdout => string STDOUT of executed program
- # :stderr => string STDERR of executed program
- #
- def executeProgramInGuest(args = {})
- a = {
- :vm => nil,
- :local_user => "",
- :local_pass => "",
- :command => "",
- }.merge(args)
- vm = a[:vm]
- local_user = a[:local_user]
- local_pass = a[:local_pass]
- command = a[:command]
- bin, arg = command.tokenize
- arg ||= ""
- fail "VM Object Requred" if vm.nil?
- fail "VMWare Tools must be running on #{vm.name}" if vm.guest.toolsRunningStatus != "guestToolsRunning"
- case vm.guest.guestFamily
- when "linuxGuest"
- workingdir = '/tmp'
- outfile = "#{workingdir}/toolsout"
- arg += " 2>#{outfile}.stderr >#{outfile}.stdout"
- when "windowsGuest"
- workingdir = 'C:\\'
- outfile = "C:\\Users\\#{local_user}\\toolsout"
- arg = "-command \"#{bin} #{arg} 2>#{outfile}.stderr >#{outfile}.stdout"
- bin = "C:\\Windows\\SysWOW64\\WindowsPowerShell\\v1.0\\powershell.exe"
- else
- fail "Unknown VM Type: #{vm.guest.guestFamily}"
- end
- spec = RbVmomi::VIM::GuestProgramSpec(:programPath => bin, :arguments => arg, :workingDirectory => workingdir)
- auth = RbVmomi::VIM::NamePasswordAuthentication(:interactiveSession => false, :username => local_user, :password => local_pass)
- pid = processManager.StartProgramInGuest(:vm => vm, :auth => auth, :spec => spec)
- while processManager.ListProcessesInGuest(:vm => vm, :auth => auth, :pids => [ pid.to_s ])[0].exitCode == nil
- sleep 1
- end
- outhash = {}
- outhash[:exitCode] = processManager.ListProcessesInGuest(:vm => vm, :auth => auth, :pids => [ pid.to_s ])[0].exitCode
- [:stdout, :stderr].each do |out|
- file = fileManager.InitiateFileTransferFromGuest(:vm => vm, :auth => auth, :guestFilePath => "#{outfile}.#{out}")
- warn "Warning: File #{out} over 1MB" if file.size > 1000000
- uri = URI.parse(file.url)
- http = Net::HTTP.new(uri.host, uri.port)
- http.use_ssl = true
- http.open_timeout = 5
- http.read_timeout = 5
- http.verify_mode = OpenSSL::SSL::VERIFY_NONE
- outhash[out] = http.request(Net::HTTP::Get.new(uri.request_uri)).body
- outhash[out].encode!(Encoding.find('UTF-8'), :binary => true, :invalid => :replace, :undef => :replace, :replace => '')
- outhash[out].delete!("\u0000")
- outhash[out].delete!("\r")
- end
- outhash
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement