Skip to content

Instantly share code, notes, and snippets.

@ciiqr
Last active September 5, 2022 10:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ciiqr/68c4c7b57f3616969086 to your computer and use it in GitHub Desktop.
Save ciiqr/68c4c7b57f3616969086 to your computer and use it in GitHub Desktop.
Simple dbus python usage
## Cheat Sheet
# Start
import dbus
bus = dbus.SystemBus()
# OR
bus = dbus.SessionBus()
# Bus name
some_bus_name = "com.example.SomeService"
# Interface Name
some_interface = "com.example.SomeService"
# Object Paths
some_base_path = "/com/example/SomeService"
some_sub_path = "/com/example/SomeService/SubPath"
# Get Objects
proxy_object = bus.get_object(some_bus_name, some_base_path)
proxy_object_devices = bus.get_object(some_bus_name, some_sub_path)
# Get Properties
some_property = proxy_object.Get(some_interface, 'SomeProperty',
dbus_interface=dbus.PROPERTIES_IFACE)
# Call Method
opt_ret_val = proxy_object.SomeMethod(opt_params,
dbus_interface=some_interface)
# Register for a Signal
proxy_object.connect_to_signal("SignalName", handler_for_signal_name,
dbus_interface=some_interface)
## Notes
# Dbus objects also have an introspectable interface with a single Introspect method, use:
proxy_object_info = proxy_object.Introspect(dbus_interface=dbus.INTROSPECTABLE_IFACE)
# Example
def human_readable_byte_list(bytes):
import array
return array.array('B', bytes).tostring()
# OR
return "".join(chr(val) for val in bytes)
import dbus
bus = dbus.SystemBus()
NM_DEVICE_TYPE_WIFI = 2
# Bus name
nm_bus = "org.freedesktop.NetworkManager"
# Interface Names
nm_interface = "org.freedesktop.NetworkManager"
nm_device_interface = "org.freedesktop.NetworkManager.Device"
nm_wireless_device_interface = "org.freedesktop.NetworkManager.Device.Wireless"
nm_access_point_interface = "org.freedesktop.NetworkManager.AccessPoint"
# Get Base Object
network_manager = bus.get_object(nm_bus, "/org/freedesktop/NetworkManager")
# Call GetDevices method to retrieve the current devices
# Returns a list of paths
devices = network_manager.GetDevices(dbus_interface=nm_interface)
# Print the device paths
for device_path in devices:
print device_path
print "Access Points"
# Get all the Access Points from all the wireless devices
for device_path in devices:
# Get Device
device = bus.get_object(nm_bus, device_path)
# Only the Wireless devices
if device.Get(nm_device_interface, 'DeviceType', dbus_interface=dbus.PROPERTIES_IFACE) == NM_DEVICE_TYPE_WIFI:
# Get Access points
access_points = device.GetAllAccessPoints(dbus_interface=nm_wireless_device_interface)
# Print out each one\s SSID
for access_point_path in access_points:
# Get Access Point
access_point = bus.get_object(nm_bus, access_point_path)
# Get it's SSID
ssid_bytes = access_point.Get(nm_access_point_interface, 'Ssid', dbus_interface=dbus.PROPERTIES_IFACE)
# Print SSID
print human_readable_byte_list(ssid_bytes) if len(human_readable_byte_list(ssid_bytes)) > 0 else "**HIDDEN SSID**"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment