#!/usr/bin/python3 """ This script queries NetworkManager for its device list, looking for a Wi-Fi device. If found, then some basic properties of the Wi-Fi device are output. """ import dbus def print_ap_ssid(path): device_obj = bus.get_object('org.freedesktop.NetworkManager', path) props_iface = dbus.Interface(device_obj, 'org.freedesktop.DBus.Properties') ssid_bytes = props_iface.Get('org.freedesktop.NetworkManager.AccessPoint', 'Ssid') ssid =''.join([chr(byte) for byte in ssid_bytes]) print(" ActiveAccessPoint: %s" % ssid) def dump_wifi(props_iface): """Dumps the properties of the given Wi-Fi object. """ props = props_iface.GetAll('org.freedesktop.NetworkManager.Device.Wireless') for key in props.keys(): if key in ["HwAddress", "PermHwAddress", "Bitrate", "Mode", "WirelessCapabilities"]: val = props[key] print(" %s = %s" % (key, val)) elif key in ["AccessPoints"]: aps = props[key] num_aps = len(aps) print(" Number of APs: %d" % num_aps) elif key in ["ActiveAccessPoint"]: print_ap_ssid(props[key]) def find_wifi(devices): """Searches for a Wi-Fi device and returns object path. """ for path in devices: device_obj = bus.get_object('org.freedesktop.NetworkManager', path) props_iface = dbus.Interface(device_obj, 'org.freedesktop.DBus.Properties') type = props_iface.Get('org.freedesktop.NetworkManager.Device', 'DeviceType') if type == 2: print ("[ %s - WiFi (%s)" % (path, type)) dump_wifi(props_iface) bus = dbus.SystemBus() try: manager = dbus.Interface(bus.get_object('org.freedesktop.NetworkManager', '/org/freedesktop/NetworkManager'), 'org.freedesktop.NetworkManager') except dbus.exceptions.DBusException as e: print("Service org.freedesktop.NetworkManager not found on DBus: {}".format(e)) exit(1) devices = manager.GetDevices() find_wifi(devices)