Execute process inside VM

Discussion about using the VirtualBox API, Tutorials, Samples.
noteirak
Site Moderator
Posts: 5229
Joined: 13. Jan 2012, 11:14
Primary OS: Debian other
VBox Version: OSE Debian
Guest OSses: Debian, Win 2k8, Win 7
Contact:

Re: Execute process inside VM

Post by noteirak »

The guest, that's where the login attempt is made
Hyperbox - Virtual Infrastructure Manager - https://apps.kamax.lu/hyperbox/
Manage your VirtualBox infrastructure the free way!
Bypus
Posts: 33
Joined: 26. Sep 2017, 11:52

Re: Execute process inside VM

Post by Bypus »

I can't find anything on the time i launched the script.
Do you think it could be because the host is in a private network (domain) and the guest is not ?
noteirak
Site Moderator
Posts: 5229
Joined: 13. Jan 2012, 11:14
Primary OS: Debian other
VBox Version: OSE Debian
Guest OSses: Debian, Win 2k8, Win 7
Contact:

Re: Execute process inside VM

Post by noteirak »

I can't speculate at this point. Are you able to login on the guest with the same credentials in a regular desktop session?
Hyperbox - Virtual Infrastructure Manager - https://apps.kamax.lu/hyperbox/
Manage your VirtualBox infrastructure the free way!
Bypus
Posts: 33
Joined: 26. Sep 2017, 11:52

Re: Execute process inside VM

Post by Bypus »

I'll try it on a personnal computer then.
What do you mean ? I have only one Administrator session on the VM (GUI talking) named "Admin" and password "test" that i can open. With the createSession() function (script talking) i use the same credentials (user = "Admin", password = "test")
noteirak
Site Moderator
Posts: 5229
Joined: 13. Jan 2012, 11:14
Primary OS: Debian other
VBox Version: OSE Debian
Guest OSses: Debian, Win 2k8, Win 7
Contact:

Re: Execute process inside VM

Post by noteirak »

is "Admin" the name of the user or the actual username that you use when loging in Windows? If yes, then you'll need to figure out why the security policy in Windows doesn't let you login.

A good starting point would be to ensure you can run the command using vboxmanage - See User Manual section.
It covers a lot of details on the requirements, pitfalls and so. If it works with vboxmanage, then the issue is still in the code. If it doesn't work with vboxmanage, your guest OS is not configured properly.
Hyperbox - Virtual Infrastructure Manager - https://apps.kamax.lu/hyperbox/
Manage your VirtualBox infrastructure the free way!
Bypus
Posts: 33
Joined: 26. Sep 2017, 11:52

Re: Execute process inside VM

Post by Bypus »

Ok so, i might have messed up with the GUI:

Code: Select all

C:\Program Files\Oracle\VirtualBox>vboxmanage.exe guestcontrol "W7 32-bit JAP" --username "Admin" --password "test" stat "c:\windows\explorer.exe"
VBoxManage.exe: error: The specified user was not able to logon on guest
VBoxManage.exe: error: Details: code VBOX_E_IPRT_ERROR (0x80bb0005), component GuestSessionWrap, interface IGuestSession, callee IUnknown
VBoxManage.exe: error: Context: "WaitForArray(ComSafeArrayAsInParam(aSessionWaitFlags), 30 * 1000, &enmWaitResult)" at line 938 of file VBoxManageGuestCtrl.cpp
Username "Admin" that was actually changed through GUI returns an error

Code: Select all

C:\Program Files\Oracle\VirtualBox>vboxmanage.exe guestcontrol "W7 32-bit JAP" --username "W7ProJapanesex86" --password "test" stat "c:\windows\explorer.exe"
Element "c:\windows\explorer.exe" found: Is a file
Original username "W7ProJapanesex86" returns something.
In the script, it still returns the error with the original username
Here is the code just in case:

Code: Select all

# -*- coding: utf-8 -*-
import vboxapi
import glob
import time

mgr = vboxapi.VirtualBoxManager(None, None)
vbox = mgr.vbox
machine = vbox.findMachine("W7 32-bit JAP")
session = mgr.openMachineSession(machine) # locks at this point

# Launch VM
session = mgr.mgr.getSessionObject(vbox)
machine.lockMachine(session, mgr.constants.LockType_Shared)

console = session.console

try:
   print "Session creation..."
   guestSess = console.guest.createSession("W7ProJapanesex86", "test", None, None)

   try:
      print "Session created"

      guestSess.waitFor(1, 30000)
      if guestSess.status != mgr.constants.GuestSessionStatus_Started:
         raise Exception, "Guest Session did not start after 30 sec"
      time.sleep(5) # Let Windows stabilize

      path = "C:/Windows/System32/arp.exe"
      flag = [mgr.constants.ProcessCreateFlag_WaitForStdOut]
      process = guestSess.processCreate(path, None, None, flag, 0)
      print "Process created "

      es = process.eventSource
      el = es.createListener()
      evtType = [mgr.constants.VBoxEventType_Any]
      es.registerListener(el, evtType, False)

      try:
         print "Guest process created"
         pwff1 = [mgr.constants.ProcessWaitForFlag_Start]
         pwr = process.waitFor(pwff1[0], 30000) 

         keepLooking = True
         while keepLooking:
            ev = es.getEvent(el, 200)
            if ev != None:
               es.eventProcessed(el, ev)
            stdOut = process.read(1, 64, 0)
            print str(stdOut)
            print str(process.status)
            if "Terminated" in str(process.status):
               break
         print "Process exit code: " +  process.exitCode
      finally:
         es.unregisterListener(el)
         if "Terminated" not in str(process.status):
            process.terminate()
   finally:
      print "Session close"
      guestSess.close()
except Exception as e:
   print(e)
   if hasattr(e, 'message'):
      print(e.message)
finally:
   print "Machine unlock"
   session.unlockMachine()
noteirak
Site Moderator
Posts: 5229
Joined: 13. Jan 2012, 11:14
Primary OS: Debian other
VBox Version: OSE Debian
Guest OSses: Debian, Win 2k8, Win 7
Contact:

Re: Execute process inside VM

Post by noteirak »

Well just put the right path of the command you want to run, that seems to be the error here, exactly like the error you get with vboxmanage.
Nothing wrong with the code itself it seems!
Hyperbox - Virtual Infrastructure Manager - https://apps.kamax.lu/hyperbox/
Manage your VirtualBox infrastructure the free way!
Bypus
Posts: 33
Joined: 26. Sep 2017, 11:52

Re: Execute process inside VM

Post by Bypus »

Nope, doesn't work.
It seems like it ignores the guestSess.waitFor(1, 30000) since it doesn't wait for 30 seconds, and if i remove waitFor(), Exception "Guest Session did not start after 30 sec" is raised (2 times ?)
noteirak
Site Moderator
Posts: 5229
Joined: 13. Jan 2012, 11:14
Primary OS: Debian other
VBox Version: OSE Debian
Guest OSses: Debian, Win 2k8, Win 7
Contact:

Re: Execute process inside VM

Post by noteirak »

Then I'm out of ideas I'm afraid, it could be an issue with the python SDK itself... Hopefully someone with more experience on it might have a clue
Hyperbox - Virtual Infrastructure Manager - https://apps.kamax.lu/hyperbox/
Manage your VirtualBox infrastructure the free way!
Bypus
Posts: 33
Joined: 26. Sep 2017, 11:52

Re: Execute process inside VM

Post by Bypus »

Ok then, thanks a lot for your help anyway !
Post Reply