Skip to content

Instantly share code, notes, and snippets.

Embed
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 gen_wpa_sup.py >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
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# 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 <muelli@cryptobitch.de>
# Copyright (C) 2016 wvengen <dev-android@willem.engen.nl>
#
import dbus, sys
bus = dbus.SystemBus()
def merge_secrets(proxy, config, setting_name):
try:
# 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:
pass
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 = []
# https://mail.python.org/pipermail/tutor/2013-August/097352.html
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('key_mgmt=WPA-PSK')
lines.append('psk="' + security.get('psk') + '"')
else:
# @todo support other security mechanisms
continue
print "network={\n" + u"\n".join(map(lambda s: ' '+s, lines)).encode('utf-8') + "\n}\n"
if __name__ == '__main__':
list_connections()
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.