Last active
December 28, 2017 16:54
-
-
Save ffigiel/30ba9204bc516c454abf to your computer and use it in GitHub Desktop.
Simple script for managing a virtualbox machine
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
#!/usr/bin/env bash | |
VM_NAME='ubuntu' | |
VBoxManage () { | |
'/cygdrive/c/Program Files/Oracle/VirtualBox/VBoxManage.exe' $@ | |
} | |
IsVmRunning () { | |
VBoxManage list runningvms | grep $1 > /dev/null | |
return $? | |
} | |
VmOn () { | |
if ! IsVmRunning $1; then | |
VBoxManage startvm $1 --type headless | |
fi | |
} | |
case "$1" in | |
'on') | |
VmOn ubuntu | |
;; | |
'off') | |
VBoxManage controlvm $VM_NAME acpipowerbutton | |
;; | |
'restart') | |
VBoxManage controlvm $VM_NAME reset | |
;; | |
'status') | |
if IsVmRunning $VM_NAME; then | |
echo 'VM is running' | |
else | |
echo 'VM is off' | |
fi | |
;; | |
'ssh') | |
if ! IsVmRunning $VM_NAME; then | |
VmOn $VM_NAME | |
echo "Waiting for VM to boot..." | |
sleep 5 | |
fi | |
ssh $VM_NAME -o ConnectTimeout=60 | |
;; | |
*) | |
echo "Usage: ubuntu (on | off | restart | status | ssh)" | |
exit 2 | |
;; | |
esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment