用python从命令行控制VirtualBox

2024-09-24 02:23:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我们使用pythonvirtualbox API来控制virtualbox。为此,我们使用“pyvb”包(如python API文档中所示)。在

al=pyvb.vb.VB()
m=pyvb.vm.vbVM()
al.startVM(m)

我们已经使用python解释器执行了。不会显示错误,但virtualbox不会启动。你能告诉我们有什么问题吗(所有必要的模块和软件包都已进口)


Tags: 模块文档api错误vm解释器vbvirtualbox
2条回答

我发现我可以使用以下函数来查找VM是否正在运行,将VM还原到特定的快照,并按名称启动VM。在

from subprocess import Popen, PIPE

    def running_vms():
        """
        Return list of running vms
        """
        f = Popen(r'vboxmanage  nologo list runningvms', stdout=PIPE).stdout
        data = [ eachLine.strip() for eachLine in f ]
        return data

    def restore_vm(name='', snapshot=''):
        """
        Restore VM to specific snapshot uuid

        name = VM Name
        snapshot = uuid of snapshot  (uuid can be found in the xml file of your machines folder)
        """
        command = r'vboxmanage  nologo snapshot %s restore %s' % (name,snapshot)
        f = Popen(command, stdout=PIPE).stdout
        data = [ eachLine.strip() for eachLine in f ]
        return data

    def launch_vm(name=''):
        """
        Launch VM

        name = VM Name
        """
        command = r'vboxmanage  nologo startvm %s ' % name
        f = Popen(command, stdout=PIPE).stdout
        data = [ eachLine.strip() for eachLine in f ]
        return data

引用的代码似乎没有指定要运行哪个VM。您不应该先执行一个^{}调用,然后在startVM调用中使用得到的VM实例吗?E、 g.:

al=pyvb.vb.VB()
m=al.getVM(guid_of_vm)
al.startVM(m)

…将启动使用给定GUID标识的VM(所有VirtualBox VM在创建时都会分配一个GUID)。您可以从VM的XML文件中获取GUID。如果您需要在运行时发现vm,可以使用方便的^{}调用:

^{pr2}$

相关问题 更多 >