1 | #!/usr/bin/python
|
---|
2 |
|
---|
3 | __copyright__ = \
|
---|
4 | """
|
---|
5 | Copyright (C) 2012-2024 Oracle and/or its affiliates.
|
---|
6 |
|
---|
7 | Permission is hereby granted, free of charge, to any person
|
---|
8 | obtaining a copy of this software and associated documentation
|
---|
9 | files (the "Software"), to deal in the Software without
|
---|
10 | restriction, including without limitation the rights to use,
|
---|
11 | copy, modify, merge, publish, distribute, sublicense, and/or sell
|
---|
12 | copies of the Software, and to permit persons to whom the
|
---|
13 | Software is furnished to do so, subject to the following
|
---|
14 | conditions:
|
---|
15 |
|
---|
16 | The above copyright notice and this permission notice shall be
|
---|
17 | included in all copies or substantial portions of the Software.
|
---|
18 |
|
---|
19 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
---|
20 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
---|
21 | OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
---|
22 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
---|
23 | HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
---|
24 | WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
---|
25 | FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
---|
26 | OTHER DEALINGS IN THE SOFTWARE.
|
---|
27 | """
|
---|
28 |
|
---|
29 |
|
---|
30 | # Things needed to be set up before running this sample:
|
---|
31 | # - Install Python and verify it works (2.7.2 will do, 3.x is untested yet)
|
---|
32 | # - On Windows: Install the PyWin32 extensions for your Python version
|
---|
33 | # (see http://sourceforge.net/projects/pywin32/)
|
---|
34 | # - If not already done, set the environment variable "VBOX_INSTALL_PATH"
|
---|
35 | # to point to your VirtualBox installation directory (which in turn must have
|
---|
36 | # the "sdk" subfolder")
|
---|
37 | # - Install the VirtualBox Python bindings by doing a
|
---|
38 | # "[python] vboxapisetup.py install"
|
---|
39 | # - Run this sample with "[python] clienttest.py"
|
---|
40 |
|
---|
41 | import os,sys
|
---|
42 | import traceback
|
---|
43 |
|
---|
44 | #
|
---|
45 | # Converts an enumeration to a printable string.
|
---|
46 | #
|
---|
47 | def enumToString(constants, enum, elem):
|
---|
48 | all = constants.all_values(enum)
|
---|
49 | for e in all.keys():
|
---|
50 | if str(elem) == str(all[e]):
|
---|
51 | return e
|
---|
52 | return "<unknown>"
|
---|
53 |
|
---|
54 | def main(argv):
|
---|
55 |
|
---|
56 | from vboxapi import VirtualBoxManager
|
---|
57 | # This is a VirtualBox COM/XPCOM API client, no data needed.
|
---|
58 | mgr = VirtualBoxManager(None, None)
|
---|
59 |
|
---|
60 | # Get the global VirtualBox object
|
---|
61 | vbox = mgr.getVirtualBox()
|
---|
62 |
|
---|
63 | print "Running VirtualBox version %s" %(vbox.version)
|
---|
64 |
|
---|
65 | # Get all constants through the Python manager code
|
---|
66 | vboxConstants = mgr.constants
|
---|
67 |
|
---|
68 | # Enumerate all defined machines
|
---|
69 | for mach in mgr.getArray(vbox, 'machines'):
|
---|
70 |
|
---|
71 | try:
|
---|
72 | # Be prepared for failures - the VM can be inaccessible
|
---|
73 | vmname = '<inaccessible>'
|
---|
74 | try:
|
---|
75 | vmname = mach.name
|
---|
76 | except Exception, e:
|
---|
77 | None
|
---|
78 | vmid = '';
|
---|
79 | try:
|
---|
80 | vmid = mach.id
|
---|
81 | except Exception, e:
|
---|
82 | None
|
---|
83 |
|
---|
84 | # Print some basic VM information even if there were errors
|
---|
85 | print "Machine name: %s [%s]" %(vmname,vmid)
|
---|
86 | if vmname == '<inaccessible>' or vmid == '':
|
---|
87 | continue
|
---|
88 |
|
---|
89 | # Print some basic VM information
|
---|
90 | print " State: %s" %(enumToString(vboxConstants, "MachineState", mach.state))
|
---|
91 | print " Session state: %s" %(enumToString(vboxConstants, "SessionState", mach.sessionState))
|
---|
92 |
|
---|
93 | # Do some stuff which requires a running VM
|
---|
94 | if mach.state == vboxConstants.MachineState_Running:
|
---|
95 |
|
---|
96 | # Get the session object
|
---|
97 | session = mgr.getSessionObject()
|
---|
98 |
|
---|
99 | # Lock the current machine (shared mode, since we won't modify the machine)
|
---|
100 | mach.lockMachine(session, vboxConstants.LockType_Shared)
|
---|
101 |
|
---|
102 | # Acquire the VM's console and guest object
|
---|
103 | console = session.console
|
---|
104 | guest = console.guest
|
---|
105 |
|
---|
106 | # Retrieve the current Guest Additions runlevel and print
|
---|
107 | # the installed Guest Additions version
|
---|
108 | addRunLevel = guest.additionsRunLevel
|
---|
109 | print " Additions State: %s" %(enumToString(vboxConstants, "AdditionsRunLevelType", addRunLevel))
|
---|
110 | if addRunLevel != vboxConstants.AdditionsRunLevelType_None:
|
---|
111 | print " Additions Ver: %s" %(guest.additionsVersion)
|
---|
112 |
|
---|
113 | # Get the VM's display object
|
---|
114 | display = console.display
|
---|
115 |
|
---|
116 | # Get the VM's current display resolution + bit depth + position
|
---|
117 | screenNum = 0 # From first screen
|
---|
118 | (screenW, screenH, screenBPP, screenX, screenY, _) = display.getScreenResolution(screenNum)
|
---|
119 | print " Display (%d): %dx%d, %d BPP at %d,%d" %(screenNum, screenW, screenH, screenBPP, screenX, screenY)
|
---|
120 |
|
---|
121 | # We're done -- don't forget to unlock the machine!
|
---|
122 | session.unlockMachine()
|
---|
123 |
|
---|
124 | except Exception, e:
|
---|
125 | print "Errror [%s]: %s" %(mach.name, str(e))
|
---|
126 | traceback.print_exc()
|
---|
127 |
|
---|
128 | # Call destructor and delete manager
|
---|
129 | del mgr
|
---|
130 |
|
---|
131 | if __name__ == '__main__':
|
---|
132 | main(sys.argv)
|
---|