Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
wifi export for network-manager
#!/usr/bin/env python
# Export network-manager WiFi connection details to a wpa-supplicant
# file. This can be saved to an Android device, so you have all your
# laptop's WiFi connections on your phone right away (root required).
# python >foo
# adb push foo /sdcard/foo
# adb shell
# su -c 'cat /sdcard/foo >>/data/misc/wifi/wpa_supplicant.conf'
# rm -f /sdcard/foo
# exit
# 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 3 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
# 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.
# Copyright (C) 2010 Red Hat, Inc.
# Copyright (C) 2015 Tobias Mueller <>
# Copyright (C) 2016 wvengen <>
import dbus, sys
bus = dbus.SystemBus()
def merge_secrets(proxy, config, setting_name):
# returns a dict of dicts mapping name::setting, where setting is a dict
# mapping key::value. Each member of the 'setting' dict is a secret
secrets = proxy.GetSecrets(setting_name)
# Copy the secrets into our connection config
for setting in secrets:
for key in secrets[setting]:
config[setting_name][key] = secrets[setting][key]
except Exception, e:
def list_connections():
# Ask the settings service for the list of connections it provides
service_name = "org.freedesktop.NetworkManager"
proxy = bus.get_object(service_name, "/org/freedesktop/NetworkManager/Settings")
settings = dbus.Interface(proxy, "org.freedesktop.NetworkManager.Settings")
connection_paths = settings.ListConnections()
ret = []
# List each connection's name, UUID, and type
for path in connection_paths:
con_proxy = bus.get_object(service_name, path)
settings_connection = dbus.Interface(con_proxy, "org.freedesktop.NetworkManager.Settings.Connection")
config = settings_connection.GetSettings()
connection = config.get('connection', {})
if connection['type'] == '802-11-wireless':
wireless = config.get('802-11-wireless', {})
merge_secrets(settings_connection, config, '802-11-wireless')
merge_secrets(settings_connection, config, '802-11-wireless-security')
lines = []
lines.append('ssid="' + bytearray(wireless['ssid']).decode('utf-8') + '"')
security = config.get('802-11-wireless-security', None)
if security is None:
continue # we don't need to export open access points
lines.append('key_mgmt=OPEN') # otherwise this would work
elif security.get('key-mgmt') == 'wpa-psk':
psk = security.get('psk', None)
if psk is None: continue # security required but not present: skip
lines.append('psk="' + security.get('psk') + '"')
# @todo support other security mechanisms
print "network={\n" + u"\n".join(map(lambda s: ' '+s, lines)).encode('utf-8') + "\n}\n"
if __name__ == '__main__':
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.