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

This is for discussing general topics about how to use VirtualBox.
Post Reply
fusion809
Posts: 4
Joined: 10. Aug 2015, 00:09

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

Post 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
socratis
Site Moderator
Posts: 27329
Joined: 22. Oct 2010, 11:03
Primary OS: Mac OS X other
VBox Version: VirtualBox+Oracle ExtPack
Guest OSses: Win(*>98), Linux*, OSX>10.5
Location: Greece

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

Post 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]]
Do NOT send me Personal Messages (PMs) for troubleshooting, they are simply deleted.
Do NOT reply with the "QUOTE" button, please use the "POST REPLY", at the bottom of the form.
If you obfuscate any information requested, I will obfuscate my response. These are virtual UUIDs, not real ones.
socratis
Site Moderator
Posts: 27329
Joined: 22. Oct 2010, 11:03
Primary OS: Mac OS X other
VBox Version: VirtualBox+Oracle ExtPack
Guest OSses: Win(*>98), Linux*, OSX>10.5
Location: Greece

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

Post 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.
Do NOT send me Personal Messages (PMs) for troubleshooting, they are simply deleted.
Do NOT reply with the "QUOTE" button, please use the "POST REPLY", at the bottom of the form.
If you obfuscate any information requested, I will obfuscate my response. These are virtual UUIDs, not real ones.
noteirak
Site Moderator
Posts: 5231
Joined: 13. Jan 2012, 11:14
Primary OS: Debian other
VBox Version: OSE Debian
Guest OSses: Debian, Win 2k8, Win 7
Contact:

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

Post by noteirak »

Moving to "Using VirtualBox" as this not belong into "VirtualBox API"
Hyperbox - Virtual Infrastructure Manager - https://apps.kamax.lu/hyperbox/
Manage your VirtualBox infrastructure the free way!
Post Reply