Page 1 of 1

Is it possible to run commands in a VM from the host's CLI?

Posted: 12. Feb 2016, 09:13
by fusion809
Hi,

I have several Linux guest machines and are presently using openSUSE Tumbleweed as my host. I am interested in being able to run commands on my VMs from my host's command-line. I know I can start a VM from the command-line with:

Code: Select all

VBoxManage startvm "VM name" --type headless
but is it possible to execute commands within the VM from my host's command-line? For example, I would like to run

Code: Select all

apt-get update && apt-get upgrade
in my Debian 8.3 VM from the command-line of my openSUSE Tumbleweed host. As this would allow me to write Bash scripts to automatically update all my guest VMs using their respective package managers.

Thanks for your time,
Brenton

Re: Is it possible to run commands in a VM from the host's C

Posted: 12. Feb 2016, 09:42
by socratis
Take a look at Ch. 8.31 VBoxManage guestcontrol and see if that suits your needs. One of the sub-commands (run) goes like:
VBoxManage guestcontrol <uuid|vmname> run [common-options]
            [--exe <path to executable>] [--timeout <msec>]
            [-E|--putenv <NAME>[=<VALUE>]] [--unquoted-args]
            [--ignore-operhaned-processes] [--no-profile]
            [--no-wait-stdout|--wait-stdout]
            [--no-wait-stderr|--wait-stderr]
            [--dos2unix] [--unix2dos]
            -- <program/arg0> [argument1] ... [argumentN]]

Re: Is it possible to run commands in a VM from the host's C

Posted: 12. Feb 2016, 10:49
by socratis
OK, I tried to test my advice and here's an example that (sort of) worked.
VBoxManage guestcontrol "Mint" run -v --username root --password # -- "/usr/bin/apt-get" - update
Ign http://archive.canonical.com trusty InRelease
Get:1 http://security.ubuntu.com trusty-security InRelease [65.9 kB]
Ign http://archive.ubuntu.com trusty InRelease
Get:2 http://archive.canonical.com trusty Release.gpg [933 B]
...

VBoxManage guestcontrol "Mint" run -v --username root --password # -- "/usr/bin/apt-get" - upgrade
Reading package lists...
Building dependency tree...
Reading state information...
The following packages will be upgraded:
  adobe-flashplugin apt apt-transport-https apt-utils base-files bind9-host
  cpp-4.8 dnsutils ecryptfs-utils firefox firefox-locale-en gcc-4.8
  gcc-4.8-base gcc-4.8-base:i386 gcc-4.9-base gcc-4.9-base:i386
...
Note that I had to use the "root" user because I couldn't get the specific command, "apt-get", to work otherwise. "ls" works as a normal user.

Oh, and BTW, the 2nd command (upgrade) never completed... :? I'd use "ssh" if I were you. It's more ... proven to work.

Re: Is it possible to run commands in a VM from the host's C

Posted: 12. Feb 2016, 22:51
by noteirak
Moving to "Using VirtualBox" as this not belong into "VirtualBox API"