vboxshell.py revision a24cd8d8c3201cdbe58d54afa136dbe822c85da1
#
# Copyright (C) 2009 Sun Microsystems, Inc.
#
# This file is part of VirtualBox Open Source Edition (OSE), as
# available from http://www.virtualbox.org. This file is free software;
# General Public License (GPL) as published by the Free Software
# Foundation, in version 2 as it comes in the "COPYING" file of the
# VirtualBox OSE distribution. VirtualBox OSE is distributed in the
# hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
#
# Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
# Clara, CA 95054 USA or visit http://www.sun.com if you need
# additional information or have any questions.
#
#
#################################################################################
# This program is a simple interactive shell for VirtualBox. You can query #
# information and issue commands from a simple command line. #
# #
# It also provides you with examples on how to use VirtualBox's Python API. #
# This shell is even somewhat documented and supports TAB-completion and #
# history if you have Python readline installed. #
# #
# Enjoy. #
################################################################################
import traceback
import shlex
# Simple implementation of IConsoleCallback, one can use it as skeleton
# for custom implementations
class GuestMonitor:
def onAdditionsStateChange(self):
def onDVDDriveChange(self):
def onFloppyDriveChange(self):
def onVRDPServerChange(self):
def onUSBControllerChange(self):
def onCanShowWindow(self):
return True
class VBoxMonitor:
pass
print "onMachineDataChange: %s" %(id)
return True, ""
print "onMediaRegistred: %s" %(id)
print "onMachineRegistred: %s" %(id)
g_hasreadline = 1
try:
import readline
import rlcompleter
except:
g_hasreadline = 0
if g_hasreadline:
"""
taken from:
"""
if text == "":
return ['\t',None][state]
else:
"""
Compute matches when text is a simple name.
Return a list of all names currently defined
in self.namespace that match.
"""
matches = []
try:
# although it has autoconversion, we need to cast
# explicitly for subscripts to work
except Exception,e:
print e
return matches
if not g_hasreadline:
return
comps = {}
comps[k] = None
def split_no_quotes(s):
"00000000-0000-0000-0000-000000000000")
for d in mach.getHardDiskAttachments():
if mach:
#print "Completed:", progress.completed, "rc:",hex(rc&0xffffffff)
if rc == 0:
# we ignore exceptions to allow starting VM even if
# perf collector cannot be started
if perf:
try:
except Exception,e:
print e
if g_verbose:
pass
# if session not opened, close doesn't make sense
else:
# Not yet implemented error string query API for remote API
if not ctx['remote']:
def getMachines(ctx):
if var:
return 'on'
else:
return 'off'
if not ctx['perf']:
return
exec cmds
import time
if dur == -1:
# not infinity, but close enough
dur = 100000
try:
# We need to catch all exceptions here, otherwise callback will never be unregistered
except:
pass
import time
if dur == -1:
# not infinity, but close enough
dur = 100000
try:
# We need to catch all exceptions here, otherwise callback will never be unregistered
except:
if g_verbose:
try:
except Exception,e:
if g_verbose:
return
return
# unfortunately IGuest is suppressed, thus WebServices knows not about it
# this is an example how to handle local only functionality
print 'Trying to use local only functionality, ignored'
return
}
try:
except Exception, e:
print 'failed: ',e
if g_verbose:
# can cache known machines, if needed
mach = None
for m in getMachines(ctx):
mach = m
break
mach = m
break
return mach
return None
if m == None:
print "Machine '%s' is unknown, use list command to find available machines" %(id)
return m
print "Help page:"
for i in names:
else:
if c == None:
else:
return 0
for m in getMachines(ctx):
return 0
import time
print "usage: info [vmname|uuid]"
return 0
if mach == None:
return 0
print " One can use setvar <mach> <var> <value> to change variable, using name in []."
return 0
if mach == None:
return 0
else:
type = "gui"
return 0
print "usage: create name ostype <basefolder>"
return 0
else:
base = ''
try:
except Exception, e:
print 'Unknown OS type:',oskind
return 0
return 0
if mach == None:
return 0
return 0
if mach == None:
return 0
return 0
if mach == None:
return 0
return 0
if mach == None:
return 0
return 0
if mach == None:
return 0
return 0
if mach == None:
return 0
return 0
if mach == None:
return 0
return 0
print "usage: guest name commands"
return 0
if mach == None:
return 0
return 0
print "usage: setvar [vmname|uuid] expr value"
return 0
if mach == None:
return 0
print "Executing",expr
try:
exec expr
except Exception, e:
print 'failed: ',e
if g_verbose:
return 0
return 1
return 0
print "'%s' is an alias for '%s'" %(k,v)
return 0
global g_verbose
return 0
print "Processor count:",cnt
if ctx['perf']:
return 0
print "usage: monitorGuest name (duration)"
return 0
if mach == None:
return 0
dur = 5
return 0
print "usage: monitorVbox (duration)"
return 0
dur = 5
return 0
return "pcnet"
return "e1000"
return None
else:
print "usage: portForward <vm> <adapter> <hostPort> <guestPort>"
return 0
if mach == None:
return 0
proto = "TCP"
return 0
print "usage: showLog <vm> <num>"
return 0
if mach == None:
return 0
log = "VBox.log"
try:
except IOError,e:
print "cannot open: ",e
return 0
print line,
return 0
try:
exec expr
except Exception, e:
print 'failed: ',e
if g_verbose:
return 0
# maybe will want more args smartness
return 0
import time
print "usage: runScript <script> <times> <pause>"
return 0
if g_verbose:
print "repeating %d times" %(times)
else:
times = 1
else:
pause = 0
try:
except IOError,e:
return 0
script = []
try:
if done != 0: break
if g_verbose:
print "%d done" %(i)
except Exception,e:
print "error:",e
if g_verbose:
return 0
print "usage: shell <command>"
try:
except Exception,e:
print "error:",e
if g_verbose:
return 0
'i':'info',
'l':'list',
'h':'help',
'a':'alias',
'q':'quit', 'exit':'quit',
'v':'verbose'}
'eval':['Evaluate arbitrary Python construction: eval \'for m in getMachines(ctx): print m.name,"has",m.memorySize,"M"\'', evalCmd, 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],
}
c = args[0]
c = aliases[c]
if ci == None:
print "Unknown command: '%s', type 'help' for list of known commands" %(c)
return 0
#
# To write your own custom commands to vboxshell, create
# file ~/.VirtualBox/shellext.py with content like
#
# def runTestCmd(ctx, args):
# print "Testy test", ctx['vb']
# return 0
#
# commands = {
# 'test': ['Test help', runTestCmd]
# }
# and issue reloadExt shell command.
# This file also will be read automatically on startup.
#
return
d = {}
try:
for (k,v) in d['commands'].items():
if g_verbose:
print "customize: adding \"%s\" - %s" %(k, v[0])
except:
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)
if ctx['perf']:
try:
except:
pass
while True:
try:
if done != 0: break
except KeyboardInterrupt:
print '====== You can type quit or q to leave'
break
except EOFError:
break;
except Exception,e:
print e
if g_verbose:
try:
# There is no need to disable metric collection. This is just an example.
if ct['perf']:
except:
pass
style = None
style = "WEBSERVICE"
if autopath:
if vpp is None and (os.path.isfile(os.path.join(cwd, "VirtualBox")) or os.path.isfile(os.path.join(cwd, "VirtualBox.exe"))) :
print "Autodetected VBOX_PROGRAM_PATH as",vpp
from vboxapi import VirtualBoxManager
}
if __name__ == '__main__':