Quote:
Originally Posted by dfed
Ok. One line to output VM name and os:
Code:
for x in `VBoxManage list vms | grep '"' | cut -d'"' -f2 2>/dev/null`; do VBoxManage showvminfo "${x}" | egrep -e "Name:" -e "Guest OS:" |cut -b 10-64 |tr -d '\n'| awk '{print $1" "$2" "$3" "$4" "$5" "$6}'; done
which outputs like this:
Code:
redhatest Red Hat (64 bit)
windowstest Windows XP (64 bit)
opensolaristest OpenSolaris (64 bit)
anothertest Windows 7 (64 bit)
archtest Arch Linux (64 bit)
bsdtest FreeBSD (64 bit)
warptest OS/2 Warp 4.5
Working on the portion to determine which is running and append that file with that.
|
Ok got it. It's messy and horrible, and I am sure there is a better way to do this, but here's one way:
Code:
for x in `VBoxManage list vms | grep '"' | cut -d'"' -f2 | sort -u 2>/dev/null`; do VBoxManage showvminfo "${x}" | egrep -e "Name:" -e "Guest OS:" -e "State:" | cut -b 10-64 | tr -d '\n' | awk '{print $1" "$2" " $3" " $4" "$5" "$6" "$7" "$8" "$9" "$10}'| awk -F"\(since" '{print $1}'; done
which outputs:
Code:
chat Ubuntu (64 bit) running
db Ubuntu (64 bit) running
mail Ubuntu (64 bit) running
nas Ubuntu (64 bit) running
shell Ubuntu (64 bit) running
web Ubuntu (64 bit) running
(on this machine, it's all ubuntu guests. the one I was on earlier has more variety. Both return correct answers.)
That allows you to have scripts that parse via grep and awk based on name, OS and whether the guest is running or not.
I should mention that if you create machine names with spaces in them, this breaks. I should also mention that this is all done in bash, which is much more clunky than if I did this in perl. I'll maybe look into rewriting it in perl later.
ETA:
Also modified the first script to start/stop/restart:
Code:
#!/bin/bash
#Written by dfed with no guarantee this pile of crap will work.
#script to start/stop/restart one or all VM's installed on system via VirtualBox
#
# If we've not had 2 variables passed in the command line, failboat has sailed.
if [[ "${#}" -lt 2 ]]; then
echo "Usage: ${0} <start|stop|restart> <(vm name)|all>" >&2
exit 1;
elif [[ "${#}" -gt 2 ]]; then
echo "Usage: ${0} <start|stop|restart> <(vm name)|all>" >&2
exit 1;
fi
# First, declare some arrays to use later:
declare -a list
list=($(VBoxManage list vms | grep '"' | cut -d'"' -f2 | sort -u 2>/dev/null))
# Sanity check on ${1}:
if [[ "${1}" == "stop" ]]; then
com=${1}
elif [[ "${1}" == "start" ]]; then
com=${1}
elif [[ "${1}" == "restart" ]]; then
com=${1}
else echo "Usage: ${0} <start|stop|restart> <(vm name)|all>" >&2; exit 1;
fi
# Sanity check on ${2}:
checkvm=($(for z in ${list[@]}; do echo ${z} | grep ${2}; done))
if [[ "${2}" == ${checkvm} ]]; then
vm=${2}
elif [[ "${2}" == "all" ]]; then
vm=${list[@]}
else echo "Virtual Machine ${2} not found. Exiting." >&2; exit 1;
fi
echo "Working to ${com} ${vm[@]}"
# Let's do some work.
if [[ "${com}" == "start" ]]; then
for x in ${vm[@]}; do nohup VBoxHeadless --startvm ${x} 2> /dev/null > /dev/null &
echo "Started VM ${x}"
done
elif [[ "${com}" == "stop" ]]; then
for x in ${vm[@]}; do VBoxManage controlvm ${x} poweroff 2>&1 > /dev/null
echo "Stopped VM ${x}"
done
elif [[ "${com}" == "restart" ]]; then
for x in ${vm[@]}; do VBoxManage controlvm ${x} poweroff 2>&1 > /dev/null && nohup VBoxHeadless --startvm ${x} 2> /dev/null > /dev/null &
echo "Restarted VM ${x}"
done
fi
exit 0