如何从主机启动VM内的脚本?

环境介绍:

  • 主机:Ubuntu 12.04 LTS
  • 客机:Ubuntu 12.04 LTS。 在此VM中,存在my_program.py

  • 虚拟化系统:VirtualBox 4.3

题:

有没有办法从主机启动my_program.py

您可以使用Vboxmanage执行此操作。 命令的forms是

 VBoxManage guestcontrol  exec[ute] --image  --username  [--passwordfile  | --password ] [--environment "= [=]"] [--verbose] [--timeout ] [--wait-exit] [--wait-stdout] [--wait-stderr] [--dos2unix] [--unix2dos] -- [[] ... []] 

要运行python脚本,可以输入以下内容 – 通过此命令将stdout和stderr返回给主机

 VBoxManage guestcontrol "Name of Virtual Machine Goes Here" exec --image /path/to/my/script --username UserNameGoesHere --password PasswordGoesHere --wait-exit --wait-stdout --wait-stderr 

在我的情况下,测试脚本是

 #!/usr/bin/python print "hello" 

您可以通过启动VirtualBox程序并从菜单中选择“帮助”来查找有关此信息,并且您要查找的特定部分是8.31。 VBoxManage guestcontrol

在guest虚拟机中启动应用程序的最简单方法是从主机向客户发送键盘按键。 在访客中,应用程序设置为在按键上启动(使用设置 – >键盘快捷键或ccsm或任何其他方式)。 在终端或脚本中,我们将按键发送给guest,在收听客户操作系统启动程序/应用程序时。

要发送按键,请使用VBoxManage的选项keyboardputscancode
例如
VBoxManage controlvm [name] keyboardputscancode 1d 38 e0 53将发送Ctrl + Alt + DeleteVBoxManage controlvm [name] keyboardputscancode 38 24将发送Alt + J

这种方法的优点是您不需要安装任何软件,甚至是客户添加。 您不需要设置任何网络,ssh,密码等。
可以在此处找到密钥的扫描代码。