Skip to content

Instantly share code, notes, and snippets.

@biggers biggers/
Last active Dec 22, 2015

What would you like to do?
Python 'sh' module example, working with Virtualbox commands & outputs
#!/usr/bin/env python
from __future__ import print_function
from pprint import pprint
# install into a virtual-env: pip install sh
# References:
# Inspired by:
from pprint import pprint as pp
from sys import exit
from sh import VBoxManage as vbm
# "testing" the 'sh' Py module...
# ... dump some info, on VirtualBox VMs on this Linux "host"
VM_INFO_KEYS = ('name', 'ostype', 'UUID', 'CfgFile', 'SnapFldr', 'LogFldr', 'memory', 'vram', 'cpus', 'VMState', 'storagecontrollername1', 'SATA Controller', 'SharedFolderPathMachineMapping1', 'Forwarding',)
def get_clean_kv(item_raw, val_spl=' ', val_strp='"'):
raw_line = item_raw.strip()
key_raw, value_raw = raw_line.split( val_spl )
key = key_raw.strip('"')
value = value_raw.strip( val_strp )
return key, value
except ValueError:
return ('No k/v: ', raw_line)
name = None
for line in vbm.list.vms(_iter=True):
name, uuid = get_clean_kv( line, val_spl=' ', val_strp='{}' )
print( "{} = {}".format(name, uuid) )
# NOTE: we use the _last_ VM-name, from above...
for line in vbm.showvminfo(name, machinereadable=True, details=True, _iter=True):
key, value = get_clean_kv( line, val_spl='=')
if key in VM_INFO_KEYS:
print("{} = {}".format( key, value ))
#res = vbm.list.ostypes()
d = dict()
for line in vbm.list.ostypes( _iter=True ):
t = get_clean_kv( line, val_spl=':')
key, value = t
v = value.strip()
d[ key ] = v
if key in ('Family ID',):
if v in ('Linux',):
pprint( d, width=240 )
d = {}
if v == '':
d = {}
# print(res)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.