Advertisement
Guest User

Install virtualbox virtual machine import ova and setup share

a guest
Feb 21st, 2022
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Batch 4.05 KB | None | 0 0
  1. ::::::::::::::::::::::::::::::::::::::::::::
  2. :: Elevate.cmd - Version 4
  3. :: Automatically check & get admin rights
  4. :: see "https://stackoverflow.com/a/12264592/1016343" for description
  5. ::::::::::::::::::::::::::::::::::::::::::::
  6.  @echo off
  7.  CLS
  8.  ECHO.
  9.  ECHO =============================
  10.  ECHO Running Admin shell
  11.  ECHO =============================
  12.  
  13. :init
  14.  setlocal DisableDelayedExpansion
  15.  set cmdInvoke=1
  16.  set winSysFolder=System32
  17.  set "batchPath=%~0"
  18.  for %%k in (%0) do set batchName=%%~nk
  19.  set "vbsGetPrivileges=%temp%\OEgetPriv_%batchName%.vbs"
  20.  setlocal EnableDelayedExpansion
  21.  
  22. :checkPrivileges
  23.   NET FILE 1>NUL 2>NUL
  24.   if '%errorlevel%' == '0' ( goto gotPrivileges ) else ( goto getPrivileges )
  25.  
  26. :getPrivileges
  27.   if '%1'=='ELEV' (echo ELEV & shift /1 & goto gotPrivileges)
  28.   ECHO.
  29.   ECHO **************************************
  30.   ECHO Invoking UAC for Privilege Escalation
  31.   ECHO **************************************
  32.  
  33.   ECHO Set UAC = CreateObject^("Shell.Application"^) > "%vbsGetPrivileges%"
  34.   ECHO args = "ELEV " >> "%vbsGetPrivileges%"
  35.   ECHO For Each strArg in WScript.Arguments >> "%vbsGetPrivileges%"
  36.   ECHO args = args ^& strArg ^& " "  >> "%vbsGetPrivileges%"
  37.   ECHO Next >> "%vbsGetPrivileges%"
  38.  
  39.   if '%cmdInvoke%'=='1' goto InvokeCmd
  40.  
  41.   ECHO UAC.ShellExecute "!batchPath!", args, "", "runas", 1 >> "%vbsGetPrivileges%"
  42.   goto ExecElevation
  43.  
  44. :InvokeCmd
  45.   ECHO args = "/c """ + "!batchPath!" + """ " + args >> "%vbsGetPrivileges%"
  46.   ECHO UAC.ShellExecute "%SystemRoot%\%winSysFolder%\cmd.exe", args, "", "runas", 1 >> "%vbsGetPrivileges%"
  47.  
  48. :ExecElevation
  49.  "%SystemRoot%\%winSysFolder%\WScript.exe" "%vbsGetPrivileges%" %*
  50.  exit /B
  51.  
  52. :gotPrivileges
  53.  setlocal & cd /d %~dp0
  54.  if '%1'=='ELEV' (del "%vbsGetPrivileges%" 1>nul 2>nul  &  shift /1)
  55.  
  56.  ::::::::::::::::::::::::::::
  57. ::START
  58. ::::::::::::::::::::::::::::
  59. REM Run shell as admin (example) - put here code as you like
  60. start /wait "" "%~dp0VirtualBox-6.1.32-r149290.msi" /passive
  61. echo y | "%programfiles%\Oracle\VirtualBox\VBoxManage.exe" extpack install --replace "%~dp0Oracle_VM_VirtualBox_Extension_Pack-6.1.32-149290.vbox-extpack"
  62. start /wait "" "%programfiles%\Oracle\VirtualBox\VBoxManage.exe" import "%~dp0antiX-21_x64-base.ova"
  63. start /wait "" "%~dp0share_setup.exe"
  64. REM End of batch file
  65.  
  66. _________________________________________________________________
  67.  
  68. share_setup.exe python code
  69.  
  70. import glob
  71. import os
  72. import subprocess
  73. import time
  74.  
  75.  
  76. def edit_file(file_loc, search_string, replace_string):
  77.     try:
  78.         with open(file_loc, 'r') as tmp_f:
  79.             tmp_data = str(tmp_f.read()).replace(search_string, replace_string)
  80.         with open(file_loc, 'w') as tmp_f:
  81.             tmp_f.write(tmp_data)
  82.     except Exception as e:
  83.         print('Error in editing file: ' + str(e))
  84.  
  85.  
  86. def find_file_by_string(dir_loc, infile_string):
  87.     file_paths = []
  88.     try:
  89.         for i in glob.iglob(dir_loc + '/**/*', recursive=True):
  90.             if os.path.isfile(i):
  91.                 if str(i).__contains__(infile_string):
  92.                     file_paths.append(i)
  93.     except Exception as e:
  94.         print('Error in finding file: ' + str(e))
  95.     return file_paths
  96.  
  97.  
  98. def main_run():
  99.     query = 'echo | set /p dummyName="%userprofile%\VirtualBox VMs"'
  100.     response = subprocess.Popen(query, shell=True, stdout=subprocess.PIPE).stdout.read().decode('utf-8').strip()
  101.     vbox_config_loc = find_file_by_string(response, 'antiX-21_x64-base.vbox')[0]
  102.     print('found file location: ' + vbox_config_loc)
  103.     if os.path.isfile(vbox_config_loc):
  104.         replace_string = str(os.path.abspath(os.getcwd() + "/../../"))
  105.         print('new share location: ' + replace_string)
  106.         edit_file(vbox_config_loc, '/media/ramdisk', replace_string)
  107.     time.sleep(3)
  108.  
  109.  
  110. if __name__ == '__main__':
  111.     print('changing default share location, up two levels from current file location...')
  112.     main_run()
  113.  
  114. _________________________________________________________________
  115.  
  116. the only thing that is missing is "accept dialog" during virtualbox install driver setup, tweak it yourself :)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement