Skip to content

Instantly share code, notes, and snippets.

@Leedehai
Last active July 13, 2019 05:30
Show Gist options
  • Save Leedehai/85c12369401ac7864b63436451ab8a27 to your computer and use it in GitHub Desktop.
Save Leedehai/85c12369401ac7864b63436451ab8a27 to your computer and use it in GitHub Desktop.
Control VirtualBox from terminal command line: VirtualBox CLI shortcuts script (set alias vm='vm.sh')
#!/usr/bin/env sh
DEFAULT_VM=your_default_vm_name # fill it yourself
SSH_USERNAME=your_username_to_ssh # fill it yourself
VM_PROPERTY_IP=/VirtualBox/GuestInfo/Net/1/V4/IP
VM_PROPERTY_OS=/VirtualBox/GuestInfo/OS/Product
function print_usage() {
echo " vm -h|--help -- display this help message"
echo " vm on|off [VM] -- turn on/off VM"
echo " vm ssh [VM] -- ssh to VM"
echo " vm ip [VM] -- show the IP address of VM ssh uses"
echo " vm os [VM] -- show the operating system type of VM"
echo " vm list|ls -r|--running -- list running VM(s)"
echo " vm list|ls -a|--all -- list all VM(s)"
echo " default VM name: $DEFAULT_VM"
}
function print_list_usage() {
echo "-r|--running -- list running VMs"
echo "-a|--all -- list all VMs"
}
case "$1" in
"on")
if [ $# -ne 2 ]; then
VM=$DEFAULT_VM
echo "Starting '$VM' .."
echo "use 'vm ssh $VM' to connect via SSH"
else
VM=$2
fi
VBoxManage startvm "$VM" --type headless &> /dev/null
if [ $? -ne 0 ]; then
echo "Error. Use 'vm list --all' to verify '$VM' exists."
echo " or use 'vm list --running' to see if '$VM' is already running."
exit 1
fi
sleep 7
;;
"off")
if [ $# -ne 2 ]; then
VM=$DEFAULT_VM
else
VM=$2
fi
VBoxManage controlvm "$VM" poweroff 2> /dev/null
if [ $? -eq 0 ]; then
echo "$VM shutdown successful."
else
echo "Error. Use 'vm list --running' to verify '$VM' is indeed running."
exit 1
fi
;;
"ssh")
if [ $# -ne 2 ]; then
VM=$DEFAULT_VM
echo "ssh to '$VM'"
else
VM=$2
fi
IP_MESSAGE=$(VBoxManage guestproperty get $VM $VM_PROPERTY_IP 2> /dev/null)
if [ $? -eq 0 ]; then
ssh $SSH_USERNAME@$(echo $IP_MESSAGE | cut -d' ' -f 2)
else
echo "Error. Use 'vm list --all' to verify '$VM' exists."
exit 1
fi
;;
"ip")
if [ $# -ne 2 ]; then
VM=$DEFAULT_VM
else
VM=$2
fi
IP_MESSAGE=$(VBoxManage guestproperty get $VM $VM_PROPERTY_IP 2> /dev/null)
if [ $? -eq 0 ]; then
echo $IP_MESSAGE | cut -d' ' -f 2
else
echo "Error. Use 'vm list --all' to verify '$VM' exists."
exit 1
fi
;;
"os")
if [ $# -ne 2 ]; then
VM=$DEFAULT_VM
else
VM=$2
fi
OS_MESSAGE=$(VBoxManage guestproperty get $VM $VM_PROPERTY_OS 2> /dev/null)
if [ $? -eq 0 ]; then
echo $OS_MESSAGE | cut -d' ' -f 2
else
echo "Error. Use 'vm list --all' to verify '$VM' exists."
exit 1
fi
;;
"list" | "ls")
if [ $# -ne 2 ]; then
echo "One option needed for subcommand 'list'"
print_list_usage
exit 1
elif [ "$2" = "-r" ] || [ "$2" = "--running" ]; then
VBoxManage list runningvms | cut -d \{ -f 1
elif [ "$2" = "-a" ] || [ "$2" = "--all" ]; then
VBoxManage list vms | cut -d \{ -f 1
else
echo "Wrong option"
print_list_usage
exit 1
fi
;;
*)
if [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
echo "VirtualBox CLI shorcuts."
print_usage
else
echo "Wrong command."
print_usage
exit 1
fi
;;
esac
@Leedehai
Copy link
Author

Leedehai commented Sep 9, 2018

screen shot 2018-09-09 at 13 20 03

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