Skip to content

Instantly share code, notes, and snippets.

@thepoppingone
Last active March 20, 2019 09:38
Show Gist options
  • Save thepoppingone/ca5845e730b543dd156e127e1d8c61b5 to your computer and use it in GitHub Desktop.
Save thepoppingone/ca5845e730b543dd156e127e1d8c61b5 to your computer and use it in GitHub Desktop.
hats-vm script for VBManage of vms and usb devices

Setup

  1. Unzip the package if you download the file from Github

  2. chmod +x the hats-vm.sh file to make it executable

  3. ./hats-vm.sh to start

Follow the instructions in the shell output to control the VMs via command line.

#!/bin/bash
#Author: Wang Poh Peng
#chkconfig: 345 95 50
#description: VBoxManage Commands
# if [ -z "$1" ]; then
echo ""
echo " ////////////////////////////////////////////////////////////////"
echo " Running HATS VM Controller to control all VMs on host machine"
echo " ////////////////////////////////////////////////////////////////"
echo ""
# exit
# fi
option="none"
while [[ $option != "exit" && $option != "quit" ]]
do
echo ""
echo " OPTIONS BELOW :"
echo "================================================================="
echo " active | List all running vms"
echo " all | List all vms"
echo " usb | List all USB devices"
echo " phones | List all phones connected through usb"
echo " select | Select a VM through its name"
echo " select_active | Select a VM through its name"
echo "================================================================="
echo " To exit/quit -> exit || quit command or Ctrl+C "
echo "================================================================="
echo " Enter Option: "
read option
case "$option" in
active)
echo ""
echo "names"
VBoxManage list runningvms
;;
all)
echo ""
echo "===================================================================================="
echo " VM Names "
echo "===================================================================================="
VBoxManage list vms
;;
usb)
echo ""
VBoxManage list usbhost
;;
phones)
echo ""
VBoxManage list usbhost | grep -i "SerialNumber" -B 8 -A 2
;;
select)
echo ""
echo "===================================================================================="
echo " VM Names "
echo "===================================================================================="
VBoxManage list vms
echo ""
echo "Enter a VM name from above - without quotations"
read vmname
echo "<$vmname> selected"
vmoption="none"
while [[ $vmoption != "back" && $vmoption != "quit" && $vmoption != "exit" ]]
do
echo " SELECT AN USB ACTION BELOW: "
echo "========================================================"
echo " start | Start VM "
echo " start_headless | Start VM in headless mode"
echo " list_filters | List all USB Filters"
echo " add_filter | Add USB filter to VM (show phones only) "
echo " remove_filter | Remove USB filter from VM "
echo " remove_all_filters | Remove all USB filters from VM "
echo " show_info | Get selected VM information "
echo "========================================================"
echo " To return to root menu type: back, quit or exit"
echo "========================================================"
echo " Enter VM Option: "
read vmoption
echo "<$vmoption> action selected"
case "$vmoption" in
show_info)
echo "Displaying $vmname's info"
VBoxManage showvminfo $vmname
;;
start)
echo "Starting $vmname in normal mode"
VBoxManage startvm $vmname
;;
list_filters)
echo "Showing USB Filters associated with VM"
VBoxManage showvminfo $vmname | sed -n "/USB Device Filters/,/Bandwidth/p" | sed -n "/USB Device Filters/,/Avail/p" | sed -e '$ d'
;;
add_filter)
echo "Listing all phones"
VBoxManage list usbhost | grep -i "SerialNumber" -B 8 -A 2
echo "----------------------------------------------"
echo "Enter Serial Number of USB device: "
read serialnumber
echo "Enter name of USB filter"
read filtername
VBoxManage usbfilter add 1 --target $vmname --name $filtername --serialnumber $serialnumber
echo "USB filter added!"
;;
remove_filter)
VBoxManage showvminfo $vmname | sed -n "/USB Device Filters/,/Bandwidth/p" | sed -n "/USB Device Filters/,/Avail/p" | sed -e '$ d'
echo "Enter index value to remove USB filter"
read indexremove
VBoxManage usbfilter remove $indexremove --target $vmname
;;
remove_all_filters)
indexfil="$(VBoxManage showvminfo osxhs | sed -n "/USB Device Filters/,/Bandwidth/p" | sed -n "/USB Device Filters/,/Avail/p" | sed -e '$ d' | grep Index | awk '{print $2}')"
while [[ ! -z "$indexfil" ]]
do
VBoxManage usbfilter remove $indexfil --target $vmname
indexfil="$(VBoxManage showvminfo osxhs | sed -n "/USB Device Filters/,/Bandwidth/p" | sed -n "/USB Device Filters/,/Avail/p" | sed -e '$ d' | grep Index | awk '{print $2}')"
done
echo "Removed all USB filters!"
;;
start_headless)
echo "Starting $vmname in headless mode"
VBoxManage startvm $vmname --type headless
;;
esac
done
;;
select_active)
echo "===================================================================================="
echo "Active VM Names "
echo "===================================================================================="
VBoxManage list runningvms
echo ""
echo "Enter a VM name from above - without quotations"
read vmname
echo "<$vmname> selected"
vmoption="none"
while [[ $vmoption != "back" && $vmoption != "quit" && $vmoption != "exit" ]]
do
echo " SELECT AN USB ACTION BELOW: "
echo "========================================================"
echo " force_shutdown | Power Off VM"
echo " shutdown | Send ACPI Signal to shutdown gracefully "
echo " reset | Reset VM"
echo " attach | Attach USB device to VM "
echo " detach | Detach USB device from VM "
echo " detach_all | Detach all USB devices from VM"
echo " show_attached | Show all attached devices"
echo " show_info | Get selected VM information "
echo "========================================================"
echo " To return to root menu type: back, quit or exit"
echo "========================================================"
echo " Enter VM Option: "
read vmoption
echo "<$vmoption> action selected"
case "$vmoption" in
force_shutdown)
echo "Pulling Plug on the VM..."
VBoxManage controlvm $vmname poweroff
;;
shutdown)
echo "Sending ACPI shutdown signal on the VM..."
VBoxManage controlvm $vmname acpipowerbutton
;;
reset)
echo "Resetting VM..."
VBoxManage controlvm $vmname reset
;;
attach)
echo ""
VBoxManage list usbhost | grep -i "SerialNumber" -B 8 -A 2
echo "Enter UUID of USB Device to attach: "
read deviceuuid
VBoxManage controlvm $vmname usbattach $deviceuuid
;;
detach)
echo ""
VBoxManage showvminfo $vmname | sed -n "/Currently Attached/,/Bandwidth/p" | sed -e '$ d'
echo "Enter UUID of USB Device to detach: "
read deviceuuid
VBoxManage controlvm $vmname usbdetach $deviceuuid
;;
detach_all)
sampleUUID="$(VBoxManage showvminfo $vmname | grep -A 2 "Currently Attached" | tail -n +3 | awk '{print $2}')"
while [ ! -z "$sampleUUID" ]
do
echo "Detaching $sampleUUID"
VBoxManage controlvm $vmname usbdetach $sampleUUID
sampleUUID="$(VBoxManage showvminfo $vmname | grep -A 2 "Currently Attached" | tail -n +3 | awk '{print $2}')"
done
echo "All devices detached!"
;;
show_attached)
VBoxManage showvminfo $vmname | sed -n "/Currently Attached/,/Bandwidth/p" | sed -e '$ d'
;;
show_info)
echo "Displaying $vmname's info"
VBoxManage showvminfo $vmname
;;
esac
done
;;
esac
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment