Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get a list of running VMs using VBoxManage

I want to loop through my running VM's and return only what is between quotes.

So this command:

VBoxManage list runningvms

returns:

"UbuntuServer" {7ef01f8d-a7d5-4405-af42-94d85f999dff}

And I only want it to return:

UbuntuServer

This is what i have so far (fail):

#!/bin/bash
for machine in `cat VBoxManage list runningvms`; do
        echo "$machine"
done
exit
like image 279
capdragon Avatar asked Aug 15 '11 19:08

capdragon


People also ask

How would you get a list of all virtual machines?

To see all VMs on the local Hyper-V host, you should run the Get-VM cmdlet. On the PowerShell screen, you can see the list of available VMs, including their name, state, CPU usage, memory assigned, uptime, status, and version.

How do you check which VMs are running?

You can use The showvminfo command for show information about a particular virtual machine. This is the same information as VBoxManage list vms would show for all virtual machines.

How do I list all VMs in Linux?

You can easily list all running Virtual machines from a Linux shell prompt without accessing GUI. This is useful to scripts or to get status for any VM. => -h 'https://vmserver.example.com:8333/sdk' : VMWare server hostname. This can be local or remote server.

Which command will display the list of all virtual machines?

vms: Lists all virtual machines currently registered with Oracle VM VirtualBox. By default this displays a compact list with each VM's name and UUID. If you also specify --long or -l , this will be a detailed list as with the showvminfo command, see Section 7.5, “VBoxManage showvminfo”.


4 Answers

VBoxManage list runningvms | cut -d '"' -f 2 | while read machine; do
   echo "$machine"
done
like image 92
jfg956 Avatar answered Sep 19 '22 23:09

jfg956


Warning: all of this is is risky if your VM names have shell glob characters in them, or contain spaces.


You can do something like this if there is only one running VM:

read machine stuff <<< $(VBoxManage list runningvms)
echo "$machine"

Alternative with bash arrays (same condition):

vbm=($(VBoxManage list runningvms))
echo "${vbm[0]}"

If that program returns more than one line, a more classic approach would be:

for machine in $(VBoxManage list runningvms|cut -d" " -f 1); do
  echo "$machine"
done
like image 38
Mat Avatar answered Sep 16 '22 23:09

Mat


for one-liner fans:

VBoxManage list runningvms | cut -d" " -f 1 | grep -oP "(?<=\").*(?=\")"
like image 41
Omriko Avatar answered Sep 16 '22 23:09

Omriko


To validate each line as you read it, the safe way to do this is to write a regular expression and use BASH_REMATCH to extract match groups from it.

With the following code:

re='^"(.*)" [{]([0-9a-f-]+)[}]$'
while read -r line; do
  if [[ $line =~ $re ]]; then
    name=${BASH_REMATCH[1]}; uuid=${BASH_REMATCH[2]}
    echo "Found VM with name $name and uuid $uuid" >&2
  else
    echo "ERROR: Could not parse line: $line" >&2
  fi
done < <(VBoxManage list runningvms)

...and the following mock implementation of VBoxManage (to allow folks without VirtualBox to reproduce the test):

VBoxManage() { printf '%s\n' '"UbuntuServer" {7ef01f8d-a7d5-4405-af42-94d85f999dff}'; }

...output is as follows:

Found VM with name UbuntuServer and uuid 7ef01f8d-a7d5-4405-af42-94d85f999dff

Note advantages of this approach:

  • It doesn't make unfounded assumptions, such as excluding virtual machines with whitespace or quote literals in their names from support.
  • It detects any line that doesn't match the expected pattern, rather than behaving in an unanticipated way in the presence of such values.
  • It still behaves correctly with data that does match the pattern but has unanticipated values. (For instance, a virtual machine named * won't have that name silently replaced with the name of a file in the current directory).
  • It doesn't involve tools external to the shell such as sed, cut, &c., but relies purely on shell-builtin functionality -- see BashFAQ #1 documenting the use of while read, and the bash-hackers' wiki on regular expression matching documenting [[ $string =~ $re ]].
like image 36
Charles Duffy Avatar answered Sep 16 '22 23:09

Charles Duffy