Skip to content

Instantly share code, notes, and snippets.

@grega
Last active January 26, 2019 16:27
Show Gist options
  • Save grega/61355d041ccfaef66419 to your computer and use it in GitHub Desktop.
Save grega/61355d041ccfaef66419 to your computer and use it in GitHub Desktop.
Output the total number of currently running VirtualBox VMs, along with their names. This has been written with Vagrant in mind, where VMs are named `hostname_default_xxxxxxxxx` where only the value of `hostname` is relevant/recognisable.
#!/bin/bash
machines=()
for machine in `VBoxManage list runningvms|cut -d" " -f 1`; do
machines+=("$machine")
done
if [ ${#machines[@]} -eq 1 ]; then
machinename=$(echo ${machines[@]} | cut -d'_' -f 1)
echo "1 VM running: $machinename"
elif [ ${#machines[@]} -gt 1 ]; then
echo "${#machines[@]} VMs running:"
for machine in ${machines[@]}; do
machinename=$(echo ${machine} | cut -d'_' -f 1)
echo "$machinename\""
done
else
echo "No VMs running"
fi
@grega
Copy link
Author

grega commented Jun 18, 2014

Thanks for the tput related help @jcreasey!

@grega
Copy link
Author

grega commented Jun 18, 2014

I'll soon be looking at implementing this alongside Vagrant 1.6's 'global status' feature (rather than relying on VBox): http://www.vagrantup.com/blog/feature-preview-vagrant-1-6-global-status.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment