VBoxManage – How to Pass Shell Arguments with VBoxManage Guestcontrol

bashcommand lineguest-additionsscriptsvirtualbox

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.

Best Answer

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
Related Question