How to pass shell arguments with VBoxManage guestcontrol?

10,729

SSH

We can run an application on a virtual guest with an SSH session from the host to this machine. However this requires that networking was enabled, and that openssh-server was installed an runs on the guest machine.

VBoxManage guestcontrol

As an alternative we can use built-in features of Virtual Box to execute a program on a running guest VM. This can be done with VBoxManage guestcontrol.

The example line below will just run ls on the virtual machine's root:

VBoxManage --nologo guestcontrol "<vm_name>" run --exe "/bin/ls" --username <guestuser> --password <password> --wait-stdout

Running a graphical application on the guest requires us to define the DISPLAY environment variable to the guest with the option --putenv. Next example will run and open gedit on the guest:

VBoxManage --nologo guestcontrol "<vm_name" run --exe "/usr/bin/gedit" --username <guestuser> --password <password> --putenv "DISPLAY=:0" --wait-stdout

We can also pass options to open a program. Next example will open a file vmtest in the guest gedit:

VBoxManage --nologo guestcontrol "vm_name" run --exe "/usr/bin/gedit" --username <guestuser> --password <password> --putenv "DISPLAY=:0" --wait-stdout -- gedit/arg0 vmtest

Options and arguments are separated from the command with -- as can best be seen in below example of a script from the host.


Example host script

Below script will play an example.ogg file using paplay in a guest machine when run on the host. Replace the variables with appropriate values.

#!/bin/bash

VM_NAME=myvm
VM_USER=takkat
VM_PASSWD=topsecret
VM_EXEC=paplay
VM_EXEC_PATH=/usr/bin/paplay
VM_ARGS=/home/takkat/Music/example.ogg

VBoxManage --nologo guestcontrol $VM_NAME run --exe $VM_EXEC_PATH \
--username $VM_USER --password $VM_PASSWD --wait-stdout \
-- {$VM_EXEC}/arg0 $VM_ARGS
Share:
10,729

Related videos on Youtube

Googlebot
Author by

Googlebot

intentionally left blank

Updated on September 18, 2022

Comments

  • Googlebot
    Googlebot over 1 year

    I run a command from the host machine on the guest machine (both Ubuntu) as

    VBoxManage guestcontrol Ubuntu1 run --exe "script.sh" --username xx --password xx --wait-stdout
    

    where the shell script on the guest machine is as

    #!/bin/bash
    echo $1
    

    How can I pass the argument 1 while executing the shell script?

    I assumed it should work as

    run --exe "script.sh arg1"
    

    but it does not.

  • Googlebot
    Googlebot about 7 years
    Sorry, I got lost. Where do you argv1 in guestcontrol command?
  • Takkat
    Takkat about 7 years
    @All it is named arg0. In the gedit example line it is -- gedit/arg0 then vmtest to open a file vmtest in the guest's gedit, in the example script it is the variable VM_ARGS that plays an ogg file in the guest and from the guest hard drive. Remember to always give full paths.
  • Googlebot
    Googlebot about 7 years
    Wonderful! Works like a charm.
  • blackwood_s
    blackwood_s almost 5 years
    above and beyond, wish I had more than one vote to give.
  • Draque Thompson
    Draque Thompson over 4 years
    This is great! Thanks for the wonderfully detailed reply you put together!