Skip to content

Instantly share code, notes, and snippets.

@m4rcu5nl
Last active April 8, 2018 01:14
Show Gist options
  • Save m4rcu5nl/12cc91639c1262a24facede48bc744c1 to your computer and use it in GitHub Desktop.
Save m4rcu5nl/12cc91639c1262a24facede48bc744c1 to your computer and use it in GitHub Desktop.
vbssh - Easy way to ssh in to VirtualBox guests with GueastAdditions installed
#!/usr/bin/env bash
# Prototype
#
# VirtualBoxSSH
# Makes ssh-ing in to my VirtualBox VMs piss easy. Only works
# with VMs that have the GuestAdditions installed.
#
# Usage: vbssh [user@]exact-name-of-guest
# (if no user defined user running the script will be used)
#
# Please note: This assumes the 2nd interface of the guest
# is used for ssh. If that is not the case, change the value
# of "/VirtualBox/GuestInfo/Net/1/V4/IP" accordingly. E.g. the
# 1st interface would be "/VirtualBox/GuestInfo/Net/0/V4/IP"
_CONN_STRING="${1}"
_CONN_ARRAY=(${_CONN_STRING//@/ })
_VMS_ACTIVE=($(VBoxManage list runningvms | awk '{print $1}' | tr -d '"'))
if [[ ${#_CONN_ARRAY[@]} -eq 1 ]]; then
_USER=$(whoami)
_VM_NAME=${_CONN_ARRAY[0]}
elif [[ ${#_CONN_ARRAY[@]} -eq 2 ]]; then
_USER=${_CONN_ARRAY[0]}
_VM_NAME=${_CONN_ARRAY[1]}
else
(>&2 echo "Invalid input.")
exit 1
fi
if [[ ! " ${_VMS_ACTIVE[@]} " =~ " ${_VM_NAME} " ]]; then
(>&2 echo "No running VM with name ${_VM_NAME} found. Exiting.")
exit 1
else
_VM_IPV4=$(VBoxManage guestproperty get ${_VM_NAME} "/VirtualBox/GuestInfo/Net/1/V4/IP" | awk '{print $2}')
fi
exec ssh ${_USER}@${_VM_IPV4}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment