{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import os\n", "import re\n", "import sys\n", "import uuid\n", "\n", "import subprocess\n", "from datetime import datetime\n", "from functools import partial\n", "from subprocess import CalledProcessError\n", "\n", "\n", "def native_cmd(cmd):\n", " \"\"\"Returns the output of a native command on a give system.\n", " \n", " NOTE: Results may contain white space charaters at end of lines.\n", " \"\"\"\n", " return subprocess.check_output(cmd, shell=True).decode()\n", "\n", "# native_cmd(\"echo %USERPROFILE%\")\n", "\n", "# os.path.expandvars('%USERPROFILE%')\n", "\n", "# os.path.expandvars('%PROGRAMFILES%')\n", "\n", "def use_nt_exe(exe_path, cmd):\n", " \"\"\"Return output of an exe program given the path and command\n", " NOTE: For using Windows systems.\"\"\"\n", " return native_cmd('\"{}\" '.format(exe_path)+cmd)\n", "\n", "\n", "def load_nt_exe(path):\n", " \"\"\"Return a partial function form an exe path.\n", " \"\"\"\n", " return partial(use_nt_exe, path)\n", "\n", "\n", "if os.name == 'nt':\n", " vbman_path = os.path.join(\"%PROGRAMFILES%\", \"Oracle\", \"VirtualBox\", \"VBoxManage.exe\")\n", " vbman_path = os.path.expandvars(vbman_path)\n", "\n", " if os.path.exists(vbman_path):\n", " vbman = load_nt_exe(vbman_path)\n", " vbman.__doc__ = vbman('')\n", " print(vbman('list vms'))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "vbman()" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# vbman_man = vbman('').replace('\\r', '')\n", "# print(vbman_man)\n", "\n", "# res = re.findall('Commands:(.+)', vbman_man, flags=re.DOTALL)[0]\n", "\n", "# res = list(filter(lambda x:len(x)>0, res.split('\\n')))\n", "\n", "# cmds = []\n", "# n = 0\n", "# for i in res[1:]:\n", "# if i[:2] == ' ' and n == 0:\n", "# cmds.append(i)\n", "# # Stop as soon as a line without two spaces is found.\n", "# else:\n", "# n+=1\n", "\n", "# # list(filter(lambda x:x[:3] != ' ', cmds))\n", "# for i in cmds:\n", "# if i[:3] != ' ':\n", "# cmd_str = i.strip().split()[0]\n", "# print(cmd_str)\n", " \n", "\n", "# class VBMan(object):\n", "# def __init__(self):\n", "# pass\n", "# setattr(VBMan, 'thing', 'hello')\n", "# VBMan.thing" ] }, { "cell_type": "code", "execution_count": 87, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['D:']" ] }, "execution_count": 87, "metadata": {}, "output_type": "execute_result" } ], "source": [ "re.split(':\\s', 'D:')" ] }, { "cell_type": "code", "execution_count": 95, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# def showvninfo(name, **kwargs):\n", "# cmd=\"showvminfo {} --details\".format(name)\n", "# result = vbman(cmd)\n", "# if '\\r' in result:\n", "# result.replace('\\r', '')\n", "# return result\n", "# print(showvninfo('win7ult_master_v0'))" ] }, { "cell_type": "code", "execution_count": 102, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'2D Video Acceleration': 'off',\n", " '3D Acceleration': 'off',\n", " 'ACPI': 'on',\n", " 'APIC': 'on',\n", " 'Allow Tracing to Access VM': 'off',\n", " 'Audio capture': 'disabled',\n", " 'Audio playback': 'disabled',\n", " 'Autostart Delay': '0',\n", " 'Autostart Enabled': 'off',\n", " 'BIOS APIC mode': 'APIC',\n", " 'Bandwidth groups': '',\n", " 'Boot Device (1)': 'Floppy',\n", " 'Boot Device (2)': 'DVD',\n", " 'Boot Device (3)': 'HardDisk',\n", " 'Boot Device (4)': 'Not Assigned',\n", " 'Boot menu mode': 'message and menu',\n", " 'CPU exec cap': '100%',\n", " 'CPUID Portability Level': '0',\n", " 'CPUID overrides': 'None',\n", " 'Capture FPS': '25',\n", " 'Capture dimensions': '1024x768',\n", " 'Capture file': 'D:\\\\virtual_machines(windows)\\\\win7ult_master_v0\\\\win7ult_master_v0.webm',\n", " 'Capture options': 'ac_enabled=false',\n", " 'Capture rate': '512 kbps',\n", " 'Capture screens': '0',\n", " 'Capturing': 'not active',\n", " 'Chipset': 'piix3',\n", " 'Clipboard Mode': 'disabled',\n", " 'Config file': 'D:\\\\virtual_machines(windows)\\\\win7ult_master_v0\\\\win7ult_master_v0.vbox',\n", " 'Configured memory balloon size': '0 MB',\n", " 'Default Frontend': '',\n", " 'Drag and drop Mode': 'disabled',\n", " 'EHCI': 'disabled',\n", " 'Effective Paravirt. Provider': 'HyperV',\n", " 'Firmware': 'BIOS',\n", " 'Groups': '/',\n", " 'Guest OS': 'Windows 7 (64-bit)',\n", " 'HPET': 'off',\n", " 'Hardw. virt.ext': 'on',\n", " 'Hardware UUID': '38cb7060-59e4-4225-83e4-6be58b335765',\n", " 'IOAPIC': 'on',\n", " 'Keyboard Device': 'PS/2 Keyboard',\n", " 'LPT 1': 'disabled',\n", " 'LPT 2': 'disabled',\n", " 'Large Pages': 'on',\n", " 'Log folder': 'D:\\\\virtual_machines(windows)\\\\win7ult_master_v0\\\\Logs',\n", " 'Long Mode': 'on',\n", " 'Memory size': '8320MB',\n", " 'Monitor count': '1',\n", " 'NIC 2': 'disabled',\n", " 'NIC 3': 'disabled',\n", " 'NIC 4': 'disabled',\n", " 'NIC 5': 'disabled',\n", " 'NIC 6': 'disabled',\n", " 'NIC 7': 'disabled',\n", " 'NIC 8': 'disabled',\n", " 'Name': 'win7ult_master_v0',\n", " 'Nested Paging': 'on',\n", " 'Number of CPUs': '4',\n", " 'PAE': 'on',\n", " 'Page Fusion': 'off',\n", " 'Paravirt. Provider': 'Default',\n", " 'Pointing Device': 'PS/2 Mouse',\n", " 'RTC': 'local time',\n", " 'SATA (1, 0)': 'Empty',\n", " 'Shared folders': '',\n", " 'Snapshot folder': 'D:\\\\virtual_machines(windows)\\\\win7ult_master_v0\\\\Snapshots',\n", " 'State': 'powered off (since 2017-11-21T15:55:31.000000000)',\n", " 'Storage Controller Bootable (0)': 'on',\n", " 'Storage Controller Instance Number (0)': '0',\n", " 'Storage Controller Max Port Count (0)': '30',\n", " 'Storage Controller Name (0)': 'SATA',\n", " 'Storage Controller Port Count (0)': '30',\n", " 'Storage Controller Type (0)': 'IntelAhci',\n", " 'Teleporter Address': '',\n", " 'Teleporter Enabled': 'off',\n", " 'Teleporter Password': '',\n", " 'Teleporter Port': '0',\n", " 'Time offset': '0ms',\n", " 'Tracing Configuration': '',\n", " 'Tracing Enabled': 'off',\n", " 'Triple Fault Reset': 'off',\n", " 'UART 1': 'disabled',\n", " 'UART 2': 'disabled',\n", " 'UART 3': 'disabled',\n", " 'UART 4': 'disabled',\n", " 'USB': 'disabled',\n", " 'UUID': 'd33cdcb8-376c-469e-9d0c-8ca7839fb896',\n", " 'VRAM size': '8MB',\n", " 'VRDE': 'disabled',\n", " 'VT-x VPID': 'on',\n", " 'VT-x unr. exec.': 'on',\n", " 'X2APIC': 'off',\n", " 'XHCI': 'disabled'}" ] }, "execution_count": 102, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def showvminfo_dict(name):\n", " cmd=\"showvminfo {} --details\".format(name)\n", " result = vbman(cmd)\n", " if '\\r' in result:\n", " result = result.replace('\\r', '')\n", " result = result.split('\\n')\n", " result = list(map(lambda x:re.split(':\\s', x), result))\n", " result = [list(map(lambda x: x.strip(), i)) for i in result]\n", " result = list(filter(lambda x:len(x)==2, result))\n", " result = list(filter(lambda x:type(x)==list, result))\n", " result = dict(result)\n", " return result\n", "\n", "showvminfo_dict('win7ult_master_v0')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "---" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\"Virtual machine 'win7ult_master_v0' is created and registered.\\r\\nUUID: d33cdcb8-376c-469e-9d0c-8ca7839fb896\\r\\nSettings file: 'D:\\\\virtual_machines(windows)\\\\win7ult_master_v0\\\\win7ult_master_v0.vbox'\\r\\n\"" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Create and register the vbox.\n", "vbman('createvm --name \"win7ult_master_v0\" --ostype Windows7_64 --register')" ] }, { "cell_type": "code", "execution_count": 45, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "''" ] }, "execution_count": 45, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Create the SATA controller\n", "vbman('storagectl win7ult_master_v0 --name SATA --add sata --controller IntelAHCI')" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Medium created. UUID: 663ff20b-4de0-4fc0-b64a-f1008e48e413\\r\\n'" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Create the blank 64gb vdi\n", "vbman('createmedium --filename \"D:\\\\virtual_machines(windows)\\\\win7ult_master_v0\\\\win7ult_master_v0.vdi\" --size 64000')" ] }, { "cell_type": "code", "execution_count": 57, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "''" ] }, "execution_count": 57, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Attach the blank vdi\n", "vbman('storageattach win7ult_master_v0 --storagectl SATA --port 0 --type hdd --medium 663ff20b-4de0-4fc0-b64a-f1008e48e413')" ] }, { "cell_type": "code", "execution_count": 59, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "''" ] }, "execution_count": 59, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Attach install dvd\n", "vbman('storageattach win7ult_master_v0 --storagectl SATA --port 1 --type dvddrive --medium emptydrive')" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "new_mem_size = 128*65\n", "\n", "# Set memory size.\n", "vbman('modifyvm win7ult_master_v0 --memory {}'.format(new_mem_size))" ] }, { "cell_type": "code", "execution_count": 73, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "''" ] }, "execution_count": 73, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Set number of Processors.\n", "vbman('modifyvm win7ult_master_v0 --cpus 4')" ] }, { "cell_type": "code", "execution_count": 103, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "''" ] }, "execution_count": 103, "metadata": {}, "output_type": "execute_result" } ], "source": [ "new_vram = 64\n", "\n", "# Set Video Memory size.\n", "vbman('modifyvm win7ult_master_v0 --vram {}'.format(new_vram))" ] }, { "cell_type": "code", "execution_count": 97, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "4\n", "8320MB\n" ] } ], "source": [ "vm_info = showvminfo_dict(\"win7ult_master_v0\")\n", "print(vm_info[\"Number of CPUs\"])\n", "print(vm_info['Memory size'])" ] }, { "cell_type": "code", "execution_count": 99, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'D:\\\\virtual_machines(windows)\\\\win7ult_master_v0'" ] }, "execution_count": 99, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def get_vm_loc(machine_name):\n", " cpath = showvminfo_dict(machine_name)[\"Config file\"]\n", " vm_loc = os.path.split(cpath)[0]\n", " return vm_loc\n", "# get_vm_loc(\"win7ult_master_v0\")\n", "\n", "yes = {'yes','y'}\n", "no = {'no','n'}\n", "\n", "def change_hardware_uuid(machine_name):\n", " try:\n", " machineinfo = vm_info_dict(machine_name)\n", " except CalledProcessError:\n", " print('{} not found'.format(machine_name))\n", " \n", " current_hardware_uuid = machineinfo['Hardware UUID']\n", " new_hardware_uuid = str(uuid.uuid4())\n", " \n", " prompt_str = \"Sure want to change {} Hardware UUID?\".format(machine_name)\n", " choice = input(prompt=prompt_str).lower()\n", " if choice in yes:\n", " now = '{}'.format(datetime.now())\n", " chng_log = '{}: {} hardware uuid was changed from {} to {}\\n'.format(now, machine_name,\n", " current_hardware_uuid, new_hardware_uuid)\n", " chng_log_path = os.path.join(get_vm_loc(machine_name), 'hardware_uuid_change_log.txt')\n", " f = open(chng_log_path, 'a+')\n", " f.write(chng_log)\n", " f.close()\n", " cmd = 'modifyvm {} --hardwareuuid {}'.format(machineinfo['UUID'], new_hardware_uuid)\n", " vbman(cmd)\n", " print('hardware uuid has been changed to {}'.format(new_hardware_uuid))\n", " if choice in no:\n", " print('operation aborted')\n", " pass\n", " else:\n", " pass\n", " return\n", "\n", "change_hardware_uuid('win7ult_master_v0')" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.3" } }, "nbformat": 4, "nbformat_minor": 2 }