vboxshell.py revision 37edc26f1c16c973c93a4d3c2e207fb61f0f83af
#
# Copyright (C) 2009-2010 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.
#
#################################################################################
# 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. #
################################################################################
import traceback
import shlex
import time
import re
import platform
from optparse import OptionParser
g_scripfile = None
g_cmd = None
try:
if g_hasreadline:
import readline
import rlcompleter
except:
g_prompt = "vbox> "
term_colors = {
'red':'\033[31m',
'blue':'\033[94m',
'green':'\033[92m',
'yellow':'\033[93m',
'magenta':'\033[35m'
}
if not g_hascolors:
return string
global term_colors
if col:
else:
return string
if g_hasreadline:
import string
"""
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:
printErr(e)
if g_verbose:
return matches
if not g_hasreadline:
return
comps = {}
comps[k] = None
# OSX need it
# Doesn't work well
# readline.parse_and_bind ("bind ^R em-inc-search-prev")
def split_no_quotes(s):
try:
while not p.completed:
reportError(ctx, p)
return 1
except KeyboardInterrupt:
print "Interrupted."
if p.cancelable:
print "Canceling task..."
p.cancel()
return 0
if ei:
return colored(p, 'green')
return colored(m, 'red')
# update cache
if mach:
# update cache
# we ignore exceptions to allow starting VM even if
# perf collector cannot be started
if perf:
try:
except Exception,e:
if g_verbose:
# if session not opened, close doesn't make sense
else:
class CachedMach:
result = []
for m in list:
try:
elem = CachedMach(m)
except:
pass
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 : absolute=%d x=%d y=%d z=%d buttons=%x" %(mev.absolute, mev.x, mev.y, mev.z, mev.buttons)
def handleEventImpl(ev):
if scev:
if gpcev:
if psev:
if shape is None:
print "pointer shape event - empty shape"
else:
if mev:
if kev:
class EventListener:
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
dur = 100000
try:
if active:
else:
if ev:
# 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:
tsLast = 0
global tsLast
def stamp():
global tsLast
return rv
def handleEventImpl(ev):
#print "got event: %s %s" %(str(type), asEnumElem(ctx, 'VBoxEventType', type))
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 ev:
# 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 m is None:
continue
if type == 'k':
#print "KBD:",codes
elif type == '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:
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)
size = (w,h)
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"))
if guest.additionsActive:
else:
print "No additions"
print "Attached USB:"
print "Remote USB:"
print "Transient shared folders:"
session = None
try:
except Exception,e:
if g_verbose:
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 Exception, e:
if g_verbose:
try:
except Exception, e:
if g_verbose:
if save:
try:
except Exception, e:
if g_verbose:
try:
except Exception, e:
if g_verbose:
if save:
try:
except:
return mach
class XPathNode:
matches = []
for e in children:
return matches
return []
return True
return False
try:
if m is not None:
except:
pass
return matches
class XPathNodeHolder(XPathNode):
children = []
return children
class XPathNodeValue(XPathNode):
class XPathNodeHolderVM(XPathNodeHolder):
class XPathNodeVM(XPathNode):
#def matches(self,subexp):
# return subexp=='vm'
class XPathNodeHolderNIC(XPathNodeHolder):
children = []
return children
class XPathNodeNIC(XPathNode):
return subexp=='nic'
class XPathNodeRoot(XPathNode):
return True
for p in pathnames:
seen = []
for s in seen:
for m in matches:
break
return nodes
return None
if m == None:
print "Machine '%s' is unknown, use list command to find available machines" %(id)
return m
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:
if m.teleporterEnabled:
tele = "[T] "
else:
tele = " "
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))
except Exception, e:
if g_verbose:
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
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)
m = a.medium
print " HDD:"
print " Id: %s" %(m.id)
print " Name: %s" %(m.name)
print " Format: %s" %(m.format)
print " DVD:"
if m:
print " Id: %s" %(m.id)
print " Name: %s" %(m.name)
if m.hostDrive:
if a.passthrough:
print " [passthrough mode]"
else:
print " Size: %s" %(m.size)
print " Floppy:"
if m:
print " Id: %s" %(m.id)
print " Name: %s" %(m.name)
if m.hostDrive:
else:
print " Size: %s" %(m.size)
print
return 0
print "usage: start name <frontend>"
return 0
if mach == None:
return 0
else:
type = "gui"
return 0
print "usage: createvm name ostype <basefolder>"
return 0
else:
base = ''
try:
except Exception, e:
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]
print "executed with pid %d" %(pid)
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"]
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
print "'%s' is an alias for '%s'" %(k,v)
return 0
global g_verbose
else:
return 0
global g_hascolors
else:
g_hascolors = not g_hascolors
return 0
#print "Global shared folders:"
#for ud in ctx['global'].getArray(vb, '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: 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:
if len(m) > 0:
for mt in m:
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
if len(m) > 0:
context = 50
print s
return 0
try:
exec expr
except Exception, e:
if g_verbose:
return 0
# maybe will want more args smartness
return 0
print "usage: runScript <script>"
return 0
try:
except IOError,e:
return 0
try:
if done != 0: break
except Exception,e:
if g_verbose:
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 = ""
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
return 0
import sys
print "usage: exportVm <machine> <path> <format> <license>"
return 0
if mach is None:
return 0
else:
format = "ovf-1.0"
else:
license = "GPL"
else:
reportError(ctx,p)
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
import time
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 = []
import sys
print "usage: typeGuest <machine> <text> <charDelay>"
return 0
if mach is None:
return 0
else:
delay = 0.1
return 0
if verbose:
return ": "+id
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:
format = "vdi"
else:
print "cannot create disk (file %s exist?)" %(loc)
return 0
return 0
print "usage: registerHdd location"
return 0
imageId = ""
parentId = ""
hdd = vb.openHardDisk(loc, ctx['global'].constants.AccessMode_ReadWrite, setImageId, imageId, setParentId, parentId)
return 0
print "usage: attachHdd vm hdd controller port:slot"
return 0
if mach is None:
return 0
try:
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:
except:
print "no HDD with path %s registered" %(loc)
return 0
return 0
print "usage: unregisterHdd path <vmunreg>"
return 0
else:
vmunreg = 0
try:
except:
print "no HDD with path %s registered" %(loc)
return 0
if vmunreg != 0:
try:
for m in machs:
print "Trying to detach from %s" %(m)
except Exception, e:
print 'failed: ',e
return 0
return 0
print "usage: removeHdd path"
return 0
try:
except:
print "no HDD with path %s registered" %(loc)
return 0
return 0
print "usage: registerIso location"
return 0
id = ""
return 0
print "usage: unregisterIso path"
return 0
try:
except:
print "no DVD with path %s registered" %(loc)
return 0
return 0
print "usage: removeIso path"
return 0
try:
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:
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.id))
return 0
print "usage: detachIso vm iso"
return 0
if mach is None:
return 0
try:
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:
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 type != None:
print "usage: attachCtr vm cname bus <type>"
return 0
if type == None:
return 0
else:
type = 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, console.takeSnapshot(name,desc)))
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, console.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:{0} socket(snd:{1}, rcv:{2}) tcpwnd(snd:{3}, rcv:{4})'.format(mtu, socksndbuf, sockrcvbuf, tcpsndwnd, tcprcvwnd);
return (0, [msg])
else:
return (1, None)
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:{0}, proxy:{1}, usehostresolver:{2}'.format(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'
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.networkAdapterCount):
print 'please specify adapter num {0} isn\'t in range [0-{1}]'.format(args[2], ctx['vb'].systemProperties.networkAdapterCount)
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 (0, str(n))
return (1, None)
else:
return (1, None)
return (0, None)
'''
usage: nic <vm> <nicnum> attachment [Null|NAT|Bridged <interface>|Internal <name>|HostOnly <interface>]
'''
#ctx['global'].constants.NetworkAttachmentType_VDE: ('VDE', adapter.VDENetwork)
}
import types
else:
(r, p) = nicAttachmentType[t]
else:
'Null': {
'p': lambda: 'do nothing',
'NAT': {
'p': lambda: 'do nothing',
'Bridged': {
'Internal': {
'HostOnly': {
'VDE': {
}
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.networkAdapterCount)
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
for m in getMachines(ctx):
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
'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 keyboardfor some time: monitorGuestKbd Win32 10', monitorGuestKbdCmd, 0],
'monitorGuestMouse':['Monitor guest keyboardfor some time: monitorGuestMouse Win32 10', monitorGuestMouseCmd, 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_verbose:
print "customize: adding \"%s\" - %s" %(k, v[0])
except:
print "Error loading user extensions from %s" %(file)
# also check 'exts' directory for all files
return
for e in exts:
# not editor temporary files, please.
if e.endswith('.py'):
def getHomeFolder(ctx):
else:
if ctx['remote']:
commands['connect'] = ["Connect to remote VBox instance: connect http://server:18083 user password", connectCmd, 0]
if vbox is not None:
else:
ctx['perf'] = None
global g_hascolors
# 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_cmd is not None:
while True:
try:
if g_batchmode:
elif g_cmd is not None:
else:
if done != 0: break
if g_batchmode:
break
except KeyboardInterrupt:
print '====== You can type quit or q to leave'
except StopIteration:
break
except EOFError:
break
except Exception,e:
if g_verbose:
try:
# There is no need to disable metric collection. This is just an example.
if ct['perf']:
except:
pass
if g_hasreadline:
if mach == None:
return 0
return 0
style = None
params = None
script_file = None
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 = {}
for e in l:
(k,v) = e.split('=')
params[k] = v
else:
params = None
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
if vsp is not None :
print "Autodetected VBOX_SDK_PATH as",vsp
from vboxapi import VirtualBoxManager
'typeInGuest': typeInGuest,
'_machlist': None,
'prompt': g_prompt
}
if __name__ == '__main__':