How to launch a script inside a VM from the host?

Environment presentation:

  • Host Machine: Ubuntu 12.04 LTS
  • Guest Machine: Ubuntu 12.04 LTS. Inside this VM, a my_program.py exists.

  • Virtualization system: VirtualBox 4.3

Question:

Is there any way to launch my_program.py from the host machine ?


Solution 1:

You can use Vboxmanage to do this. The form of the command is

VBoxManage guestcontrol <uuid|vmname> exec[ute]
            --image <path to program> --username <name>
            [--passwordfile <file> | --password <password>]
            [--environment "<NAME>=<VALUE> [<NAME>=<VALUE>]"]
            [--verbose] [--timeout <msec>]
            [--wait-exit] [--wait-stdout] [--wait-stderr]
            [--dos2unix] [--unix2dos]
            -- [[<argument1>] ... [<argumentN>]]

To run your python script, you can enter the following - stdout and stderr are returned to the host machine by this command

VBoxManage guestcontrol "Name of Virtual Machine Goes Here" exec --image /path/to/my/script --username UserNameGoesHere --password PasswordGoesHere --wait-exit --wait-stdout --wait-stderr

In my case the test script was

#!/usr/bin/python
print "hello"

You can find information about this by starting the VirtualBox program, and selecting Help from the menu, and the specific section you are looking for is 8.31. VBoxManage guestcontrol

Solution 2:

The easiest way to launch an app in guest is to send a keyboard key press to guest from host. In the guest the app is setuped to launch on a key press (Using settings -> keyboard shortcuts or ccsm or any other ways). From a terminal or from a script we send the key press to guest, on listening that guest OS launch the program/app.

To send a key press use the VBoxManage's option keyboardputscancode
For example
VBoxManage controlvm [name] keyboardputscancode 1d 38 e0 53 will send Ctrl + Alt + Delete and VBoxManage controlvm [name] keyboardputscancode 38 24 will send Alt + J.

The advantage of this method is you don't need to install any software, even guest additions. You don't need to setup any network, ssh, passwords etc..
Scan codes for a key can be found here.