Run commands on an Ubuntu guest in VirtualBox VM from Windows host

7,895

Here is the help from the vboxmanage command:

 guestcontrol              <uuid|vmname>

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

So the full command should be:

C:\Program Files\Oracle\VirtualBox\VBoxManage guestcontrol "Ubuntu" execute --username (myname) --password (mypassword) --verbose --wait-exit --wait-stdout --image "/usr/local/bin/oof2" -- --text

Note the extra --. Everything after that will be passed as arguments to the process.


Setting Up SSH

  1. Install PuTTY on the host machine. (Direct link to installer)
  2. Open a terminal in the VM and type ip -4 addr. Look for your IP address, it'll be next to the word "inet" under the "eth0" section (probably going to be 10.0.2.XXX)
  3. From the VM window, open the "Device" menu, and select "Network"->"Network Settings". From there select "Port Forwarding"
  4. Add a rule, Host Port = 2222, Guest IP = , Guest Port = 22. Click OK on all dialogs.
  5. Run putty, use localhost as the hostname, and 2222 as the port. Click "Open"

If that works and you get a terminal, you can now use this command to run your application:

C:\Program Files (x86)\PuTTY\plink.exe -P 2222 -pw mypassword myname@localhost  "/usr/local/bin/oof2" --text
Share:
7,895

Related videos on Youtube

Hogy88
Author by

Hogy88

Updated on September 18, 2022

Comments

  • Hogy88
    Hogy88 almost 2 years

    I am working on some coding, and I have an idea that should work but I have no clue how to make it work. I am using Wolfram Mathematica on my Windows host machine to run some code, but I have to export a file to a Linux-based program that I run in an Ubuntu Virtualbox VM. As of now, when the file is exported, I have to go over and click it to make it run.

    While it's not too much work, I'd like to have it automated so that Mathematica can call my other program and I don't have to click anything. I've done my best to look around online for help, but honestly it's mostly over my head (I'm a civil engineer, not really computer savvy).

    Here's what I've got so far:

    C:\Program Files\Oracle\VirtualBox\VBoxManage guestcontrol "Ubuntu" execute --username (myname) --password (mypassword) --verbose --wait-exit --wait-stdout --image "/home/(myname)/Desktop/oof2-2.1.11/oof2"
    

    It tells me VERR_ACCESS_DENIED and closes the guest session.

    When I run my program in Ubuntu, all I have to do is type oof2 in the terminal window and it starts up, but how do I get it to do this via the command line? I've read through the guest control manual for VirtualBox and I didn't see anything that I can understand that directly applies. Any help is appreciated.

    As a final note, I can run the examples given in the guest control manual and they work fine.

    • heavyd
      heavyd over 9 years
      If you login to the Ubuntu machine from VirtualBox can you execute the app using the full path /home/(myname)/Desktop/oof2-2.1.11/oof2?
    • Hogy88
      Hogy88 over 9 years
      Well, I actually cannot. It initially said "access denied", so I did sudo ..., and it said "command not found". How do I find out which path the terminal is using? Again, I just type "oof2" and it pops up.
    • heavyd
      heavyd over 9 years
      try which oof2
    • heavyd
      heavyd over 9 years
      oof2 --help or oof2 -h see if there is an option to pass it a script. But if you're trying to start a GUI application, you might have a hard time because VirtualBox is starting a new session for your user that wouldn't be associated with the current desktop.
    • Hogy88
      Hogy88 over 9 years
      I can type '/usr/local/bin/oof2 --text' into the Ubuntu terminal and it runs the text version, from which I can load a script (still in the terminal). I'm not sure how to make this work in the command line, though. I tried "--image '/usr/local/bin/oof2 --text'" and that didn't work. By the way, your help is appreciated more than you know. I've been working on this for several days now with little headway until the last hour.
    • heavyd
      heavyd over 9 years
      Take the --text out of the quotes: --image '/usr/local/bin/oof2' --text
    • Hogy88
      Hogy88 over 9 years
      Returns: Process started (next line) <-[1;24r<-[0;10m<-[4l<-[?7h<-[24;1HProcess terminated (end) where <- is an arrow. If I type in the script I want it to load after the --text, it says "error: Unknown option: --text"
  • Hogy88
    Hogy88 over 9 years
    Do you have any knowledge about Python curses? It's giving me '_curses.error: endwin() returned ERR Process terminated'
  • heavyd
    heavyd over 9 years
    I don't, but I'd assume it has to do with running it this way. Have you tried running it over SSH instead?
  • Hogy88
    Hogy88 over 9 years
    I have not, and I don't know what that is yet (sorry...if you imaging Homer Simpson trying to code, I'm pretty close)
  • heavyd
    heavyd over 9 years
    No problem, it's a way of connecting over the network instead of through virtual box. Is the VM connected to the network? If so which network type is selected in VB (NAT, bridged, etc.)?
  • Hogy88
    Hogy88 over 9 years
    It's a NAT connection.
  • heavyd
    heavyd over 9 years
    @Hogy88, I've update my answer with instructions for SSH.
  • Hogy88
    Hogy88 over 9 years
    Figured it out!!!! I'm so excited, it's unreal. The full code is C:\Program Files\Oracle\VirtualBox\VBoxManage guestcontrol "Ubuntu" execute --username (myname) --password (mypassword) --verbose --wait-exit --wait-stdout --image "/usr/local/bin/oof2" -- --batch --script="(script location)" This is great, my research will be worlds faster now that I can do this. Thank you so much for your help!!! Is there a way I can recommend you, that sort of thing? Not sure how this site works yet.