Skip to content

Instantly share code, notes, and snippets.

@mtavkhelidze
Created October 31, 2019 10:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mtavkhelidze/06d739916239d25cf22249b8139c8e11 to your computer and use it in GitHub Desktop.
Save mtavkhelidze/06d739916239d25cf22249b8139c8e11 to your computer and use it in GitHub Desktop.
Start/Stop VirtualBox VM in headless mode
#!/usr/bin/env bash
vmm=VBoxManage
function usage {
echo "Usage:" $(basename $0) "start|stop" "hostname" >/dev/stderr
}
function get_ip {
VBoxManage guestproperty enumerate $1 \
| grep "V4/IP" \
| cut -d ' ' -f 4 \
| tr -d ","
}
if [ "$1" = "" ]; then
echo "Need command: start | stop"
usage
exit 1
fi
cmd=$1
if [ "$2" = "" ]; then
echo "Need VM hostname"
usage
exit 1
fi
vm=$($vmm list vms | grep $2 | cut -d ' ' -f 1 | tr -d '"')
echo ${cmd} $vm
if [ $cmd = "start" ]
then
VBoxManage startvm $vm --type headless
echo "$2:" $(get_ip $vm)
elif [ $cmd = "stop" ]
then
VBoxManage controlvm $vm acpipowerbutton
else
usage
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment