Skip to content

Instantly share code, notes, and snippets.

@meeuw
Last active September 13, 2018 20:30
Show Gist options
  • Save meeuw/5765413 to your computer and use it in GitHub Desktop.
Save meeuw/5765413 to your computer and use it in GitHub Desktop.
Python script to create, using dbus, an adhoc wifi connection
import dbus
bus = dbus.SystemBus()
wpas_obj = bus.get_object("fi.w1.wpa_supplicant1",
"/fi/w1/wpa_supplicant1")
wpas = dbus.Interface(wpas_obj, "fi.w1.wpa_supplicant1")
path = wpas.CreateInterface({'Ifname':"wlan0"})
if_obj = bus.get_object("fi.w1.wpa_supplicant1", path)
path = if_obj.AddNetwork({
'ssid':"adhocname",
'mode':1,
'frequency':2412,
'proto':"WPA",
'key_mgmt':"NONE",
'pairwise':"NONE",
'group':'TKIP',
'psk':"verysecret123",
}, dbus_interface="fi.w1.wpa_supplicant1.Interface")
network = bus.get_object("fi.w1.wpa_supplicant1", path)
network.Set("fi.w1.wpa_supplicant1.Network", "Enabled", True, dbus_interface=dbus.PROPERTIES_IFACE)
print(network.GetAll("fi.w1.wpa_supplicant1.Network", dbus_interface=dbus.PROPERTIES_IFACE))
@laneschmidt
Copy link

Little old but I get error: org.freedesktop.DBus.Error.UnknownMethod when trying to call dbus.Interface()

@meeuw
Copy link
Author

meeuw commented Sep 13, 2018

I've made some minor changes; it works for me on Fedora 28. You should run this script as root, edit the interface (wlan0) and if it fails somewhere, restart wpa_supplicant and rerun the script. You might also want to make the interface unmanaged for NetworkManager by adding:

[keyfile]
unmanaged-devices=mac:00:11:22:33:44:55

to /etc/NetworkManager/NetworkManager.conf

@laneschmidt
Copy link

I'm running Ubuntu 16.04. I'll try what you said, thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment