Get running vm's via vboxapi

Discussion about using the VirtualBox API, Tutorials, Samples.
Post Reply
stielchen
Posts: 10
Joined: 24. Aug 2014, 11:35

Get running vm's via vboxapi

Post by stielchen »

Hi,
is there a way to get all running vm's via vboxapi? With

Code: Select all

mgr = VirtualBoxManager(None, None) 
vbox = mgr.vbox
vboxVMList=mgr.getArray(vbox, 'machines')
vboxNameList = [mach.name for mach in vboxVMList]  
I found a way to get all available vm's (running or not).

It should do the same like "vboxmanage list runningvms", but with vboxapi...

Thanks!
stielchen
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: Get running vm's via vboxapi

Post by noteirak »

Moved to VirtualBox API section
Hyperbox - Virtual Infrastructure Manager - https://apps.kamax.lu/hyperbox/
Manage your VirtualBox infrastructure the free way!
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: Get running vm's via vboxapi

Post by noteirak »

There isn't a built-in way of retrieving the list of running VMs. what vboxmanage does is check for each VM its current state and return only the running ones.
Java code example :

Code: Select all

public class ListRunningVms {
   
   public static void main(String[] args) {
      
      VirtualBoxManager vboxManager = VirtualBoxManager.createInstance(null);
      try {
         vboxManager.connect("http://localhost:18083", "user", "password"); // only if you are using WebServices Bindings
         IVirtualBox vbox = vboxManager.getVBox();
         for (IMachine vm : vbox.getMachines()) {
            if (vm.getState().equals(MachineState.Running)) {
               System.out.println(vm.getName()+ " "+vm.getId());
            }
         }
      } finally {
         vboxManager.disconnect(); // only if you are using WebServices Bindings
         vboxManager.cleanup();
      }
   }
   
}
Hyperbox - Virtual Infrastructure Manager - https://apps.kamax.lu/hyperbox/
Manage your VirtualBox infrastructure the free way!
stielchen
Posts: 10
Joined: 24. Aug 2014, 11:35

Re: Get running vm's via vboxapi

Post by stielchen »

@noteirak:
Thank you for the hint. I did it like vboxmanage and filtered after the state "Running". Works perfect for me...
regards
stielchen
Post Reply