2015-09-26 105 views
1

我試圖使用wpa_supplicant python模塊連接到wpa的wifi。在API中,我找到了一個名爲select_network的函數,它似乎應該可以幫助我關聯一個wifi AP,但我不明白輸入參數的含義。如何使用Python模塊wpa_supplicant連接到安全的WiFi?

輸入參數被稱爲到所需網絡的D-bus對象路徑。 谷歌搜索後,我找不到有關使用此功能的任何示例。

更新

我使用d-腳下看到/ FI/W1/wpasupplicant1對象路徑。 像下面這樣:

/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 

更新-1

還是不知道如何處理select_network此功能。 但是我發現下面的鏈接:

http://www.programcreek.com/python/example/10250/dbus.Dictionary

此鏈接介紹如何使用DBUS模塊dircetly連接到無線網絡。

任何提示將不勝感激。

回答

1

另外本作的人努力得到這個工作,你需要通過添加-u啓用的應用程序的wpa_supplicant DBUS守護功能(使DBUS)關於如何使用它

簡單的例子,只是一個粗略的編程例。

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()