{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Sure want to change win10pro_v1 Hardware UUID?y\n", "hardware uuid has been changed to ce3e10b2-8957-4d5d-941a-e45779a31054\n" ] } ], "source": [ "import uuid\n", "import re\n", "import os\n", "import subprocess\n", "from subprocess import CalledProcessError\n", "from datetime import datetime\n", "\n", "def linux_cmd(cmd):\n", " return subprocess.check_output(cmd, shell=True).decode()\n", "# linux_cmd(\"echo 'hello'\")\n", "\n", "def vms2dict():\n", " cmd=\"VBoxManage list vms\"\n", " result = linux_cmd(cmd)\n", " res = list(map(lambda x: x.split(), result.split('\\n')[:-1]))\n", " cut = lambda x:x[1:-1]\n", " res = dict([list(map(lambda x:cut(x), i)) for i in res])\n", " return res\n", "\n", "def vm_info_dict(name):\n", " cmd=\"VBoxManage showvminfo {} --details\".format(name)\n", " result = linux_cmd(cmd).split('\\n')\n", " result = list(map(lambda x:x.split(':'),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", "def get_vm_loc(machine_name):\n", " cpath = vm_info_dict(machine_name)[\"Config file\"]\n", " vm_loc = os.path.split(cpath)[0]\n", " return vm_loc\n", "\n", "yes = {'yes','y'}\n", "no = {'no','n'}\n", "\n", "def identical_clone(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 = 'VBoxManage modifyvm {} --hardwareuuid {}'.format(machineinfo['UUID'], new_hardware_uuid)\n", " linux_cmd(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", "identical_clone('win10pro_v1')" ] }, { "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" }, "varInspector": { "cols": { "lenName": 16, "lenType": 16, "lenVar": 40 }, "kernels_config": { "python": { "delete_cmd_postfix": "", "delete_cmd_prefix": "del ", "library": "var_list.py", "varRefreshCmd": "print(var_dic_list())" }, "r": { "delete_cmd_postfix": ") ", "delete_cmd_prefix": "rm(", "library": "var_list.r", "varRefreshCmd": "cat(var_dic_list()) " } }, "types_to_exclude": [ "module", "function", "builtin_function_or_method", "instance", "_Feature" ], "window_display": false } }, "nbformat": 4, "nbformat_minor": 2 }