# -*- coding: utf-8 -*-
# $Id$
"""
VirtualBox Python Shell.
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, supports TAB-completion and
history if you have Python readline installed.
Finally, shell allows arbitrary custom extensions, just create
.VirtualBox/shexts/ and drop your extensions there.
Enjoy.
P.S. Our apologies for the code quality.
"""
__copyright__ = \
"""
Copyright (C) 2009-2015 Oracle Corporation
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.
"""
import traceback
import shlex
import time
import re
import platform
#
# Global Variables
#
g_sScriptFile = None
g_sCmd = None
try:
import readline
import rlcompleter
except ImportError:
g_dTermColors = {
'red': '\033[31m',
'blue': '\033[94m',
'green': '\033[92m',
'yellow': '\033[93m',
'magenta': '\033[35m',
'cyan': '\033[36m'
}
"""
Translates a string to one including coloring settings, if enabled.
"""
if not g_fHasColors:
return strg
if col:
return strg
if g_fHasReadline:
"""
taken from:
"""
return ['\t', None][state]
else:
if firstWord:
return True
return True
return False
"""
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:
if g_fVerbose:
return matches
if not g_fHasReadline:
return
comps = {}
# OSX need it
# Doesn't work well
# readline.parse_and_bind ("bind ^R em-inc-search-prev")
def split_no_quotes(s):
try:
return 1
except KeyboardInterrupt:
print "Interrupted."
if progress.cancelable:
print "Canceling task..."
return 0
if oVBoxMgr.errIsOurXcptKind(e):
else:
if errorinfo:
# update cache
if mach:
print "Success!"
else:
# update cache
# we ignore exceptions to allow starting VM even if
# perf collector cannot be started
if perf:
try:
except Exception, e:
if g_fVerbose:
class CachedMach:
if mach.accessible:
else:
result = []
return result
if ctx['vb'] is not None:
if simple:
return ctx['_machlistsimple']
else:
return ctx['_machlist']
else:
return []
if var:
else:
if var:
return 'yes'
else:
return 'no'
if not ctx['perf']:
return
exec cmds
print "Mouse : mode=%d x=%d y=%d z=%d w=%d buttons=%x" % (mev.mode, mev.x, mev.y, mev.z, mev.w, mev.buttons)
if scev:
if stev:
if gpcev:
if psev:
if shape is None:
print "pointer shape event - empty shape"
else:
if mev:
if kev:
if mtev:
pass
try:
# a bit convoluted QI to make it work with MS COM
except:
pass
if active:
else:
if dur == -1:
# not infinity, but close enough
try:
if active:
else:
if event:
# otherwise waitable events will leak (active listeners ACK automatically)
# We need to catch all exceptions here, otherwise listener will never be unregistered
except:
pass
if listener and registered:
global g_tsLast
def stamp():
global g_tsLast
return timePassed
#print "got event: %s %s" % (str(evtype), asEnumElem(ctx, 'VBoxEventType', evtype))
if mev:
if kev:
# we create an aggregated event source to listen for multiple event sources (keyboard and mouse in our case)
agg = console.eventSource.createAggregator([console.keyboard.eventSource, console.mouse.eventSource])
if dur == -1:
# not infinity, but close enough
dur = 100000
try:
if event:
# We need to catch all exceptions here, otherwise listener will never be unregistered
except:
pass
if listener and registered:
if dur == -1:
# not infinity, but close enough
dur = 100000
print "Header is", header
try:
break
if match is None:
continue
if rtype == 'k':
#print "KBD:", codes
elif rtype == 'm':
if mm is not None:
# absolute
#print "MA: ", mdict['x'], mdict['y'], mdict['z'], mdict['b']
mouse.putMouseEventAbsolute(int(mdict['x']), int(mdict['y']), int(mdict['z']), int(mdict['w']), int(mdict['b']))
else:
#print "MR: ", mdict['x'], mdict['y'], mdict['b']
mouse.putMouseEvent(int(mdict['x']), int(mdict['y']), int(mdict['z']), int(mdict['w']), int(mdict['b']))
# We need to catch all exceptions here, to close file
except KeyboardInterrupt:
except:
pass
f = args[0]
else:
f = "/tmp/screenshot.png"
else:
screen = 0
else:
w = fbw
else:
h = fbh
print "Saving screenshot (%d x %d) screen %d in %s..." % (w, h, screen, f)
size = (w, h)
mode = "RGBA"
f = args[0]
else:
f = "/tmp/screenshot.png"
else:
screen = 0
else:
w = fbw
else:
h = fbh
print "Saving screenshot (%d x %d) screen %d in %s..." % (w, h, screen, f)
print "Use host:port format for teleport target"
return
else:
passwd = ""
else:
maxDowntime = 250
print "Success!"
else:
# we need to set up guest statistics
else:
update = 1
try:
except:
# to allow sleep interruption
pass
cpu = 0
try:
print "%s: %d" % (s, val)
except:
# likely not implemented
pass
print "Adding CPU %d..." % (cpu)
print "Removing CPU %d..." % (cpu)
if c:
return v1
else:
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"))
print "Support seamless: %s" % (getFacilityStatus(ctx, guest, ctx['const'].AdditionsFacilityType_Seamless))
print "Support graphics: %s" % (getFacilityStatus(ctx, guest, ctx['const'].AdditionsFacilityType_Graphics))
else:
print "No additions"
print "Attached USB:"
print "Remote USB:"
print "Transient shared folders:"
session = None
try:
except Exception, e:
if g_fVerbose:
return
return
# this could be an example how to handle local only (i.e. unavailable
# in Webservices) functionality
print 'Trying to use local only functionality, ignored'
return
}
try:
except KeyboardInterrupt:
except Exception, e:
if g_fVerbose:
try:
except Exception, e:
if g_fVerbose:
if save:
try:
except Exception, e:
if g_fVerbose:
try:
except Exception, e:
if g_fVerbose:
if save:
try:
except:
return mach
class XPathNode:
matches = []
for e in children:
return matches
return []
return True
return False
try:
if match is not None:
except:
pass
return matches
children = []
return children
#def matches(self, subexp):
# return subexp=='vm'
children = []
return children
return subexp == 'nic'
return True
seen = []
for s in seen:
break
return nodes
return None
if mach == None:
print "Machine '%s' is unknown, use list command to find available machines" % (uuid)
return mach
if sp != 0:
else:
spec = ""
print "Help page:"
for i in names:
else:
if c == None:
print "Command '%s' not known" % (cmd)
else:
return 0
return colored(e, 'green')
try:
tele = "[T] "
else:
tele = " "
print "%sMachine '%s' [%s], machineState=%s, sessionState=%s" % (tele, colVm(ctx, mach.name), mach.id, asEnumElem(ctx, "MachineState", mach.state), asEnumElem(ctx, "SessionState", mach.sessionState))
except Exception, e:
if g_fVerbose:
return 0
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 []."
print " Firmware [firmwareType]: %s (%s)" % (asEnumElem(ctx, "FirmwareType", mach.firmwareType), mach.firmwareType)
print
print " Chipset [chipsetType]: %s (%s)" % (asEnumElem(ctx, "ChipsetType", mach.chipsetType), mach.chipsetType)
print
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
print
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)
# OSE has no VRDE
try:
except:
pass
print
print " '%s': type %s standard: %#x" \
print
if controllers:
print
for controller in controllers:
print " '%s': bus %s type %s" % (controller.name, asEnumElem(ctx, "StorageBus", controller.bus), asEnumElem(ctx, "StorageControllerType", controller.controllerType))
if attaches:
print
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)
print " HDD:"
print " DVD:"
if medium:
if a.passthrough:
print " [passthrough mode]"
else:
print " Floppy:"
if medium:
else:
print
return 0
print "usage: start name <frontend>"
return 0
if mach == None:
return 0
else:
vmtype = "gui"
return 0
print "usage: createvm name ostype"
return 0
try:
except Exception:
print 'Unknown OS type:', oskind
return 0
return 0
print "usage: ginfo [vmname|uuid]"
return 0
if mach == None:
return 0
return 0
print "exec in guest needs at least program name"
return
# shall contain program name as argv[0]
flags = 0
if inputPipe is not None:
print args[0]
if pid != 0:
try:
while True:
if inputPipe is not None:
if indata is not None:
off = 0
while write > 0:
else:
# EOF
try:
except:
pass
continue
if outputPipe is not None:
else:
continue
break
except KeyboardInterrupt:
print "Interrupted."
if progress.cancelable:
print "Exit code: %d" % (code)
return 0
else:
flags = 0
if prompt:
if not line:
raise EOFError
return line
import getpass
print "usage: gexec [vmname|uuid] command args"
return 0
if mach == None:
return 0
env = [] # ["DISPLAY=:0"]
gargs.insert(0, lambda ctx, mach, console, args: execInGuest(ctx, console, args, env, user, passwd, 10000))
return 0
print "usage: gcopy [vmname|uuid] host_path guest_path"
return 0
if mach == None:
return 0
return 0
try:
except:
return None
print "usage: gpipe [vmname|uuid] hostProgram guestProgram, such as gpipe linux '/bin/uname -a' '/bin/sh -c \"/usr/bin/tee; /bin/uname -a\"'"
return 0
if mach == None:
return 0
import subprocess
env = []
gargs.insert(0, lambda ctx, mach, console, args: execInGuest(ctx, console, args, env, user, passwd, 10000, lambda ctx:readCmdPipe(ctx, hcmd)))
try:
except:
pass
ctx['process'] = None
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
else:
return 0
print "usage: screenshot vm <file> <width> <height> <monitor>"
return 0
if mach == None:
return 0
return 0
print "usage: teleport name host:port <password>"
return 0
if mach == None:
return 0
return 0
if enabled:
print "usage: openportal name port <password>"
return 0
if mach == None:
return 0
else:
passwd = ""
return 0
print "usage: closeportal name"
return 0
if mach == None:
return 0
return 0
print "usage: gueststats name <check interval>"
return 0
if mach == None:
return 0
return 0
if plug:
print "Adding CPU %d..." % (cpu)
else:
print "Removing CPU %d..." % (cpu)
print "usage: plugcpu name cpuid"
return 0
if mach == None:
return 0
else:
return 0
print "usage: unplugcpu name cpuid"
return 0
if mach == None:
return 0
else:
return 0
print "Executing", expr
exec expr
print "usage: setvar [vmname|uuid] expr value"
return 0
if mach == None:
return 0
return 0
print "usage: setextra [vmname|uuid|global] key <value>"
return 0
else:
value = None
return 0
if mach == None:
return 0
return 0
print "usage: getextra [vmname|uuid|global] <key>"
return 0
else:
key = None
else:
if obj == None:
return 0
if key == None:
else:
for k in keys:
return 0
return 1
return 0
return 0
global g_fVerbose
else:
g_fVerbose = not g_fVerbose
return 0
global g_fHasColors
else:
g_fHasColors = not g_fHasColors
return 0
try:
except Exception, e:
if g_fVerbose:
#print "Global shared folders:"
#for ud in ctx['global'].getArray(vbox, 'sharedFolders'):
# printSf(ctx, sf)
print " processor #%d speed: %dMHz %s" % (i, host.getProcessorSpeed(i), host.getProcessorDescription(i))
else:
if ctx['perf']:
return 0
print "usage: monitorGuest name (duration)"
return 0
if mach == None:
return 0
dur = 5
cmdExistingVm(ctx, mach, 'guestlambda', [lambda ctx, mach, console, args: monitorSource(ctx, console.eventSource, active, dur)])
return 0
print "usage: monitorGuestKbd name (duration)"
return 0
if mach == None:
return 0
dur = 5
cmdExistingVm(ctx, mach, 'guestlambda', [lambda ctx, mach, console, args: monitorSource(ctx, console.keyboard.eventSource, active, dur)])
return 0
print "usage: monitorGuestMouse name (duration)"
return 0
if mach == None:
return 0
dur = 5
cmdExistingVm(ctx, mach, 'guestlambda', [lambda ctx, mach, console, args: monitorSource(ctx, console.mouse.eventSource, active, dur)])
return 0
print "usage: monitorGuestMultiTouch name (duration)"
return 0
if mach == None:
return 0
dur = 5
cmdExistingVm(ctx, mach, 'guestlambda', [lambda ctx, mach, console, args: monitorSource(ctx, console.mouse.eventSource, active, dur)])
return 0
print "usage: monitorVBox (duration)"
return 0
dur = 5
return 0
return "pcnet"
return "e1000"
return "virtio"
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 = 0
uOffset = 0
while True:
break
# print adds either NL or space to chunks not ending with a NL
return 0
print "usage: findLog vm pattern <num>"
return 0
if mach == None:
return 0
log = 0
uOffset = 0
while True:
# to reduce line splits on buffer boundary
break
for s in d:
print s
return 0
print "usage: findAssert vm <num>"
return 0
if mach == None:
return 0
log = 0
uOffset = 0
context = 0
while True:
# to reduce line splits on buffer boundary
break
for s in d:
if active:
print s
if context == 0:
else:
continue
context = 50
print s
return 0
try:
exec expr
except Exception, e:
if g_fVerbose:
return 0
# maybe will want more args smartness
return 0
print "usage: runScript <script>"
return 0
try:
except IOError, e:
return 0
try:
break
except Exception, e:
if g_fVerbose:
return 0
print "usage: sleep <secs>"
return 0
try:
except:
# to allow sleep interrupt
pass
return 0
print "usage: shell <commands>"
return 0
try:
except KeyboardInterrupt:
# to allow shell command interruption
pass
return 0
print "usage: connect url <username> <passwd>"
return 0
if ctx['vb'] is not None:
print "Already connected, disconnect first..."
return 0
else:
url = None
else:
user = ""
else:
passwd = ""
try:
except Exception, e:
if g_fVerbose:
return 0
print "usage: disconnect"
return 0
if ctx['vb'] is None:
print "Not connected yet."
return 0
try:
except:
ctx['vb'] = None
raise
ctx['vb'] = None
return 0
if ctx['wsinfo'] is None:
print "Never connected..."
return 0
try:
except:
pass
try:
except Exception, e:
if g_fVerbose:
return 0
print "usage: exportVm <machine> <path> <format> <license>"
return 0
if mach is None:
return 0
else:
fmt = "ovf-1.0"
else:
lic = "GPL"
else:
return 0
# PC XT scancodes
scancodes = {
'a': 0x1e,
'b': 0x30,
'c': 0x2e,
'd': 0x20,
'e': 0x12,
'f': 0x21,
'g': 0x22,
'h': 0x23,
'i': 0x17,
'j': 0x24,
'k': 0x25,
'l': 0x26,
'm': 0x32,
'n': 0x31,
'o': 0x18,
'p': 0x19,
'q': 0x10,
'r': 0x13,
's': 0x1f,
't': 0x14,
'u': 0x16,
'v': 0x2f,
'w': 0x11,
'x': 0x2d,
'y': 0x15,
'z': 0x2c,
'0': 0x0b,
'1': 0x02,
'2': 0x03,
'3': 0x04,
'4': 0x05,
'5': 0x06,
'6': 0x07,
'7': 0x08,
'8': 0x09,
'9': 0x0a,
' ': 0x39,
'-': 0xc,
'=': 0xd,
'[': 0x1a,
']': 0x1b,
';': 0x27,
'\'': 0x28,
',': 0x33,
'.': 0x34,
'/': 0x35,
'\t': 0xf,
'\n': 0x1c,
'`': 0x29
}
extScancodes = {
'ESC' : [0x01],
'BKSP': [0xe],
'SPACE': [0x39],
'TAB': [0x0f],
'CAPS': [0x3a],
'ENTER': [0x1c],
'LSHIFT': [0x2a],
'RSHIFT': [0x36],
'INS': [0xe0, 0x52],
'DEL': [0xe0, 0x53],
'END': [0xe0, 0x4f],
'HOME': [0xe0, 0x47],
'PGUP': [0xe0, 0x49],
'PGDOWN': [0xe0, 0x51],
'LGUI': [0xe0, 0x5b], # GUI, aka Win, aka Apple key
'RGUI': [0xe0, 0x5c],
'LCTR': [0x1d],
'RCTR': [0xe0, 0x1d],
'LALT': [0x38],
'RALT': [0xe0, 0x38],
'APPS': [0xe0, 0x5d],
'F1': [0x3b],
'F2': [0x3c],
'F3': [0x3d],
'F4': [0x3e],
'F5': [0x3f],
'F6': [0x40],
'F7': [0x41],
'F8': [0x42],
'F9': [0x43],
'F10': [0x44 ],
'F11': [0x57],
'F12': [0x58],
'UP': [0xe0, 0x48],
'LEFT': [0xe0, 0x4b],
'DOWN': [0xe0, 0x50],
'RIGHT': [0xe0, 0x4d],
}
if code != 0:
return [code]
print "bad ext", ch
return extCode
return codes
pressed = []
i = 0
i = i+1
if ch == '{':
# start group, all keys to be pressed at the same time
continue
if ch == '}':
# end group, release all keys
for c in pressed:
pressed = []
continue
if ch == 'W':
# just wait a bit
continue
if ch == '^':
ch = 'LCTR'
if ch == '|':
ch = 'LSHIFT'
if ch == '_':
ch = 'LALT'
if ch == '$':
ch = 'LGUI'
if not group:
else:
if ch == '\\':
i = i+1
if ch == 'n':
ch = '\n'
elif ch == '&':
combo = ""
i = i+1
if ch == ';':
break
if not group and modGroupEnd:
for c in pressed:
pressed = []
print "usage: typeGuest <machine> <text> <charDelay>"
return 0
if mach is None:
return 0
else:
delay = 0.1
return 0
if verbose:
return ": "+uuid
else:
return ""
if inBytes:
else:
else:
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, True)))
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)))
return 0
print "usage: listUsb"
return 0
return 0
for a in atts:
return [None, 0, 0]
print "usage: createHdd sizeM location type"
return 0
else:
fmt = "vdi"
hdd = ctx['vb'].createMedium(format, loc, ctx['global'].constants.AccessMode_ReadWrite, ctx['global'].constants.DeviceType_HardDisk)
else:
print "cannot create disk (file %s exist?)" % (loc)
return 0
return 0
print "usage: registerHdd location"
return 0
imageId = ""
parentId = ""
hdd = vbox.openMedium(loc, ctx['global'].constants.DeviceType_HardDisk, ctx['global'].constants.AccessMode_ReadWrite, false)
return 0
print "usage: attachHdd vm hdd controller port:slot"
return 0
if mach is None:
return 0
try:
hdd = vbox.openMedium(loc, ctx['global'].constants.DeviceType_HardDisk, ctx['global'].constants.AccessMode_ReadWrite, false)
except:
print "no HDD with path %s registered" % (loc)
return 0
else:
cmdClosedVm(ctx, mach, lambda ctx, mach, args: mach.attachDevice(ctr, port, slot, ctx['global'].constants.DeviceType_HardDisk, hdd.id))
return 0
for a in atts:
if a.medium:
print "usage: detachHdd vm hdd"
return 0
if mach is None:
return 0
try:
hdd = vbox.openMedium(loc, ctx['global'].constants.DeviceType_HardDisk, ctx['global'].constants.AccessMode_ReadWrite, false)
except:
print "no HDD with path %s registered" % (loc)
return 0
return 0
print "usage: unregisterHdd path <vmunreg>"
return 0
else:
vmunreg = 0
try:
hdd = vbox.openMedium(loc, ctx['global'].constants.DeviceType_HardDisk, ctx['global'].constants.AccessMode_ReadWrite, false)
except:
print "no HDD with path %s registered" % (loc)
return 0
if vmunreg != 0:
try:
print "Trying to detach from %s" % (mach)
except Exception, e:
print 'failed: ', e
return 0
return 0
print "usage: removeHdd path"
return 0
try:
hdd = vbox.openMedium(loc, ctx['global'].constants.DeviceType_HardDisk, ctx['global'].constants.AccessMode_ReadWrite, false)
except:
print "no HDD with path %s registered" % (loc)
return 0
return 0
print "usage: registerIso location"
return 0
iso = vbox.openMedium(loc, ctx['global'].constants.DeviceType_DVD, ctx['global'].constants.AccessMode_ReadOnly, false)
return 0
print "usage: unregisterIso path"
return 0
try:
dvd = vbox.openMedium(loc, ctx['global'].constants.DeviceType_DVD, ctx['global'].constants.AccessMode_ReadOnly, false)
except:
print "no DVD with path %s registered" % (loc)
return 0
return 0
print "usage: removeIso path"
return 0
try:
dvd = vbox.openMedium(loc, ctx['global'].constants.DeviceType_DVD, ctx['global'].constants.AccessMode_ReadOnly, false)
except:
print "no DVD with path %s registered" % (loc)
return 0
else:
return 0
print "usage: attachIso vm iso controller port:slot"
return 0
if mach is None:
return 0
try:
dvd = vbox.openMedium(loc, ctx['global'].constants.DeviceType_DVD, ctx['global'].constants.AccessMode_ReadOnly, false)
except:
print "no DVD with path %s registered" % (loc)
return 0
else:
cmdClosedVm(ctx, mach, lambda ctx, mach, args: mach.attachDevice(ctr, port, slot, ctx['global'].constants.DeviceType_DVD, dvd))
return 0
print "usage: detachIso vm iso"
return 0
if mach is None:
return 0
try:
dvd = vbox.openMedium(loc, ctx['global'].constants.DeviceType_DVD, ctx['global'].constants.AccessMode_ReadOnly, false)
except:
print "no DVD with path %s registered" % (loc)
return 0
return 0
print "usage: mountIso vm iso controller port:slot"
return 0
if mach is None:
return 0
try:
dvd = vbox.openMedium(loc, ctx['global'].constants.DeviceType_DVD, ctx['global'].constants.AccessMode_ReadOnly, false)
except:
print "no DVD with path %s registered" % (loc)
return 0
else:
# autodetect controller and location, just find first controller with media == DVD
return 0
print "usage: unmountIso vm controller port:slot"
return 0
if mach is None:
return 0
else:
# autodetect controller and location, just find first controller with media == DVD
return 0
if ctrltype != None:
print "usage: attachCtr vm cname bus <type>"
return 0
if ctrltype == None:
return 0
else:
ctrltype = None
if mach is None:
return 0
if bus is None:
return 0
return 0
print "usage: detachCtr vm name"
return 0
if mach is None:
return 0
return 0
if (args[0]):
else:
print "usage: attachUsb vm deviceuid"
return 0
if mach is None:
return 0
return 0
print "usage: detachUsb vm deviceuid"
return 0
if mach is None:
return 0
return 0
print "usage: gui"
return 0
try:
except KeyboardInterrupt:
# to allow interruption
pass
return 0
print "usage: shareFolder vm path name <writable> <persistent>"
return 0
if mach is None:
return 0
for a in args[4:]:
if a == 'writable':
if a == 'persistent':
if persistent:
else:
cmdExistingVm(ctx, mach, 'guestlambda', [lambda ctx, mach, console, args: console.createSharedFolder(name, path, writable)])
return 0
print "usage: unshareFolder vm name"
return 0
if mach is None:
return 0
break
if not found:
cmdExistingVm(ctx, mach, 'guestlambda', [lambda ctx, mach, console, args: console.removeSharedFolder(name)])
return 0
print "Take snapshot: snapshot vm take name <description>"
print "Restore snapshot: snapshot vm restore name"
print "Merge snapshot: snapshot vm merge name"
return 0
if mach is None:
return 0
if cmd == 'take':
print "usage: snapshot vm take name <description>"
return 0
else:
desc = ""
cmdAnyVm(ctx, mach, lambda ctx, mach, console, args: progressBar(ctx, mach.takeSnapshot(name, desc, true)))
return 0
if cmd == 'restore':
print "usage: snapshot vm restore name"
return 0
return 0
if cmd == 'restorecurrent':
print "usage: snapshot vm restorecurrent"
return 0
return 0
if cmd == 'delete':
print "usage: snapshot vm delete name"
return 0
cmdAnyVm(ctx, mach, lambda ctx, mach, console, args: progressBar(ctx, mach.deleteSnapshot(snap.id)))
return 0
print "Command '%s' is unknown" % (cmd)
return 0
usage: nat <vm> <nicnum> alias [default|[log] [proxyonly] [sameports]]
default - set settings to default values
log - switch on alias logging
proxyonly - switch proxyonly mode on
sameports - enforces NAT using the same ports
"""
alias = {
'log': 0x1,
'proxyonly': 0x2,
'sameports': 0x4
}
first = 0
msg = ''
if first == 0:
first = 1
else:
msg += ', '
else:
msg += ')'
return (0, [msg])
else:
if 'default' not in args:
print 'Invalid alias mode: ' + args[a]
return (1, None)
return (0, None)
usage: nat <vm> <nicnum> settings [<mtu> [[<socsndbuf> <sockrcvbuf> [<tcpsndwnd> <tcprcvwnd>]]]]
mtu - set mtu <= 16000
socksndbuf/sockrcvbuf - sets amount of kb for socket sending/receiving buffer
"""
msg = 'mtu:%s socket(snd:%s, rcv:%s) tcpwnd(snd:%s, rcv:%s)' % (mtu, socksndbuf, sockrcvbuf, tcpsndwnd, tcprcvwnd)
return (0, [msg])
else:
return (1, None)
print 'invalid %s parameter (%i not in range [8-1024])' % (i, args[i])
return (1, None)
a = [args[1]]
else:
#print a
return (0, None)
usage: nat <vm> <nicnum> dns [passdomain] [proxy] [usehostresolver]
passdomain - enforces builtin DHCP server to pass domain
proxy - switch on builtin NAT DNS proxying mechanism
usehostresolver - proxies all DNS requests to Host Resolver interface
"""
msg = 'passdomain:%s, proxy:%s, usehostresolver:%s' % (yesno[int(nat.DNSPassDomain)], yesno[int(nat.DNSProxy)], yesno[int(nat.DNSUseHostResolver)])
return (0, [msg])
else:
return (0, None)
usage nat <vm> <nicnum> tftp [prefix <prefix>| bootfile <bootfile>| server <server>]
prefix - alters prefix TFTP settings
bootfile - alters bootfile TFTP settings
server - sets booting server
"""
if server is None:
if server is None:
server += '.0'
server = '%d.%d.%d.4' % (a, b, c)
if prefix is None:
if bootfile is None:
return (0, [msg])
else:
print 'invalid args:', args
return (1, None)
else:
print "invalid cmd:", cmd
return (1, None)
return (0, None)
usage:
nat <vm> <nicnum> <pf> [ simple tcp|udp <hostport> <guestport>]
|[no_name tcp|udp <hostip> <hostport> <guestip> <guestport>]
|[ex tcp|udp <pf-name> <hostip> <hostport> <guestip> <guestport>]
|[delete <pf-name>]
"""
msg = []
else:
pfcmd = {
'simple': {
},
'no_name': {
},
'ex': {
'func': lambda: nat.addRedirect(args[3], proto[args[2]], args[4], int(args[5]), args[6], int(args[7]))
},
'delete': {
}
}
print natPortForwarding.__doc__
return (1, None)
return (0, None)
usage: nat <vm> <nicnum> network [<network>]
"""
else:
return (0, [msg])
else:
print 'Invalid arguments'
return (1, None)
return (0, None)
"""This command is entry point to NAT settins management
usage: nat <vm> <nicnum> <cmd> <cmd-args>
cmd - [alias|settings|tftp|dns|pf|network]
for more information about commands:
nat help <cmd>
"""
natcommands = {
'alias' : natAlias,
'settings' : natSettings,
'tftp': natTftp,
'dns': natDns,
'pf': natPortForwarding,
'network': natNetwork
}
else:
return 0
return 0
if mach == None:
print "please specify vm"
return 0
if len(args) < 3 or not args[2].isdigit() or int(args[2]) not in range(0, ctx['vb'].systemProperties.getMaxNetworkAdapters(mach.chipsetType)):
print 'please specify adapter num %d isn\'t in range [0-%d]' % (args[2], ctx['vb'].systemProperties.getMaxNetworkAdapters(mach.chipsetType))
return 0
cmdargs = []
# @todo vvl if nicnum is missed but command is entered
# use NAT func for every adapter on machine.
rosession = 1
session = None
rosession = 0
if rosession == 0:
if rc == 0:
elif report is not None:
for r in report:
print msg
return 0
return (0, r)
else:
return (1, None)
return (0, None)
'''
usage: nic <vm> <nicnum> trace [on|off [file]]
'''
return (0, r)
return (0, None)
return (0, r)
else:
print (1, None)
return (0, None)
'''
usage: nic <vm> <nicnum> cable [on|off]
'''
'''
usage: nic <vm> <nicnum> enable [on|off]
'''
'''
usage: nic <vm> <nicnum> type [Am79c970A|Am79c970A|I82540EM|I82545EM|I82543GC|Virtio]
'''
return (1, None)
else:
return (1, None)
return (0, None)
'''
usage: nic <vm> <nicnum> attachment [Null|NAT|Bridged <interface>|Internal <name>|HostOnly <interface>
'''
# @todo show details of the generic network attachment type
}
import types
else:
(r, p) = nicAttachmentType[t]
return (0, 'attachment:%s, name:%s' % (r, p))
else:
'Null': {
'p': lambda: 'do nothing',
'NAT': {
'p': lambda: 'do nothing',
'Bridged': {
'Internal': {
'HostOnly': {
# @todo implement setting the properties of a generic attachment
'Generic': {
'p': lambda: 'do nothing',
}
return (1, None)
print nicAttachmentType.__doc__
return (1, None)
return (0, None)
'''
This command to manage network adapters
usage: nic <vm> <nicnum> <cmd> <cmd-args>
where cmd : attachment, trace, linespeed, cable, enable, type
'''
# 'command name':{'runtime': is_callable_at_runtime, 'op': function_name}
niccomand = {
'attachment': nicAttachmentSubCmd,
'trace': nicTraceSubCmd,
'linespeed': nicLineSpeedSubCmd,
'cable': nicCableSubCmd,
'enable': nicEnableSubCmd,
'type': nicTypeSubCmd
}
else:
return 0
if vm is None:
print 'please specify vm'
return 0
print 'please specify adapter num %d isn\'t in range [0-%d]'% (args[2], ctx['vb'].systemProperties.getMaxNetworkAdapters(vm.chipsetType))
return 0
session = None
if rc == 0:
if report is not None:
return 0
return 0
return 0
return 0
try:
for e in elems:
except:
print "Error executing"
return 0
print "foreachvm command <args>"
return 0
return 0
print "usage: recordDemo vm filename (duration)"
return 0
if mach == None:
return 0
dur = 10000
cmdExistingVm(ctx, mach, 'guestlambda', [lambda ctx, mach, console, args: recordDemo(ctx, console, filename, dur)])
return 0
print "usage: playbackDemo vm filename (duration)"
return 0
if mach == None:
return 0
dur = 10000
cmdExistingVm(ctx, mach, 'guestlambda', [lambda ctx, mach, console, args: playbackDemo(ctx, console, filename, dur)])
return 0
for a in assigned:
if a.isPhysicalDevice:
print "%s: assigned host device %s guest %s" % (colDev(ctx, a.name), pciAddr(ctx, a.hostAddress), pciAddr(ctx, a.guestAddress))
for a in atts:
if a.isPhysicalDevice:
print "%s: physical, guest %s, host %s" % (colDev(ctx, a.name), pciAddr(ctx, a.guestAddress), pciAddr(ctx, a.hostAddress))
else:
return
if match is None:
return -1
print "usage: lspci vm"
return 0
if mach == None:
return 0
return 0
print "usage: attachpci vm hostpci <guestpci>"
return 0
if mach == None:
return 0
if hostaddr == -1:
return 0
if guestaddr == -1:
return 0
else:
return 0
print "usage: detachpci vm hostpci"
return 0
if mach == None:
return 0
if hostaddr == -1:
return 0
return 0
print "usage: goto line"
return 0
return 0
'i':'info',
'l':'list',
'h':'help',
'a':'alias',
'q':'quit', 'exit':'quit',
'tg': 'typeGuest',
'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, 0)\'', guestCmd, 0],
'monitorGuest':['Monitor what happens with the guest for some time: monitorGuest Win32 10', monitorGuestCmd, 0],
'monitorGuestKbd':['Monitor guest keyboard for some time: monitorGuestKbd Win32 10', monitorGuestKbdCmd, 0],
'monitorGuestMouse':['Monitor guest mouse for some time: monitorGuestMouse Win32 10', monitorGuestMouseCmd, 0],
'monitorGuestMultiTouch':['Monitor guest touch screen for some time: monitorGuestMultiTouch Win32 10', monitorGuestMultiTouchCmd, 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 0', 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 = args[0]
c = aliases[c]
if ci == None:
print "Unknown command: '%s', type 'help' for list of known commands" % (c)
return 0
if c not in ['connect', 'reconnect', 'help', 'quit']:
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 or 'reloadExt'.
#
# Also one can put shell extensions into ~/.VirtualBox/shexts and
# they will also be picked up, so this way one can exchange
# shell extensions easily.
return
d = {}
try:
for (k, v) in d['commands'].items():
if g_fVerbose:
print "customize: adding \"%s\" - %s" % (k, v[0])
except:
print "Error loading user extensions from %s" % (filename)
# also check 'exts' directory for all files
return
for e in exts:
# not editor temporary files, please.
if e.endswith('.py'):
else:
if ctx['remote']:
commands['connect'] = ["Connect to remote VBox instance: connect http://server:18083 user password", connectCmd, 0]
if vbox is not None:
try:
except Exception, e:
if g_fVerbose:
else:
ctx['perf'] = None
global g_fHasColors
# 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
cmds = []
if g_sCmd is not None:
while True:
try:
if g_fBatchMode:
elif g_sCmd is not None:
else:
if done != 0: break
if g_fBatchMode:
break
except KeyboardInterrupt:
print '====== You can type quit or q to leave'
except StopIteration:
break
except EOFError:
break
except Exception, e:
if g_fVerbose:
try:
# There is no need to disable metric collection. This is just an example.
if ct['perf']:
except:
pass
if g_fHasReadline:
if mach == None:
return 0
return 0
#
# Parse command line arguments.
#
parse = OptionParser()
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")
if options.batch_file is not None:
if options.command_line is not None:
params = None
params = {}
for strparam in strparamlist:
except: pass; # In case __file__ isn't there.
else:
if sPath is None:
for sCurLoc in asLocations:
print "Autodetected VBOX_PROGRAM_PATH as", sCurLoc
break;
if sPath:
if sPath is None:
for sCurLoc in asLocations:
print "Autodetected VBOX_SDK_PATH as", sCurLoc
break;
if sPath:
del sTmp;
del sPath, asLocations;
#
# Set up the shell interpreter context and
#
ctx = {
'global': oVBoxMgr,
'typeInGuest': typeInGuest,
'_machlist': None,
'prompt': g_sPrompt,
'scriptLine': 0,
'interrupt': False,
}
del oVBoxMgr
if __name__ == '__main__':