vboxshell.py revision 9a4f5aa4c7145421d7ed4e1794b3cbdbc4fa4636
1N/A# Copyright (C) 2009 Sun Microsystems, Inc. 1N/A# This file is part of VirtualBox Open Source Edition (OSE), as 1N/A# you can redistribute it and/or modify it under the terms of the GNU 1N/A# General Public License (GPL) as published by the Free Software 1N/A# Foundation, in version 2 as it comes in the "COPYING" file of the 1N/A# VirtualBox OSE distribution. VirtualBox OSE is distributed in the 1N/A# hope that it will be useful, but WITHOUT ANY WARRANTY of any kind. 1N/A# Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa 1N/A# additional information or have any questions. 1N/A################################################################################# 1N/A# This program is a simple interactive shell for VirtualBox. You can query # 1N/A# information and issue commands from a simple command line. # 1N/A# It also provides you with examples on how to use VirtualBox's Python API. # 1N/A# This shell is even somewhat documented and supports TAB-completion and # 1N/A# history if you have Python readline installed. # 1N/A################################################################################ 1N/A# Simple implementation of IConsoleCallback, one can use it as skeleton 1N/A# for custom implementations print "onMachineStateChange: %s %d" %(
id,
state)
print "onMachineDataChange: %s" %(
id)
print "onExtraDataCanChange: %s %s=>%s" %(
id,
key,
value)
print "onExtraDataChange: %s %s=>%s" %(
id,
key,
value)
print "onMediaRegistred: %s" %(
id)
print "onMachineRegistred: %s" %(
id)
print "onSessionStateChange: %s %d" %(
id,
state)
print "onSnapshotTaken: %s %s" %(
mach,
id)
print "onSnapshotDiscarded: %s %s" %(
mach,
id)
print "onSnapshotChange: %s %s" %(
mach,
id)
return [
'\t',
None][
state]
Compute matches when text is a simple name. Return a list of all names currently defined in self.namespace that match. # although it has autoconversion, we need to cast # explicitly for subscripts to work "00000000-0000-0000-0000-000000000000")
print "created machine with UUID",
mach.
id print "removing machine ",
mach.
name,
"with UUID",
id # we ignore exceptions to allow starting VM even if # perf collector cannot be started # if session not opened, close doesn't make sense # Not yet implemented error string query API for remote API if ctx[
'vb']
is not None:
# not infinity, but close enough # We need to catch all exceptions here, otherwise callback will never be unregistered # not infinity, but close enough # We need to catch all exceptions here, otherwise callback will never be unregistered print "Session to '%s' not open: %s" %(
mach.
name,e)
# unfortunately IGuest is suppressed, thus WebServices knows not about it # this is an example how to handle local only functionality if ctx[
'remote']
and cmd ==
'stats2':
print 'Trying to use local only functionality, ignored' # can cache known machines, if needed print "usage: %s [vmname|uuid]" %(
args[
0])
print "Machine '%s' is unknown, use list command to find available machines" %(
id)
print "Command '%s' not known" %(
args[
1])
print " ",
args[
1],
":", c[
0]
print "usage: info [vmname|uuid]" print " One can use setvar <mach> <var> <value> to change variable, using name in []." print " ID [n/a]: %s" %(
mach.
id)
print "usage: create name ostype <basefolder>" print 'Unknown OS type:',
oskind print "usage: guest name commands" print "usage: setvar [vmname|uuid] expr value" print "'%s' is an alias for '%s'" %(k,v)
print "Processor count:",
cnt print "3D acceleration available" print "3D acceleration NOT available" print "Network interfaces:" print "usage: monitorGuest name (duration)" print "usage: monitorVbox (duration)" print "usage: portForward <vm> <adapter> <hostPort> <guestPort>" print "usage: showLog <vm> <num>" # maybe will want more args smartness print "usage: runScript <script>" print "cannot open:",
args[
1],
":",e
print "usage: sleep <secs>" # to allow sleep interrupt print "usage: shell <commands>" # to allow shell command interruption print "usage: connect [url] [username] [passwd]" if ctx[
'vb']
is not None:
print "Already connected, disconnect first..." print "usage: disconnect" print "Not connected yet." 'q':
'quit',
'exit':
'quit',
'start':[
'Start virtual machine by name or uuid',
startCmd,
0],
'create':[
'Create virtual machine',
createCmd,
0],
'remove':[
'Remove virtual machine',
removeCmd,
0],
'pause':[
'Pause virtual machine',
pauseCmd,
0],
'resume':[
'Resume virtual machine',
resumeCmd,
0],
'save':[
'Save execution state of virtual machine',
saveCmd,
0],
'stats':[
'Stats for virtual machine',
statsCmd,
0],
'powerdown':[
'Power down virtual machine',
powerdownCmd,
0],
'list':[
'Shows known virtual machines',
listCmd,
0],
'info':[
'Shows info on machine',
infoCmd,
0],
'alias':[
'Control aliases',
aliasCmd,
0],
'setvar':[
'Set VMs variable: setvar Fedora BIOSSettings.ACPIEnabled True',
setvarCmd,
0],
'eval':[
'Evaluate arbitrary Python construction: eval \'for m in getMachines(ctx): print m.name,"has",m.memorySize,"M"\'',
evalCmd,
0],
'host':[
'Show host information',
hostCmd,
0],
'guest':[
'Execute command for guest: guest Win32 \'console.mouse.putMouseEvent(20, 20, 0, 0)\'',
guestCmd,
0],
'monitorGuest':[
'Monitor what happens with the guest for some time: monitorGuest Win32 10',
monitorGuestCmd,
0],
'monitorVbox':[
'Monitor what happens with Virtual Box for some time: monitorVbox 10',
monitorVboxCmd,
0],
'portForward':[
'Setup permanent port forwarding for a VM, takes adapter number host port and guest port: portForward Win32 0 8080 80',
portForwardCmd,
0],
'showLog':[
'Show log file of the VM, : showLog Win32',
showLogCmd,
0],
'reloadExt':[
'Reload custom extensions: reloadExt',
reloadExtCmd,
0],
'runScript':[
'Run VBox script: runScript script.vbox',
runScriptCmd,
0],
'sleep':[
'Sleep for specified number of seconds: sleep 3.14159',
sleepCmd,
0],
'shell':[
'Execute external shell comman: shell "ls /etc/rc*"',
shellCmd,
0]
print "Unknown command: '%s', type 'help' for list of known commands" %(c)
# To write your own custom commands to vboxshell, create # def runTestCmd(ctx, args): # print "Testy test", ctx['vb'] # 'test': ['Test help', runTestCmd] # and issue reloadExt shell command. # This file also will be read automatically on startup. for (k,v)
in d[
'commands'].
items():
print "customize: adding \"%s\" - %s" %(k, v[
0])
cmds[k] = [v[
0], v[
1],
1]
print "Error loading user extensions:" # to allow to print actual host information, we collect info for # last 150 secs maximum, (sample every 10 secs and keep up to 15 samples) print '====== You can type quit or q to leave' # There is no need to disable metric collection. This is just an example. print "Autodetected VBOX_PROGRAM_PATH as",
vpp