vboxshell.py revision 18dbcbb8555ee72f93ae6e7c08904d066f3d45d4
c97989161fbe75bc14cea477a5443bbf474dd3advboxsync# Copyright (C) 2009-2010 Oracle Corporation
5c73d69670892b2d7d6a369da2629fd15512011avboxsync# This file is part of VirtualBox Open Source Edition (OSE), as
5c73d69670892b2d7d6a369da2629fd15512011avboxsync# available from http://www.virtualbox.org. This file is free software;
c97989161fbe75bc14cea477a5443bbf474dd3advboxsync# you can redistribute it and/or modify it under the terms of the GNU
5c73d69670892b2d7d6a369da2629fd15512011avboxsync# General Public License (GPL) as published by the Free Software
5c73d69670892b2d7d6a369da2629fd15512011avboxsync# Foundation, in version 2 as it comes in the "COPYING" file of the
5c73d69670892b2d7d6a369da2629fd15512011avboxsync# VirtualBox OSE distribution. VirtualBox OSE is distributed in the
5c73d69670892b2d7d6a369da2629fd15512011avboxsync# hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
5c73d69670892b2d7d6a369da2629fd15512011avboxsync#################################################################################
5c73d69670892b2d7d6a369da2629fd15512011avboxsync# This program is a simple interactive shell for VirtualBox. You can query #
5c73d69670892b2d7d6a369da2629fd15512011avboxsync# information and issue commands from a simple command line. #
5c73d69670892b2d7d6a369da2629fd15512011avboxsync# It also provides you with examples on how to use VirtualBox's Python API. #
5c73d69670892b2d7d6a369da2629fd15512011avboxsync# This shell is even somewhat documented, supports TAB-completion and #
5c73d69670892b2d7d6a369da2629fd15512011avboxsync# history if you have Python readline installed. #
5c73d69670892b2d7d6a369da2629fd15512011avboxsync# Finally, shell allows arbitrary custom extensions, just create #
5c73d69670892b2d7d6a369da2629fd15512011avboxsync# .VirtualBox/shexts/ and drop your extensions there. #
5c73d69670892b2d7d6a369da2629fd15512011avboxsync################################################################################
term_colors = {
if not g_hascolors:
return string
global term_colors
if col:
return string
if g_hasreadline:
import string
if firstWord:
return True
return True
return False
matches = []
except Exception,e:
printErr(e)
if g_verbose:
return matches
if not g_hasreadline:
comps = {}
comps[k] = None
def split_no_quotes(s):
while not p.completed:
except KeyboardInterrupt:
if p.cancelable:
p.cancel()
if ei:
if mach:
if perf:
except Exception,e:
if g_verbose:
class CachedMach:
result = []
for m in list:
return result
if simple:
if var:
if var:
exec cmds
if scev:
if gpcev:
if psev:
if shape is None:
class EventListener:
if active:
if active:
if ev:
f = "/tmp/screenshot.png"
w = fbw
h = fbh
size = (w,h)
return v1
return v2
print " %s: %s (vendorId=%d productId=%d serial=%s) %s" %(ud.id, colored(ud.product,'blue'), ud.vendorId, ud.productId, ud.serialNumber,asEnumElem(ctx, 'USBDeviceState', ud.state))
print " %s: %s (vendorId=%d productId=%d serial=%s)" %(ud.id, colored(ud.product,'blue'), ud.vendorId, ud.productId, ud.serialNumber)
print " name=%s host=%s %s %s" %(sf.name, colPath(ctx,sf.hostPath), cond(sf.accessible, "accessible", "not accessible"), cond(sf.writable, "writable", "read-only"))
session = None
except Exception,e:
if g_verbose:
except Exception, e:
if g_verbose:
except Exception, e:
if g_verbose:
if save:
except Exception, e:
if g_verbose:
if save:
mach = None
mach = m
mach = m
return mach
class XPathNode:
matches = []
for e in children:
return matches
return True
return False
return matches
children = []
return children
children = []
return children
return True
for p in pathnames:
seen = []
for s in seen:
for m in matches:
return nodes
for i in names:
if m.teleporterEnabled:
print "%sMachine '%s' [%s], machineState=%s, sessionState=%s" %(tele,colVm(ctx,m.name),m.id,asEnumElem(ctx, "MachineState", m.state), 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 [guest win machine.setHWVirtExProperty(ctx[\\'const\\'].HWVirtExPropertyType_Enabled,value)]: " + asState(hwVirtEnabled)
print " VPID support [guest win machine.setHWVirtExProperty(ctx[\\'const\\'].HWVirtExPropertyType_VPID,value)]: " + asState(hwVirtVPID)
print " Nested paging [guest win machine.setHWVirtExProperty(ctx[\\'const\\'].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:
while True:
except KeyboardInterrupt:
if prompt:
if not line:
raise EOFError
return line
import getpass
if mach == None:
print "usage: gcat [vmname|uuid] local_file | guestProgram, such as gcat linux /home/nike/.bashrc | sh -c 'cat >'"
if mach == None:
env = []
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
global g_hascolors
print " processor #%d speed: %dMHz %s" %(i,host.getProcessorSpeed(i), host.getProcessorDescription(i))
if mach == None:
cmdExistingVm(ctx, mach, 'guestlambda', [lambda ctx,mach,console,args: monitorSource(ctx, console.eventSource, active, dur)])
if mach == None:
if mach == None:
while True:
if mach == None:
while True:
for mt in m:
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 %s [logical %s]" %(colPath(ctx,hdd.location), hdd.format, optId(verbose,hdd.id),colSizeM(ctx,asSize(hdd.size, True)), colSizeM(ctx,asSize(hdd.logicalSize, False)))
print " %s (%s)%s %s" %(colPath(ctx,dvd.location), dvd.format,optId(verbose,dvd.id),colSizeM(ctx,asSize(dvd.size, True)))
print " %s (%s)%s %s" %(colPath(ctx,floppy.location), floppy.format,optId(verbose,floppy.id), colSizeM(ctx,asSize(floppy.size, True)))
for a in atts:
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:
if mach is None:
if mach is None:
except KeyboardInterrupt:
if mach is None:
if persistent:
cmdExistingVm(ctx, mach, 'guestlambda', [lambda ctx,mach,console,args: console.createSharedFolder(name, path, writable)])
if mach is None:
if not found:
cmdExistingVm(ctx, mach, 'guestlambda', [lambda ctx,mach,console,args: console.removeSharedFolder(name)])
if mach is None:
cmdAnyVm(ctx, mach, lambda ctx,mach,console,args: progressBar(ctx, console.takeSnapshot(name,desc)))
cmdAnyVm(ctx, mach, lambda ctx,mach,console,args: progressBar(ctx, console.deleteSnapshot(snap.id)))
alias = {
socksndbuf/sockrcvbuf - sets amount of kb for socket sending/receiving buffer
msg = 'mtu:{0} socket(snd:{1}, rcv:{2}) tcpwnd(snd:{3}, rcv:{4})'.format(mtu, socksndbuf, sockrcvbuf, tcpsndwnd, tcprcvwnd);
msg = 'passdomain:{0}, proxy:{1}, usehostresolver:{2}'.format(yesno[int(nat.dnsPassDomain)], yesno[int(nat.dnsProxy)], yesno[int(nat.dnsUseHostResolver)])
if server is None:
if server is None:
if prefix is None:
if bootfile is None:
msg = []
pfcmd = {
'func': lambda: nat.addRedirect(args[3], proto[args[2]], args[4], int(args[5]), args[6], int(args[7]))
natcommands = {
if mach == None:
if len(args) < 3 or not args[2].isdigit() or int(args[2]) not in range(0, ctx['vb'].systemProperties.networkAdapterCount):
print 'please specify adapter num {0} isn\'t in range [0-{1}]'.format(args[2], ctx['vb'].systemProperties.networkAdapterCount)
cmdargs = []
session = None
elif report is not None:
for r in report:
print msg
usage: nic <vm> <nicnum> attachment [Null|NAT|Bridged <interface>|Internal <name>|HostOnly <interface>]
import types
(r, p) = nicAttachmentType[t]
niccomand = {
if vm is None:
print 'please specify adapter num %d isn\'t in range [0-%d]'%(args[2], ctx['vb'].systemProperties.networkAdapterCount)
session = None
if report is not None:
for e in elems:
'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],
'findLog':['Show entries matching pattern in log file of the VM, : findLog Win32 PDM|CPUM', findLogCmd, 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],
'attachUsb': ['Attach USB device to the VM (use listUsb to show available devices): attachUsb win uuid', attachUsbCmd, 0],
'shareFolder': ['Make host\'s folder visible to guest: shareFolder win /share share writable', shareFolderCmd, 0],
'foreach' : ['Generic "for each" construction, using XPath-like notation: foreach //vms/vm[@OSTypeId=\'MacOS\'] "print obj.name"', foreachCmd, 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:
commands['connect'] = ["Connect to remote VBox instance: connect http://server:18083 user password", connectCmd, 0]
if vbox is not None:
global g_hascolors
cmds = []
if g_cmd is not None:
while True:
if g_batchmode:
elif g_cmd is not None:
if g_batchmode:
except KeyboardInterrupt:
except StopIteration:
except EOFError:
except Exception,e:
if g_verbose:
if g_hasreadline:
if mach == None:
style = None
params = None
script_file = None
parse.add_option("-v", "--verbose", dest="verbose", action="store_true", default=False, help = "switch on verbose")
parse.add_option("-a", "--autopath", dest="autopath", action="store_true", default=False, help = "switch on autopath")
parse.add_option("-w", "--webservice", dest="style", action="store_const", const="WEBSERVICE", help = "connect to webservice")
params = {}
params[k] = v
params = None