如何使用python从虚拟机中读取文件

时间:2014-01-24 03:58:55

标签: python virtual-machine virtualbox

我正在使用Oracle VirtualBox在我的系统上运行虚拟机,我想要完成的是从我的物理系统读取虚拟机上的文件。我正在使用virtualbox api(pyvbox)与VirtualBox进行交互。这是我第一次使用virtualbox api。

更新

>>> import virtualbox
>>> vbox = virtualbox.VirtualBox()
>>> vm =vbox.find_machine("XPBox")
>>> session = vm.create_session()
>>> gs = session.console.guest.create_session('xphandler' , ' qwerty')
>>> process, stdout, stderr = gs.execute('C:\\Windows\\System32\\cmd.exe', ['/C', 'tasklist'])

Traceback (most recent call last):
  File "<pyshell#29>", line 1, in <module>
    process, stdout, stderr = gs.execute('C:\\Windows\\System32\\cmd.exe', ['/C', 'tasklist'])
  File "C:\Python27\lib\site-packages\virtualbox\library_ext\guest_session.py", line 54, in execute
    process.wait_for(int(library.ProcessWaitResult.start), 0)
  File "C:\Python27\lib\site-packages\virtualbox\library.py", line 13666, in wait_for
    reason = ProcessWaitResult(reason)
  File "C:\Python27\lib\site-packages\virtualbox\library.py", line 121, in __init__
    raise ValueError("Can not find enumeration where value=%s" % value)
ValueError: Can not find enumeration where value=None
>>> 

当我输入以下给定的解决方案时,这是输出。

2 个答案:

答案 0 :(得分:3)

仔细阅读pyvbox文档。我相信你可以找到一些解决方案。文档中给出的以下示例不是您想要的,而是启动的良好参考。

ipython

In [1]: import virtualbox

In [2]: vbox = virtualbox.VirtualBox()

In [3]: vm = vbox.find_machine('test_vm')

In [4]: session = vm.create_session()

In [5]: gs = session.console.guest.create_session('Name', 'password')

In [6]: process, stdout, stderr = gs.execute('C:\\Windows\\System32\\cmd.exe', ['/C', 'tasklist'])

In [7]: print stdout

答案 1 :(得分:1)

以下是有关如何从来宾VM进行复制的示例: https://gist.github.com/mjdorma/9044686

您发布的回溯似乎发生在许多用户身上。它发生在IProcess.wait_for返回None时。该库试图枚举已返回的ProcessWaitResult类型。当vboxapi从尝试调用IProcess.wait_for COM接口时返回None时会发生这种情况。

 In [5]: virtualbox.library.IProcess.wait_for?
Type:       instancemethod
String Form:<unbound method IProcess.wait_for>
File:       virtualbox\library.py
Definition: virtualbox.library.IProcess.wait_for(self, wait_for, timeout_ms)
Docstring:
Waits for one or more events to happen.

in wait_for of type int
    Specifies what to wait for;
    see <link to="ProcessWaitForFlag"/> for more information.

in timeout_ms of type int
    Timeout (in ms) to wait for the operation to complete.
    Pass 0 for an infinite timeout.

return reason of type ProcessWaitResult
    The overall wait result;
    see <link to="ProcessWaitResult"/> for more information.

提示:确保在运行pyvbox时运行最新的虚拟机版本。