Previous page

Next page

Locate page in Contents

Executing a Program in a Virtual Machine

You 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:

  1. Log in to a virtual machine. You have three choices here (the details are described in the code sample below):
    • If you want to execute a console program as a specific user, you can do so by supplying the user ID and password. A new session will be created in a virtual machine.
    • If you want to execute a console program as a superuser (root, LocalSystem), you can use a login ID that is predefined in the API for this purpose.
    • If you want to run a GUI application, you need to use a predefined login ID that's used to bind to an existing GUI session in a virtual machine. For this login type to work, the virtual machine must be running and a user must be logged in to it. Please note that after you launch a GUI application, you cannot control it. For instance, if the application requires user interaction, you cannot directly control it from your Python program. If you need to interact with a GUI program, use the Remote Desktop Access functionality.
  2. Create an  object to hold the program input parameters and populate it.
  3. Execute the program.
  4. Evaluate the results. Please note that you cannot obtain the results of the program execution directly. You can only determine whether the program executed successfully or not.

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()