SSH
We can run an application on a virtual guest with an SSH session from the host to this machine. However this requires that networking was enabled, and that openssh-server was installed an runs on the guest machine.
VBoxManage guestcontrol
As an alternative we can use built-in features of Virtual Box to execute a program on a running guest VM. This can be done with VBoxManage guestcontrol
.
The example line below will just run ls
on the virtual machine's root:
VBoxManage --nologo guestcontrol "<vm_name>" run --exe "/bin/ls" --username <guestuser> --password <password> --wait-stdout
Running a graphical application on the guest requires us to define the DISPLAY environment variable to the guest with the option --putenv
. Next example will run and open gedit on the guest:
VBoxManage --nologo guestcontrol "<vm_name" run --exe "/usr/bin/gedit" --username <guestuser> --password <password> --putenv "DISPLAY=:0" --wait-stdout
We can also pass options to open a program. Next example will open a file vmtest
in the guest gedit:
VBoxManage --nologo guestcontrol "vm_name" run --exe "/usr/bin/gedit" --username <guestuser> --password <password> --putenv "DISPLAY=:0" --wait-stdout -- gedit/arg0 vmtest
Options and arguments are separated from the command with --
as can best be seen in below example of a script from the host.
Example host script
Below script will play an example.ogg
file using paplay
in a guest machine when run on the host. Replace the variables with appropriate values.
#!/bin/bash
VM_NAME=myvm
VM_USER=takkat
VM_PASSWD=topsecret
VM_EXEC=paplay
VM_EXEC_PATH=/usr/bin/paplay
VM_ARGS=/home/takkat/Music/example.ogg
VBoxManage --nologo guestcontrol $VM_NAME run --exe $VM_EXEC_PATH \
--username $VM_USER --password $VM_PASSWD --wait-stdout \
-- {$VM_EXEC}/arg0 $VM_ARGS
Instead of starting an interactive ssh session, pass the echo command directly as an argument to ssh:
sshpass -p "password" ssh -o StrictHostKeyChecking=no username@192.168.1.3 '
echo "mgmt.led_pattern_override=2" >> /var/etc/persistent/cfg/mgmt
'
Your other option would be to use expect
to script the interactive session - but that's overcomplicated for this case.
Best Answer
Something like this:
Examples
As OP commented: