Skip to content

Instantly share code, notes, and snippets.

@Zrubi
Last active July 11, 2021 22:48
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Zrubi/6229d5400bde987b1aa8da516553b909 to your computer and use it in GitHub Desktop.
Save Zrubi/6229d5400bde987b1aa8da516553b909 to your computer and use it in GitHub Desktop.
#!/usr/bin/python2
# -*- encoding: utf8 -*-
#
# The Qubes OS Project, http://www.qubes-os.org
#
# Copyright (C) 2016 Laszlo Zrubecz <mail@zrubi.hu>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
#
from qubes.qubes import QubesVmCollection
from qubes.qubes import QubesHost
from qubes.qubes import QubesException
from optparse import OptionParser
import sys
import pprint
usage = "usage: %prog [options] <vm-name>"
parser = OptionParser (usage)
parser.add_option("--dot", dest="dot",
action="store_true", default=False,
help="Output in DOT format")
(options, args) = parser.parse_args ()
qvm_collection = QubesVmCollection()
qvm_collection.lock_db_for_reading()
qvm_collection.load()
qvm_collection.unlock_db()
#print qvm_collection
data = {}
for vm in qvm_collection.values():
data_row = {}
data_row['name'] = vm.name
if vm.is_netvm() and not vm.is_proxyvm():
data_row['netvm'] = 'n/a'
elif vm.netvm is None:
data_row['netvm'] = '-'
else:
data_row['netvm'] = vm.netvm.name
data_row['type'] = vm.type
data_row['ip'] = vm.ip
data_row['ip_back'] = vm.gateway if vm.is_netvm() else 'n/a'
data_row['gw'] = vm.netvm.gateway if vm.netvm else 'n/a'
data_row['pci'] = vm.pcidevs
data[vm.qid] = data_row
#print data_row
#for vm in data:
# print vm
#pprint.pprint(data)
if options.dot:
print "digraph Qubes {"
for vmid in data:
if data[vmid]['netvm'] == "-" or data[vmid]['netvm'] == "n/a":
print " \"%s\"" % data[vmid]['name']
else:
print " \"%s\" -> \"%s\"" % (data[vmid]['name'],data[vmid]['netvm'])
print "}"
@marmarek
Copy link

qvm_collection[vm.netvm.qid].name -> vm.netvm.name :)

@andrewdavidwong
Copy link

Proposal to integrate this (or a similar tool) into Qubes: QubesOS/qubes-issues#2575

@Zrubi
Copy link
Author

Zrubi commented Jan 13, 2017

Of course this is still just a barebone.
We can use several other graph features to make the result more useful :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment