Skip to main content
usage
Source Link
domson
  • 351
  • 2
  • 11
#!/bin/python
#> usage: ./query-dbus.py --pattern "^.*activit.*$"
import argparse
parser = argparse.ArgumentParser(description='query dbus tree')
parser.add_argument('-s','--system', help='query SystemBus', required=False, default=False)
parser.add_argument('-S','--session', help='query SessionBus (default)', required=False, default=True)
parser.add_argument('-p','--pattern', help='search pattern', required=False, default=None)
args = vars(parser.parse_args())

import re
pattern = None
if args['pattern']:
  pattern = re.compile(args['pattern'], re.IGNORECASE)

import dbus
from xml.etree import ElementTree

if args['session']:
  bus = dbus.SessionBus()
else:
  bus = dbus.SystemBus()

def busNames():
    return [ name for name in  bus.list_names() if not name.startswith(":") ]


def objectTree(service, object_path="/", paths=None, serviceDict=None):
    if str(service).startswith(':') == True: 
        return
      
    if paths == None:
        paths = {}
    pathdict = {}
    try:
      try:
          obj = bus.get_object(service, object_path)
      except dbus.exceptions.DBusException:
          print("[ERROR] service \"%s\" doesn't exist or isn't running" \
                % service)
          return
      
      try:
        iface = dbus.Interface(obj, 'org.freedesktop.DBus.Introspectable')
        xml_string = iface.Introspect()
      except dbus.exceptions.DBusException:
        print("[ERROR] service \"%s\" doesn't reply request. timeout." \
                % service)
        return
      root = ElementTree.fromstring(xml_string)
      for child in root:
          if child.tag == 'node':
              if object_path == '/':
                      object_path = ''
              new_path = '/'.join((object_path, child.attrib['name']))
              objectTree(service, new_path, paths)
          else:
              if object_path == "":
                  object_path = "/"
              functiondict = {}
            
              for func in child:
                if pattern is None or pattern.match(func.attrib["name"]):
                  if func.tag not in functiondict.keys():
                      functiondict[func.tag] = []
                  functiondict[func.tag].append(func.attrib["name"])
                  
              if functiondict:
                pathdict[child.attrib["name"]] = functiondict
      if serviceDict == None:
          serviceDict = {}
      if pathdict:
        paths[object_path] = pathdict
      if paths:
        serviceDict[service] = paths
      return serviceDict
    except:
      raise

import json
import random

for service in bus.list_names():
  object_tree = objectTree(service)
  if object_tree:
    print(json.dumps(object_tree,indent=3))
#!/bin/python
import argparse
parser = argparse.ArgumentParser(description='query dbus tree')
parser.add_argument('-s','--system', help='query SystemBus', required=False, default=False)
parser.add_argument('-S','--session', help='query SessionBus (default)', required=False, default=True)
parser.add_argument('-p','--pattern', help='search pattern', required=False, default=None)
args = vars(parser.parse_args())

import re
pattern = None
if args['pattern']:
  pattern = re.compile(args['pattern'], re.IGNORECASE)

import dbus
from xml.etree import ElementTree

if args['session']:
  bus = dbus.SessionBus()
else:
  bus = dbus.SystemBus()

def busNames():
    return [ name for name in  bus.list_names() if not name.startswith(":") ]


def objectTree(service, object_path="/", paths=None, serviceDict=None):
    if str(service).startswith(':') == True: 
        return
      
    if paths == None:
        paths = {}
    pathdict = {}
    try:
      try:
          obj = bus.get_object(service, object_path)
      except dbus.exceptions.DBusException:
          print("[ERROR] service \"%s\" doesn't exist or isn't running" \
                % service)
          return
      
      try:
        iface = dbus.Interface(obj, 'org.freedesktop.DBus.Introspectable')
        xml_string = iface.Introspect()
      except dbus.exceptions.DBusException:
        print("[ERROR] service \"%s\" doesn't reply request. timeout." \
                % service)
        return
      root = ElementTree.fromstring(xml_string)
      for child in root:
          if child.tag == 'node':
              if object_path == '/':
                      object_path = ''
              new_path = '/'.join((object_path, child.attrib['name']))
              objectTree(service, new_path, paths)
          else:
              if object_path == "":
                  object_path = "/"
              functiondict = {}
            
              for func in child:
                if pattern is None or pattern.match(func.attrib["name"]):
                  if func.tag not in functiondict.keys():
                      functiondict[func.tag] = []
                  functiondict[func.tag].append(func.attrib["name"])
                  
              if functiondict:
                pathdict[child.attrib["name"]] = functiondict
      if serviceDict == None:
          serviceDict = {}
      if pathdict:
        paths[object_path] = pathdict
      if paths:
        serviceDict[service] = paths
      return serviceDict
    except:
      raise

import json
import random

for service in bus.list_names():
  object_tree = objectTree(service)
  if object_tree:
    print(json.dumps(object_tree,indent=3))
#!/bin/python
#> usage: ./query-dbus.py --pattern "^.*activit.*$"
import argparse
parser = argparse.ArgumentParser(description='query dbus tree')
parser.add_argument('-s','--system', help='query SystemBus', required=False, default=False)
parser.add_argument('-S','--session', help='query SessionBus (default)', required=False, default=True)
parser.add_argument('-p','--pattern', help='search pattern', required=False, default=None)
args = vars(parser.parse_args())

import re
pattern = None
if args['pattern']:
  pattern = re.compile(args['pattern'], re.IGNORECASE)

import dbus
from xml.etree import ElementTree

if args['session']:
  bus = dbus.SessionBus()
else:
  bus = dbus.SystemBus()

def busNames():
    return [ name for name in  bus.list_names() if not name.startswith(":") ]


def objectTree(service, object_path="/", paths=None, serviceDict=None):
    if str(service).startswith(':') == True: 
        return
      
    if paths == None:
        paths = {}
    pathdict = {}
    try:
      try:
          obj = bus.get_object(service, object_path)
      except dbus.exceptions.DBusException:
          print("[ERROR] service \"%s\" doesn't exist or isn't running" \
                % service)
          return
      
      try:
        iface = dbus.Interface(obj, 'org.freedesktop.DBus.Introspectable')
        xml_string = iface.Introspect()
      except dbus.exceptions.DBusException:
        print("[ERROR] service \"%s\" doesn't reply request. timeout." \
                % service)
        return
      root = ElementTree.fromstring(xml_string)
      for child in root:
          if child.tag == 'node':
              if object_path == '/':
                      object_path = ''
              new_path = '/'.join((object_path, child.attrib['name']))
              objectTree(service, new_path, paths)
          else:
              if object_path == "":
                  object_path = "/"
              functiondict = {}
            
              for func in child:
                if pattern is None or pattern.match(func.attrib["name"]):
                  if func.tag not in functiondict.keys():
                      functiondict[func.tag] = []
                  functiondict[func.tag].append(func.attrib["name"])
                  
              if functiondict:
                pathdict[child.attrib["name"]] = functiondict
      if serviceDict == None:
          serviceDict = {}
      if pathdict:
        paths[object_path] = pathdict
      if paths:
        serviceDict[service] = paths
      return serviceDict
    except:
      raise

import json
import random

for service in bus.list_names():
  object_tree = objectTree(service)
  if object_tree:
    print(json.dumps(object_tree,indent=3))
complete solution
Source Link
domson
  • 351
  • 2
  • 11

According to the partly complete answer of Reegan Miranda I ported the code to python 3.9 and made it introspect the whole bus system by regex filter.

#!/bin/python
import argparse
parser = argparse.ArgumentParser(description='query dbus tree')
parser.add_argument('-s','--system', help='query SystemBus', required=False, default=False)
parser.add_argument('-S','--session', help='query SessionBus (default)', required=False, default=True)
parser.add_argument('-p','--pattern', help='search pattern', required=False, default=None)
args = vars(parser.parse_args())

import re
pattern = None
if args['pattern']:
  pattern = re.compile(args['pattern'], re.IGNORECASE)

import dbus
from xml.etree import ElementTree

if args['session']:
  bus = dbus.SessionBus()
else:
  bus = dbus.SystemBus()

def busNames():
    return [ name for name in  bus.list_names() if not name.startswith(":") ]


def pathNamesobjectTree(service, object_path="/", paths=None, serviceDict=None):
    if str(service).startswith(':') == True: 
        return
      
    if paths == None:
        paths = {}
    paths[object_path]pathdict = {}
    try:
      try:
          obj = bus.get_object(service, object_path)
      except dbus.exceptions.DBusException:
          print("[ERROR] service \"%s\" doesn't exist or isn't running" \
                % service)
          return
      
      try:
        iface = dbus.Interface(obj, 'org.freedesktop.DBus.Introspectable')
    try:
      xml_string = iface.Introspect()
      except dbus.exceptions.DBusException:
        print("[ERROR] service \"%s\" doesn't reply request. timeout." \
                % service)
        return
      root = ElementTree.fromstring(xml_string)
      for child in root:
          if child.tag == 'node':
              if object_path == '/':
                      object_path = ''
              new_path = '/'.join((object_path, child.attrib['name']))
              pathNamesobjectTree(service, new_path, paths)
          else:
              if object_path == "":
                  object_path = "/"
              functiondict = {}
              paths[object_path][child.attrib["name"]] = functiondict
              for func in child:
                if pattern is None or pattern.match(func.attrib["name"]):
                  if func.tag not in functiondict.keys():
                      functiondict[func.tag] =[]= []
                  functiondict[func.tag].append(func.attrib["name"])
                  
              if functiondict:
                pathdict[child.attrib["name"]] = functiondict
      if serviceDict == None:
          serviceDict = {}
      if pathdict:
        paths[object_path] = pathdict
      if paths:
        serviceDict[service] = paths
      return serviceDict
    except:
      pass

raise

import json
import random 

for service in dbusbus.SystemBuslist_names().list_names:
  object_tree = objectTree(service)
  if object_tree:
    print(json.dumps(pathNames(service)object_tree,indent=3))

According to the complete answer of Reegan Miranda I ported the code to python 3.9 and made it introspect the whole bus system.

#!/bin/python

import dbus
from xml.etree import ElementTree
bus = dbus.SystemBus()

def busNames():
    return [ name for name in  bus.list_names() if not name.startswith(":") ]


def pathNames(service,object_path="/",paths=None,serviceDict=None):
    if paths == None:
        paths = {}
    paths[object_path] = {}
    obj = bus.get_object(service, object_path)
    iface = dbus.Interface(obj, 'org.freedesktop.DBus.Introspectable')
    try:
      xml_string = iface.Introspect()
      root = ElementTree.fromstring(xml_string)
      for child in root:
          if child.tag == 'node':
              if object_path == '/':
                      object_path = ''
              new_path = '/'.join((object_path, child.attrib['name']))
              pathNames(service, new_path,paths)
          else:
              if object_path == "":
                  object_path = "/"
              functiondict = {}
              paths[object_path][child.attrib["name"]] = functiondict
              for func in child:
                  if func.tag not in functiondict.keys():
                      functiondict[func.tag] =[]
                  functiondict[func.tag].append(func.attrib["name"])
      if serviceDict == None:
          serviceDict = {}
      serviceDict[service] = paths
      return serviceDict
    except:
      pass



import json
import random
for service in dbus.SystemBus().list_names():
  print(json.dumps(pathNames(service),indent=3))

According to the partly complete answer of Reegan Miranda I ported the code to python 3.9 and made it introspect the whole bus system by regex filter.

#!/bin/python
import argparse
parser = argparse.ArgumentParser(description='query dbus tree')
parser.add_argument('-s','--system', help='query SystemBus', required=False, default=False)
parser.add_argument('-S','--session', help='query SessionBus (default)', required=False, default=True)
parser.add_argument('-p','--pattern', help='search pattern', required=False, default=None)
args = vars(parser.parse_args())

import re
pattern = None
if args['pattern']:
  pattern = re.compile(args['pattern'], re.IGNORECASE)

import dbus
from xml.etree import ElementTree

if args['session']:
  bus = dbus.SessionBus()
else:
  bus = dbus.SystemBus()

def busNames():
    return [ name for name in  bus.list_names() if not name.startswith(":") ]


def objectTree(service, object_path="/", paths=None, serviceDict=None):
    if str(service).startswith(':') == True: 
        return
      
    if paths == None:
        paths = {}
    pathdict = {}
    try:
      try:
          obj = bus.get_object(service, object_path)
      except dbus.exceptions.DBusException:
          print("[ERROR] service \"%s\" doesn't exist or isn't running" \
                % service)
          return
      
      try:
        iface = dbus.Interface(obj, 'org.freedesktop.DBus.Introspectable')
        xml_string = iface.Introspect()
      except dbus.exceptions.DBusException:
        print("[ERROR] service \"%s\" doesn't reply request. timeout." \
                % service)
        return
      root = ElementTree.fromstring(xml_string)
      for child in root:
          if child.tag == 'node':
              if object_path == '/':
                      object_path = ''
              new_path = '/'.join((object_path, child.attrib['name']))
              objectTree(service, new_path, paths)
          else:
              if object_path == "":
                  object_path = "/"
              functiondict = {}
            
              for func in child:
                if pattern is None or pattern.match(func.attrib["name"]):
                  if func.tag not in functiondict.keys():
                      functiondict[func.tag] = []
                  functiondict[func.tag].append(func.attrib["name"])
                  
              if functiondict:
                pathdict[child.attrib["name"]] = functiondict
      if serviceDict == None:
          serviceDict = {}
      if pathdict:
        paths[object_path] = pathdict
      if paths:
        serviceDict[service] = paths
      return serviceDict
    except:
      raise

import json
import random 

for service in bus.list_names():
  object_tree = objectTree(service)
  if object_tree:
    print(json.dumps(object_tree,indent=3))
Source Link
domson
  • 351
  • 2
  • 11

According to the complete answer of Reegan Miranda I ported the code to python 3.9 and made it introspect the whole bus system.

#!/bin/python

import dbus
from xml.etree import ElementTree
bus = dbus.SystemBus()

def busNames():
    return [ name for name in  bus.list_names() if not name.startswith(":") ]


def pathNames(service,object_path="/",paths=None,serviceDict=None):
    if paths == None:
        paths = {}
    paths[object_path] = {}
    obj = bus.get_object(service, object_path)
    iface = dbus.Interface(obj, 'org.freedesktop.DBus.Introspectable')
    try:
      xml_string = iface.Introspect()
      root = ElementTree.fromstring(xml_string)
      for child in root:
          if child.tag == 'node':
              if object_path == '/':
                      object_path = ''
              new_path = '/'.join((object_path, child.attrib['name']))
              pathNames(service, new_path,paths)
          else:
              if object_path == "":
                  object_path = "/"
              functiondict = {}
              paths[object_path][child.attrib["name"]] = functiondict
              for func in child:
                  if func.tag not in functiondict.keys():
                      functiondict[func.tag] =[]
                  functiondict[func.tag].append(func.attrib["name"])
      if serviceDict == None:
          serviceDict = {}
      serviceDict[service] = paths
      return serviceDict
    except:
      pass



import json
import random
for service in dbus.SystemBus().list_names():
  print(json.dumps(pathNames(service),indent=3))