Sunday, November 25, 2012

VirtualBox: command line (ssh) access to guest system

Today I needed to access my VirtualBox Solaris session from my Mac (the screensaver seemed to hang and I wanted to kill it to regain access).

I found this very informative posting which contained all the information and I took its contents and put them into this little script (not really necessary but a nice little exercise to write a loop for these kinds of repetitive statements).

#!/bin/bash

Machine="Solaris 10"    # My guest system
Adapter=e1000
GuestPort=22
HostPort=2222
Protocol=TCP

# Note: do not put any spaces into the brace enclosed string,
# it will break brace expansion
for var in {Protocol,HostPort,GuestPort} ; do
  echo $var
  eval VBoxManage setextradata \"$Machine\"  "VBoxInternal/Devices/$Adapter/0/LUN#0/Config/ssh/$var" \${$var}
done

# This is just to check if the settings are ok
VBoxManage getextradata "$Machine"  enumerate

# And this is also only a test to see if the system is reachable
ping -p $HostPort localhost

Access to the guest then works like this
ssh -p 2222 localhost

Access did not work right away for my VirtualBox Solaris session since it was running at the time when I executed the commands. In order to enable access I had to save and restart the session (of course a reboot would have worked too).

Given that VBoxManage is in your PATH the script should work on any UNIX system with bash.

I admit though that this script is probably more confusing than helpful since it kind of hides what is actually going on, the original posting simply lists 3 statements which are easy to read and understand.

No comments:

Post a Comment