vboxshell.py revision 3d0cb7c5e86b0051797daeb2e9d354b30ab449b3
6a97c5c107ea1c9935054f25a4fb5ca59a214b72vboxsync# Copyright (C) 2009-2010 Sun Microsystems, Inc.
6a97c5c107ea1c9935054f25a4fb5ca59a214b72vboxsync# This file is part of VirtualBox Open Source Edition (OSE), as
6a97c5c107ea1c9935054f25a4fb5ca59a214b72vboxsync# available from http://www.virtualbox.org. This file is free software;
6a97c5c107ea1c9935054f25a4fb5ca59a214b72vboxsync# you can redistribute it and/or modify it under the terms of the GNU
e64031e20c39650a7bc902a3e1aba613b9415deevboxsync# General Public License (GPL) as published by the Free Software
6a97c5c107ea1c9935054f25a4fb5ca59a214b72vboxsync# Foundation, in version 2 as it comes in the "COPYING" file of the
cf5f6bf2704d4fff443139e10bccc6a0a7fa4b85vboxsync# VirtualBox OSE distribution. VirtualBox OSE is distributed in the
cf5f6bf2704d4fff443139e10bccc6a0a7fa4b85vboxsync# hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
cf5f6bf2704d4fff443139e10bccc6a0a7fa4b85vboxsync# Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
cf5f6bf2704d4fff443139e10bccc6a0a7fa4b85vboxsync# Clara, CA 95054 USA or visit http://www.sun.com if you need
cf5f6bf2704d4fff443139e10bccc6a0a7fa4b85vboxsync# additional information or have any questions.
cf5f6bf2704d4fff443139e10bccc6a0a7fa4b85vboxsync#################################################################################
cf5f6bf2704d4fff443139e10bccc6a0a7fa4b85vboxsync# This program is a simple interactive shell for VirtualBox. You can query #
cf5f6bf2704d4fff443139e10bccc6a0a7fa4b85vboxsync# information and issue commands from a simple command line. #
cf5f6bf2704d4fff443139e10bccc6a0a7fa4b85vboxsync# It also provides you with examples on how to use VirtualBox's Python API. #
cf5f6bf2704d4fff443139e10bccc6a0a7fa4b85vboxsync# This shell is even somewhat documented, supports TAB-completion and #
cf5f6bf2704d4fff443139e10bccc6a0a7fa4b85vboxsync# history if you have Python readline installed. #
cf5f6bf2704d4fff443139e10bccc6a0a7fa4b85vboxsync# Finally, shell allows arbitrary custom extensions, just create #
6a97c5c107ea1c9935054f25a4fb5ca59a214b72vboxsync# .VirtualBox/shexts/ and drop your extensions there. #
6a97c5c107ea1c9935054f25a4fb5ca59a214b72vboxsync################################################################################
6a97c5c107ea1c9935054f25a4fb5ca59a214b72vboxsync# Simple implementation of IConsoleCallback, one can use it as skeleton
6a97c5c107ea1c9935054f25a4fb5ca59a214b72vboxsync# for custom implementations
6a97c5c107ea1c9935054f25a4fb5ca59a214b72vboxsync def onMousePointerShapeChange(self, visible, alpha, xHot, yHot, width, height, shape):
6a97c5c107ea1c9935054f25a4fb5ca59a214b72vboxsync print "%s: onMousePointerShapeChange: visible=%d" %(self.mach.name, visible)
6a97c5c107ea1c9935054f25a4fb5ca59a214b72vboxsync def onMouseCapabilityChange(self, supportsAbsolute, supportsRelative, needsHostCursor):
6a97c5c107ea1c9935054f25a4fb5ca59a214b72vboxsync print "%s: onMouseCapabilityChange: supportsAbsolute = %d, supportsRelative = %d, needsHostCursor = %d" %(self.mach.name, supportsAbsolute, supportsRelative, needsHostCursor)
6a97c5c107ea1c9935054f25a4fb5ca59a214b72vboxsync def onKeyboardLedsChange(self, numLock, capsLock, scrollLock):
6a97c5c107ea1c9935054f25a4fb5ca59a214b72vboxsync print "%s: onKeyboardLedsChange capsLock=%d" %(self.mach.name, capsLock)
6a97c5c107ea1c9935054f25a4fb5ca59a214b72vboxsync print "%s: onStateChange state=%d" %(self.mach.name, state)
6a97c5c107ea1c9935054f25a4fb5ca59a214b72vboxsync print "%s: onAdditionsStateChange" %(self.mach.name)
6a97c5c107ea1c9935054f25a4fb5ca59a214b72vboxsync print "%s: onNetworkAdapterChange" %(self.mach.name)
6a97c5c107ea1c9935054f25a4fb5ca59a214b72vboxsync print "%s: onStorageControllerChange" %(self.mach.name)
6a97c5c107ea1c9935054f25a4fb5ca59a214b72vboxsync print "%s: onUSBControllerChange" %(self.mach.name)
6a97c5c107ea1c9935054f25a4fb5ca59a214b72vboxsync def onUSBDeviceStateChange(self, device, attached, error):
6a97c5c107ea1c9935054f25a4fb5ca59a214b72vboxsync print "%s: onUSBDeviceStateChange" %(self.mach.name)
6a97c5c107ea1c9935054f25a4fb5ca59a214b72vboxsync print "%s: onRuntimeError fatal=%d message=%s" %(self.mach.name, fatal, message)
6a97c5c107ea1c9935054f25a4fb5ca59a214b72vboxsync print "%s: onShowWindow: %d" %(self.mach.name, winId)
6a97c5c107ea1c9935054f25a4fb5ca59a214b72vboxsync print "onExtraDataCanChange: %s %s=>%s" %(id, key, value)
6a97c5c107ea1c9935054f25a4fb5ca59a214b72vboxsync # Witty COM bridge thinks if someone wishes to return tuple, hresult
6a97c5c107ea1c9935054f25a4fb5ca59a214b72vboxsync # is one of values we want to return
6a97c5c107ea1c9935054f25a4fb5ca59a214b72vboxsync print "onExtraDataChange: %s %s=>%s" %(id, key, value)
6a97c5c107ea1c9935054f25a4fb5ca59a214b72vboxsync def onGuestPropertyChange(self, id, name, newValue, flags):
6a97c5c107ea1c9935054f25a4fb5ca59a214b72vboxsync print "onGuestPropertyChange: %s: %s=%s" %(id, name, newValue)
8f1c8e96fab0c5f5a8dcabeb4e20a56a8b4cea18vboxsync taken from:
8f1c8e96fab0c5f5a8dcabeb4e20a56a8b4cea18vboxsync http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/496812
6a97c5c107ea1c9935054f25a4fb5ca59a214b72vboxsync return rlcompleter.Completer.complete(self,text,state)
6a97c5c107ea1c9935054f25a4fb5ca59a214b72vboxsync Compute matches when text is a simple name.
6a97c5c107ea1c9935054f25a4fb5ca59a214b72vboxsync Return a list of all names currently defined
6a97c5c107ea1c9935054f25a4fb5ca59a214b72vboxsync in self.namespace that match.
6a97c5c107ea1c9935054f25a4fb5ca59a214b72vboxsync # although it has autoconversion, we need to cast
6a97c5c107ea1c9935054f25a4fb5ca59a214b72vboxsync # explicitly for subscripts to work
6a97c5c107ea1c9935054f25a4fb5ca59a214b72vboxsync # OSX need it
6a97c5c107ea1c9935054f25a4fb5ca59a214b72vboxsync print "Interrupted."
6a97c5c107ea1c9935054f25a4fb5ca59a214b72vboxsync mach = vb.createMachine(name, kind, base, "", False)
6a97c5c107ea1c9935054f25a4fb5ca59a214b72vboxsync # update cache
85e5ab5adbba74b522731762dd05ca88cb529140vboxsync # update cache
6a97c5c107ea1c9935054f25a4fb5ca59a214b72vboxsync progress = vb.openRemoteSession(session, uuid, type, "")
6a97c5c107ea1c9935054f25a4fb5ca59a214b72vboxsync print "Completed:", completed, "rc:",hex(rc&0xffffffff)
6a97c5c107ea1c9935054f25a4fb5ca59a214b72vboxsync # we ignore exceptions to allow starting VM even if
6a97c5c107ea1c9935054f25a4fb5ca59a214b72vboxsync # perf collector cannot be started
6a97c5c107ea1c9935054f25a4fb5ca59a214b72vboxsync # if session not opened, close doesn't make sense
8f1c8e96fab0c5f5a8dcabeb4e20a56a8b4cea18vboxsync ctx['_machlist'] = ctx['global'].getArray(ctx['vb'], 'machines')
8f1c8e96fab0c5f5a8dcabeb4e20a56a8b4cea18vboxsync return 'on'
6a97c5c107ea1c9935054f25a4fb5ca59a214b72vboxsync return 'off'
6a97c5c107ea1c9935054f25a4fb5ca59a214b72vboxsync return 'yes'
d0a71f63bd810b54e0359223fe53b07730154dc5vboxsync return 'no'
6a97c5c107ea1c9935054f25a4fb5ca59a214b72vboxsync cb = ctx['global'].createCallback('IConsoleCallback', GuestMonitor, machine)
6a97c5c107ea1c9935054f25a4fb5ca59a214b72vboxsync # not infinity, but close enough
6a97c5c107ea1c9935054f25a4fb5ca59a214b72vboxsync # We need to catch all exceptions here, otherwise callback will never be unregistered
8f1c8e96fab0c5f5a8dcabeb4e20a56a8b4cea18vboxsync cb = ctx['global'].createCallback('IVirtualBoxCallback', VBoxMonitor, [vbox, isMscom])
6a97c5c107ea1c9935054f25a4fb5ca59a214b72vboxsync # not infinity, but close enough
6a97c5c107ea1c9935054f25a4fb5ca59a214b72vboxsync # We need to catch all exceptions here, otherwise callback will never be unregistered
size = (w,h)
except Exception,e:
if g_verbose:
except Exception, e:
if g_verbose:
except Exception, e:
if g_verbose:
if save:
mach = None
mach = m
mach = m
return mach
for i in names:
if m.teleporterEnabled:
print "%sMachine '%s' [%s], state=%s" %(tele,m.name,m.id,asEnumElem(ctx,"SessionState", m.sessionState))
if mach == None:
print " Firmware [firmwareType]: %s (%s)" %(asEnumElem(ctx,"FirmwareType", mach.firmwareType),mach.firmwareType)
print " Clipboard mode [clipboardMode]: %s (%s)" %(asEnumElem(ctx,"ClipboardMode", mach.clipboardMode), mach.clipboardMode)
print " Machine status [n/a]: %s (%s)" % (asEnumElem(ctx,"SessionState", mach.sessionState), mach.sessionState)
print " Hardware virtualization [mach.setHWVirtExProperty(ctx['global'].constants.HWVirtExPropertyType_Enabled,value)]: " + asState(hwVirtEnabled)
print " VPID support [mach.setHWVirtExProperty(ctx['global'].constants.HWVirtExPropertyType_VPID,value)]: " + asState(hwVirtVPID)
hwVirtNestedPaging = mach.getHWVirtExProperty(ctx['global'].constants.HWVirtExPropertyType_NestedPaging)
print " Nested paging [mach.setHWVirtExProperty(ctx['global'].constants.HWVirtExPropertyType_NestedPaging,value)]: " + asState(hwVirtNestedPaging)
print " Hardware 2d video acceleration[accelerate2DVideoEnabled]: " + asState(mach.accelerate2DVideoEnabled)
print " Audio [via audioAdapter]: chip %s; host driver %s" %(asEnumElem(ctx,"AudioControllerType", mach.audioAdapter.audioController), asEnumElem(ctx,"AudioDriverType", mach.audioAdapter.audioDriver))
print " Keyboard [keyboardHidType]: %s (%s)" %(asEnumElem(ctx,"KeyboardHidType", mach.keyboardHidType), mach.keyboardHidType)
print " Pointing device [pointingHidType]: %s (%s)" %(asEnumElem(ctx,"PointingHidType", mach.pointingHidType), mach.pointingHidType)
if controllers:
print " '%s': bus %s type %s" % (controller.name, asEnumElem(ctx,"StorageBus", controller.bus), asEnumElem(ctx,"StorageControllerType", controller.controllerType))
if attaches:
for a in attaches:
print " Controller: '%s' port/device: %d:%d type: %s (%s):" % (a.controller, a.port, a.device, asEnumElem(ctx,"DeviceType", a.type), a.type)
m = a.medium
if m.hostDrive:
if a.passthrough:
if m.hostDrive:
if mach == None:
except Exception, e:
if mach == None:
(progress, pid) = console.guest.executeProcess(args[0], 0, args[1:], [], "", "", "", user, passwd, tmo)
if mach == None:
if mach == None:
if mach == None:
if mach == None:
if mach == None:
if mach == None:
if mach == None:
if mach == None:
if mach == None:
if mach == None:
if mach == None:
if enabled:
if mach == None:
if mach == None:
if mach == None:
if plug:
if mach == None:
if mach == None:
exec expr
if mach == None:
value = None
if mach == None:
key = None
if obj == None:
if key == None:
for k in keys:
global g_verbose
print "Processor #%d speed: %dMHz %s" %(i,host.getProcessorSpeed(i), host.getProcessorDescription(i))
print " %s (vendorId=%d productId=%d serial=%s) %s" %(ud.product, ud.vendorId, ud.productId, ud.serialNumber, getUSBStateString(ud.state))
if mach == None:
if mach == None:
if mach == None:
while True:
exec expr
except Exception, e:
if g_verbose:
except IOError,e:
except Exception,e:
if g_verbose:
except KeyboardInterrupt:
url = None
import sys
if mach is None:
scancodes = {
extScancodes = {
return [code]
return extCode
return codes
import time
pressed = []
for c in pressed:
pressed = []
if not group:
for c in pressed:
pressed = []
import sys
if mach is None:
if verbose:
if inBytes:
print " %s (%s)%s %dM [logical %dM]" %(hdd.location, hdd.format, optId(verbose,hdd.id),asSize(hdd.size, True), asSize(hdd.logicalSize, False))
print " %s (%s)%s %dM" %(floppy.location, floppy.format,optId(verbose,hdd.id), asSize(hdd.size, True))
hdd = vb.openHardDisk(loc, ctx['global'].constants.AccessMode_ReadWrite, setImageId, imageId, setParentId, parentId)
if mach is None:
cmdClosedVm(ctx, mach, lambda ctx,mach,args: mach.attachDevice(ctr, port, slot, ctx['global'].constants.DeviceType_HardDisk,hdd.id))
for a in atts:
if a.medium:
if mach is None:
for m in machs:
except Exception, e:
if mach is None:
cmdClosedVm(ctx, mach, lambda ctx,mach,args: mach.attachDevice(ctr, port, slot, ctx['global'].constants.DeviceType_DVD,dvd.id))
if mach is None:
if mach is None:
if mach is None:
if type != None:
if type == None:
type = None
if mach is None:
if bus is None:
if mach is None:
'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, 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],
'screenshot':['Take VM screenshot to a file: screenshot Win /tmp/win.png 1024 768', screenshotCmd, 0],
'teleport':['Teleport VM to another box (see openportal): teleport Win anotherhost:8000 <passwd> <maxDowntime>', teleportCmd, 0],
'typeGuest':['Type arbitrary text in guest: typeGuest Linux "^lls\\n&UP;&BKSP;ess /etc/hosts\\nq^c" 0.7', typeGuestCmd, 0],
'openportal':['Open portal for teleportation of VM from another box (see teleport): openportal Win 8000 <passwd>', openportalCmd, 0],
'closeportal':['Close teleportation portal (see openportal,teleport): closeportal Win', closeportalCmd, 0],
'setextra':['Set extra data, empty value removes key: setextra <vm|global> <key> <value>', setExtraDataCmd, 0],
'gueststats':['Print available guest stats (only Windows guests with additions so far): gueststats Win32', gueststatsCmd, 0],
'unplugcpu':['Remove a CPU from a running VM (additions required, Windows cannot unplug): unplugcpu Linux 1', unplugcpuCmd, 0],
'registerHdd': ['Register HDD image with VirtualBox instance: registerHdd /disk.vdi', registerHddCmd, 0],
'unregisterHdd': ['Unregister HDD image with VirtualBox instance: unregisterHdd /disk.vdi', unregisterHddCmd, 0],
'attachHdd': ['Attach HDD to the VM: attachHdd win /disk.vdi "IDE Controller" 0:1', attachHddCmd, 0],
'registerIso': ['Register CD/DVD image with VirtualBox instance: registerIso /os.iso', registerIsoCmd, 0],
'unregisterIso': ['Unregister CD/DVD image with VirtualBox instance: unregisterIso /os.iso', unregisterIsoCmd, 0],
'attachIso': ['Attach CD/DVD to the VM: attachIso win /os.iso "IDE Controller" 0:1', attachIsoCmd, 0],
'mountIso': ['Mount CD/DVD to the running VM: mountIso win /os.iso "IDE Controller" 0:1', mountIsoCmd, 0],
'unmountIso': ['Unmount CD/DVD from running VM: unmountIso win "IDE Controller" 0:1', unmountIsoCmd, 0],
c = aliases[c]
if ci == None:
# file ~/.VirtualBox/shellext.py with content like
# Also one can put shell extensions into ~/.VirtualBox/shexts and
if g_verbose:
for e in exts:
if vbox is not None:
while True:
except KeyboardInterrupt:
except EOFError:
except Exception,e:
if g_verbose:
if mach == None:
style = None
if autopath: