Reputation: 419
I'm trying to use the wpa_supplicant python module to connect to wifi with wpa. In the API, I found a function called select_network which seems like it should help me to associate with a wifi AP, but I don't understand the input parameter's meaning.
The input parameter is called D-bus object path to the desired network. After googling, I cannot find any example about using this function.
Update
I use d-feet to see the object paths under /fi/w1/wpasupplicant1. Like the following:
/fi/w1/wpa_supplicant1
/fi/w1/wpa_supplicant1/interfaces/7
/fi/w1/wpa_supplicant1/interfaces/7/BSSs/14
/fi/w1/wpa_supplicant1/interfaces/7/Networks/0
/fi/w1/wpa_supplicant1/interfaces/7/Networks/1
Update-1
Still don't know how to deal with select_network this function. However I found the following link:
http://www.programcreek.com/python/example/10250/dbus.Dictionary
this link describes how to use dbus module dircetly to connect to the wifi.
Any tips will be appreciate.
Upvotes: 4
Views: 4900
Reputation: 41
Additionally to this for people struggling to get this to work you need to enable DBUS functionality on the wpa_supplicant application daemon by adding -u (enable dbus)
Quick example on how to use it, just a rough programming example.
from wpa_supplicant.core import WpaSupplicantDriver
from twisted.internet.selectreactor import SelectReactor
import threading
import time
import errno
import sys
import types
import netifaces
import dbus
class PythonWifiScanner:
wifiAccessPoints = []
def __init__(self,reactor):
self._reactor = reactor
threading.Thread(target=self._reactor.run, kwargs={'installSignalHandlers': 0}).start()
time.sleep(0.2) # let reactor start
self.driver = WpaSupplicantDriver(reactor)
self.supplicant = self.driver.connect()
# get network interfaces
self.net_iface = netifaces.interfaces()
def get_configured_networks(self,interfaceNumber):
return self.supplicant.get_interface(self.net_iface[interfaceNumber].decode()).get_networks()
def get_single_wpa_interface(self,interfaceNumber):
return self.supplicant.get_interface(self.net_iface[interfaceNumber].decode())
def get_interfaces(self):
return self.net_iface
def select_network(self,network_path,interfaceNumber):
return self.supplicant.get_interface(self.net_iface[interfaceNumber].decode()).select_network(network_path)
def add_network(self,network_cfg,interfaceNumber):
return self.supplicant.get_interface(self.net_iface[interfaceNumber].decode()).add_network(network_cfg)
def scan_interface_for_networks(self,interfaceNumber):
# Get interface and scan the network
interface = self.supplicant.get_interface(self.net_iface[interfaceNumber].decode())
wifiNetworks = interface.scan(block=True)
self.wifiAccessPoints = []
for singleWifi in wifiNetworks:
self.wifiAccessPoints.append(singleWifi.get_ssid())
return wifiNetworks
# Start a simple Twisted SelectReactor
sample_network_cfg = {}
sample_network_cfg['psk'] = "EnterYourKeyHere"
sample_network_cfg['ssid'] = "EnterYourWifiHere"
sample_network_cfg['key_mgmt'] = "WPA-PSK"
reactor = SelectReactor()
dave=PythonWifiScanner(reactor)
value = None
bus = dbus.SystemBus()
print "Interface:" + dave.get_interfaces()[3]
# scan for available networks
for singleWifi in dave.scan_interface_for_networks(3):
print "Wifi SSID:" + singleWifi.get_ssid()
print "Wifi Network Type:" + singleWifi.get_network_type()
# Add network configuration to wpa_supplicant
configpath = dave.add_network(sample_network_cfg,3)
# Attach and Select your network (will need to setip address)
dave.select_network(configpath.get_path(),3)
reactor.stop()
Upvotes: 3