|
| 1 | +import argparse |
| 2 | +import json |
| 3 | +import multiprocessing |
| 4 | +import os |
| 5 | +import signal |
| 6 | +import time |
| 7 | +from tqdm import tqdm |
| 8 | +import vm_automation |
| 9 | + |
| 10 | +WINDOWS_REQUIRED = "Win" |
| 11 | +UAC_ENABLE_COMMAND = ['cmd.exe', |
| 12 | + '/k', |
| 13 | + '%SystemRoot%\System32\\reg.exe', |
| 14 | + 'ADD', |
| 15 | + 'HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System', |
| 16 | + '/v', |
| 17 | + 'EnableLUA', |
| 18 | + '/t', |
| 19 | + 'REG_DWORD', |
| 20 | + '/d', |
| 21 | + '1', |
| 22 | + '/f'] |
| 23 | +DISABLE_SMB1_COMMAND = ['cmd.exe', |
| 24 | + '/k', |
| 25 | + '%SystemRoot%\System32\\reg.exe', |
| 26 | + 'ADD', |
| 27 | + 'HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\LanmanServer\Parameters', |
| 28 | + '/v', |
| 29 | + 'SMB1', |
| 30 | + '/t', |
| 31 | + 'REG_DWORD', |
| 32 | + '/d', |
| 33 | + '0', |
| 34 | + '/f'] |
| 35 | + |
| 36 | + |
| 37 | + |
| 38 | +def get_vm_server(config_file): |
| 39 | + if os.path.isfile(config_file): |
| 40 | + with open(config_file) as config_file_handle: |
| 41 | + config_map = json.load(config_file_handle) |
| 42 | + if config_map['HYPERVISOR_TYPE'].lower() == "esxi": |
| 43 | + vmServer = vm_automation.esxiServer.createFromConfig(config_map, 'esxi_autoamtion.log') |
| 44 | + vmServer.connect() |
| 45 | + if config_map['HYPERVISOR_TYPE'].lower() == "workstation": |
| 46 | + vmServer = vm_automation.workstationServer(config_map, 'workstation_automation.log') |
| 47 | + return vmServer |
| 48 | + return None |
| 49 | + |
| 50 | + |
| 51 | +def enable_uac(vm_config, vm_name, command): |
| 52 | + schedule_delay = 30 |
| 53 | + vm_server = get_vm_server(config_file=vm_config) |
| 54 | + vm_server.enumerateVms() |
| 55 | + for vm in vm_server.vmList: |
| 56 | + if vm_name == vm.vmName: |
| 57 | + vm.powerOn() |
| 58 | + vm_ready = False |
| 59 | + while vm_ready is False: |
| 60 | + vm_ready = vm_server.waitForVmsToBoot([vm]) |
| 61 | + vm.setUsername('vagrant') |
| 62 | + vm.setPassword('vagrant') |
| 63 | + vm.runCmdOnGuest(command) |
| 64 | + time.sleep(schedule_delay) |
| 65 | + vm.vmObject.ShutdownGuest() |
| 66 | + time.sleep(10) |
| 67 | + vm.powerOff() |
| 68 | + |
| 69 | + |
| 70 | +def main(): |
| 71 | + parser = argparse.ArgumentParser() |
| 72 | + parser.add_argument("-k", "--keyword", help="VM search parameter") |
| 73 | + parser.add_argument("-a", "--action", help="action [enable_uac|disable_smb1]") |
| 74 | + parser.add_argument("hypervisorConfig", help="json hypervisor config") |
| 75 | + |
| 76 | + args = parser.parse_args() |
| 77 | + |
| 78 | + validActions = ['enable_uac', 'disable_smb1'] |
| 79 | + |
| 80 | + prefix = args.keyword |
| 81 | + |
| 82 | + if args.action.lower() not in validActions: |
| 83 | + print('INVALID ACTION') |
| 84 | + if args.action.lower() == 'enable_uac': |
| 85 | + command = UAC_ENABLE_COMMAND |
| 86 | + elif args.action.lower() == 'disable_smb1': |
| 87 | + command = UAC_ENABLE_COMMAND |
| 88 | + |
| 89 | + vm_server = get_vm_server(config_file=args.hypervisorConfig) |
| 90 | + if vm_server is None: |
| 91 | + print ("Failed to connect to VM environment") |
| 92 | + exit(1) |
| 93 | + |
| 94 | + vm_list = [] |
| 95 | + vm_server.enumerateVms() |
| 96 | + for vm in vm_server.vmList: |
| 97 | + if prefix in vm.vmName and WINDOWS_REQUIRED in vm.vmName: |
| 98 | + vm_list.append(vm.vmName) |
| 99 | + |
| 100 | + original_sigint_handler = signal.signal(signal.SIGINT, signal.SIG_IGN) |
| 101 | + |
| 102 | + pool = None |
| 103 | + try: |
| 104 | + pool = multiprocessing.Pool(3) |
| 105 | + |
| 106 | + signal.signal(signal.SIGINT, original_sigint_handler) |
| 107 | + |
| 108 | + results = [] |
| 109 | + for vm_name in vm_list: |
| 110 | + pool.apply_async(enable_uac, [args.hypervisorConfig, vm_name, command], callback=results.append) |
| 111 | + |
| 112 | + with tqdm(total=len(vm_list)) as progress: |
| 113 | + current_len = 0 |
| 114 | + while len(results) < len(vm_list): |
| 115 | + if (len(results) > current_len): |
| 116 | + progress.update(len(results) - current_len) |
| 117 | + current_len = len(results) |
| 118 | + time.sleep(5) |
| 119 | + progress.update(len(results)) |
| 120 | + |
| 121 | + except KeyboardInterrupt: |
| 122 | + print("User cancel received, terminating all task") |
| 123 | + if pool is not None: |
| 124 | + pool.terminate() |
| 125 | + |
| 126 | + print("Processing complete " + str(len(vm_list)) + " systems updated") |
| 127 | + if pool is not None: |
| 128 | + pool.close() |
| 129 | + pool.join() |
| 130 | + |
| 131 | + |
| 132 | + |
| 133 | +if __name__ == "__main__": |
| 134 | + main() |
0 commit comments