Skip to content

Instantly share code, notes, and snippets.

@hfs
Created July 26, 2014 20:28
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save hfs/eb75d55680edd55d320d to your computer and use it in GitHub Desktop.
Save hfs/eb75d55680edd55d320d to your computer and use it in GitHub Desktop.
Query NetworkManager via DBUS in Python: Get the SSID of the active wireless connection
#!/usr/bin/env python
import dbus
NM = 'org.freedesktop.NetworkManager'
NMCA = NM + '.Connection.Active'
NMDW = NM + '.Device.Wireless'
NMAP = NM + '.AccessPoint'
DBUS_PROPS = 'org.freedesktop.DBus.Properties'
def main():
bus = dbus.SystemBus()
nm = bus.get_object(NM, '/org/freedesktop/NetworkManager')
conns = nm.Get(NM, 'ActiveConnections', dbus_interface=DBUS_PROPS)
conn = conns[0]
active_conn = bus.get_object(NM, conn)
devices = active_conn.Get(NMCA, 'Devices', dbus_interface=DBUS_PROPS)
dev_name = devices[0]
dev = bus.get_object(NM, dev_name)
ap_name = dev.Get(NMDW, 'ActiveAccessPoint', dbus_interface=DBUS_PROPS)
ap = bus.get_object(NM, ap_name)
ssid = ap.Get(NMAP, 'Ssid', dbus_interface=DBUS_PROPS, byte_arrays=True)
print(ssid)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment