Executing a Program in a Virtual MachineYou can execute a program inside a virtual machine from your program using the Python API. The virtual machine must have Parallels Tools installed for this functionality to work. To execute a program, you need to perform the following steps:
Sample The following sample program demonstrates how to execute a batch file (C:\\123.bat) in Windows running in a virtual machine. To test the program, create a batch file with a simple instruction (e.g. creating a directory on the C: drive) and see if after executing the program, the directory is actually created. """ Executes a batch file in a Windows virtual machine. @param vm: An instance of the prlsdkapi.Vm class identifying the source virtual machine. """ def exec_batch(vm):
# Uncomment the desired "g_login = " and "g_password = " lines below.
# Bind to an existing session. # Use this login to run a console program as a # superuser (LocalSystem in Windows, root in Linux). # Use this exact login ID and leave the password blank. g_login = "531582ac-3dce-446f-8c26-dd7e3384dcf4" g_password = "" # Log in as a specific user (a new session will be created). #g_login = "your_user_name" #g_password = "your_password" # Bind to an existing GUI session. # The user must be logged in to the virtual machine. # Use this exact login ID and leave the password blank. #g_login = "4a5533a7-31c6-4d7a-a400-1f330dc57a9d" #g_password = "" # Create a StringList object to hold the program input parameters and populate it. hArgsList = prlsdkapi.StringList() hArgsList.add_item("cmd.exe") hArgsList.add_item("/C") hArgsList.add_item("C:\\123.bat") # Create an empty StringList object. # The object is passed to the Vm_guest.run_program() method and is used to # specify the list of environment variables and their values to add to the program # execution environment. In this sample, we are not adding any variables. # If you wish to add a variable, add it in the var_name=var_value format. hEnvsList = prlsdkapi.StringList() hEnvsList.add_item("") # Establish a user session with the virtual machine. # The Vm.login_in_guest() method returns a VmGuest object, which is # obtained from the Job using the Result.get_param() method. vm_guest = vm.login_in_guest(g_login, g_password).wait().get_param() # Run the program. try: vm_guest.run_program("cmd.exe", hArgsList, hEnvsList).wait() except prlsdkapi.PrlSDKError, e: print "Error: %s" % e # Logout from the virtual machine. vm_guest.logout().wait() |
||||
|