Created
July 16, 2014 09:44
-
-
Save Bardyl/90b436c6303ade493f8c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#/!bin/bash | |
VMSLIST=$(vboxmanage list vms) | |
VMSLISTRUNNING=$(vboxmanage list runningvms) | |
REGEX='\"[a-zA-Z0-9\.-]+\"' | |
OPT=$1 | |
case "$OPT" in | |
'--list') | |
i=0 | |
if [[ $2 == '--running' ]]; then | |
for VM in $VMSLISTRUNNING; do | |
if [[ $VM =~ $REGEX ]]; then | |
echo $VM | cut -d '"' -f 2 | |
i=$[i+1] | |
fi | |
done | |
if [[ $i -ne 0 ]]; then | |
echo "At time, there is" $i "running vms on the server." | |
else | |
echo "At time, there is no running vms on the server." | |
fi | |
else | |
for VM in $VMSLIST; do | |
if [[ $VM =~ $REGEX ]]; then | |
echo $VM | cut -d '"' -f 2 | |
i=$[i+1] | |
fi | |
done | |
if [[ $i -ne 0 ]]; then | |
echo "At time, there is $i vm on the server." | |
else | |
echo "At time, there is no vms on the server." | |
fi | |
fi;; | |
'--start') | |
if [[ $2 == '--all' ]]; then | |
for VM in $VMSLIST; do | |
if [[ $VM =~ $REGEX ]]; then | |
VM=$(echo $VM | cut -d '"' -f 2) | |
vboxmanage startvm $VM | |
fi | |
done | |
else | |
for ARG in $@; do | |
if [[ $ARG != '--start' && $VMSLIST =~ $ARG ]]; then | |
vboxmanage startvm $ARG | |
else | |
if [[ $ARG != '--start' ]]; then | |
echo "Sorry but $ARG isn't a VM." | |
else | |
echo "Please specify a vm you want to start or add --all to start all vms." | |
fi | |
fi | |
done | |
fi;; | |
'--stop') | |
if [[ $2 == '--all' ]]; then | |
for VM in $VMSLISTRUNNING; do | |
if [[ $VM =~ $REGEX ]]; then | |
VM=$(echo $VM | cut -d '"' -f 2) | |
vboxmanage controlvm $VM acpipowerbutton | |
fi | |
done | |
else | |
for ARG in $@; do | |
if [[ $ARG != '--stop' && $VMSLISTRUNNING =~ $ARG ]]; then | |
vboxmanage controlvm $ARG acpipowerbutton | |
else | |
if [[ $ARG != '--stop' ]]; then | |
echo "Sorry but $ARG isn't a VM." | |
else | |
echo "Please specify a vm you want to power off or add --all to power off all vms." | |
fi | |
fi | |
done | |
fi;; | |
'') | |
echo >&2 "Please specify what you want to do. Please type --help to command help." | |
exit 1;; | |
*) | |
echo >&2 "Invalid option $1." | |
exit 1;; | |
esac | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment