diff --git a/vmw_vm_ctrl-alt-f1 b/vmw_vm_ctrl-alt-f1 new file mode 100755 index 0000000..2115a83 --- /dev/null +++ b/vmw_vm_ctrl-alt-f1 @@ -0,0 +1,105 @@ +#!/usr/bin/env python3 +from pyVmomi import vim +import pyVim.connect +import atexit +import argparse +import siteconf + +site_conf = siteconf.read() + + +def get_args(): + parser = argparse.ArgumentParser( + description='Rename VM') + + parser.add_argument('-s', '--site', + required=True, + action='store', + help='name of the site to connect to') + + parser.add_argument('-v', '--vm', + required=True, + action='store', + help='vm to rename') + + args = parser.parse_args() + return args + + +def get_obj(content, vimtype, name): + """ + Return an object by name, if name is None the + first found object is returned + """ + obj = None + container = content.viewManager.CreateContainerView( + content.rootFolder, vimtype, True) + for c in container.view: + if name: + if c.name == name: + obj = c + break + else: + obj = c + break + + return obj + + +def main(): + args = get_args() + + if not args.site in site_conf: + print(f"site not found: {args.site}") + exit(1) + + host = site_conf[args.site]['vcenter'] + user = site_conf[args.site]['vcenter_user'] + passwd = site_conf[args.site]['vcenter_passwd'] + + si = pyVim.connect.SmartConnect( + host = host, + user = user, + pwd = passwd, + disableSslCertValidation = True + ) + atexit.register(pyVim.connect.Disconnect, si) + content = si.RetrieveContent() + + vm = get_obj(content, [vim.VirtualMachine], args.vm) + if not vm: + print("vm not found") + exit(1) + + if vm.runtime.powerState != vim.VirtualMachinePowerState.poweredOn: + print("VM is not powered on.") + exit(1) + + key_modifier = vim.UsbScanCodeSpecModifierType() + key_modifier.leftAlt = True + key_modifier.leftControl = True + key_modifier.leftGui = False + key_modifier.leftShift = False + key_modifier.rightAlt = False + key_modifier.rightControl = False + key_modifier.rightGui = False + key_modifier.rightShift = False + + key_event = vim.UsbScanCodeSpecKeyEvent() + key_event.modifiers = key_modifier + # F1 0x3a + # F2 0x3b + # F12 0x45 + # DELETE 0x4c + key_event.usbHidCode = 0x3a << 16 | 0o0007 + + usb_spec = vim.UsbScanCodeSpec() + usb_spec.keyEvents.append(key_event) + + vm.PutUsbScanCodes(usb_spec) + + +if __name__ == "__main__": + main() + +# vim: set tabstop=4 shiftwidth=4 expandtab smarttab: