2013-02-21 31 views
10

पर पाइथन से संरक्षित वाईफाई से कनेक्ट करना मैं उबंटू लिनक्स के लिए एक सॉफ्टवेयर बना रहा हूं जिसे वाईफाई एपी से कनेक्ट करने की आवश्यकता है। वाईफाई नेटवर्क पूर्वनिर्धारित नहीं है और सॉफ़्टवेयर के एक ही रन के दौरान कई बार बदल सकता है (उपयोगकर्ता वह है जो परिवर्तन का आदेश देता है)। विचार यह है: एसएसआईडी और उनके डब्ल्यूपीए या WEP पासफ्रेज का एक सेट दिया गया है, सॉफ़्टवेयर सिस्टम में कहीं भी कॉन्फ़िगरेशन फ़ाइलों को बदलने की आवश्यकता के बिना, नेटवर्क पर स्विच करने में सक्षम होना चाहिए।लिनक्स

बड़ी समस्या, जैसा कि लगता है, कनेक्शन में पासफ्रेज पास करना है। यहां तक ​​कि मैं अब तक किस प्रकार से परिचालन कर रहा हूं:

  • उबंटू 12.10 मशीन वाईफ़ाई डोंगल से सुसज्जित है।
  • अजगर, जो सॉफ्टवेयर चलाता है, और जो कनेक्शन
  • connman अनुरोध करने के लिए उपयोग किया जाएगा 0.79
  • wpa_supplicant v1.0
  • डी-बस

पहले तो मैंने सोचा कि यह होगा डी-बस के माध्यम से कनमन को पासफ्रेज पास करना संभव है, लेकिन न तो कनान के इस संस्करण, न ही 1.11, इसके लिए किसी भी विधि का खुलासा करते हैं। तब मुझे पता चला कि फ़ाइल को /var/lib/connman/ निर्देशिका में डंप करना संभव है। फ़ाइल की सामग्री को बहुत सरल कर रहे हैं और इस तरह दिखेगा:

[service_SSID] 
Type=wifi 
Name=Network-SSID 
Passphrase=here-goes-the-passphrase 

एक बार इस फाइल को बनाया जाता है, नेटवर्क से कनेक्ट करने के लिए एक सरल net.connman.Service.Connect करने के लिए उपयुक्त सेवा में) विधि कॉल (की आवश्यकता है। समस्या यह है कि जब तक यह पुनरारंभ नहीं होता है तब तक connman कॉन्फ़िगरेशन फ़ाइल को पार्स नहीं करेगा। इसके लिए सूडो विशेषाधिकार, अतिरिक्त समय की आवश्यकता होती है, और सभी चीजों के लिए जोखिम उठता है "अब क्या गलत हो सकता है" चीजें होती हैं। तब मुझे लगा कि पासफ्रेज किसी भी तरह wpa_supplicant डी-बस एपीआई को पारित किया जा सकता है, लेकिन मैं कुछ भी खोजने में विफल रहा।

Google खोजों ने भी मुझे असफल कर दिया है। ऐसा लगता है कि किसी ने कभी पहले ऐसा करने की कोशिश नहीं की।

कमांड sudo iwconfig wlan0 essid <SSID> key s:<PASSPHRASE> परिणाम SET failed on device wlan0 ; Invalid argument. त्रुटि में परिणाम। इसके अलावा, इसे सूडो की आवश्यकता होती है जिसे मैं टालना चाहता हूं।

मैंने यह पता लगाने की कोशिश की कि wpa_gui प्रोग्राम कैसे जादू करता है। सबसे पहले मैंने पाया कि इसे सूडो की भी आवश्यकता है, और यह सीधे /var/run/wpa_supplicant/wlan0 पर आदेशों का एक गुच्छा भेजता है। अगर मैं कुछ भी सरल नहीं समझता तो इस व्यवहार को दोहराना मेरे लिए आखिरी उपाय होगा।

तो, बड़ा सवाल यह है: एक WEP/WPA संरक्षित वाईफाई नेटवर्क से कनेक्ट करने के लिए पायथन का उपयोग कैसे करें?
मैं यह भी सोच रहा हूं कि कॉनमैन का उपयोग करना एक अच्छा तरीका है और यदि मुझे नेटवर्क प्रबंधक पर वापस नहीं जाना चाहिए, जो उबंटू का डिफ़ॉल्ट है।

उत्तर

5

यह दिखाता है कि WPA के लिए यह कैसे करें।

सबसे पहले, कुत्ते को कुचलने और नेटवर्कमैनेजर का उपयोग करें। नीचे दी गई उदाहरण स्क्रिप्ट से पता चलता है कि वायरलेस समर्थन को कैसे सक्षम किया जाए, जांचें कि दिए गए एसएसआईडी के साथ नेटवर्क उपलब्ध है या नहीं, उस एसएसआईडी से एक WPA पासफ्रेज़ का उपयोग करके कनेक्ट करें, और फिर नेटवर्क से डिस्कनेक्ट करें और वायरलेस अक्षम करें। मुझे पूरा यकीन है कि इस स्क्रिप्ट में सुधार किया जा सकता है, लेकिन वर्तमान संस्करण एक उदाहरण के रूप में पर्याप्त कार्य करता है:

#!/usr/bin/python 

# This script shows how to connect to a WPA protected WiFi network 
# by communicating through D-Bus to NetworkManager 0.9. 
# 
# Reference URLs: 
# http://projects.gnome.org/NetworkManager/developers/ 
# http://projects.gnome.org/NetworkManager/developers/settings-spec-08.html 

import dbus 
import time 

SEEKED_SSID = "skynet" 
SEEKED_PASSPHRASE = "qwertyuiop" 

if __name__ == "__main__": 
    bus = dbus.SystemBus() 
    # Obtain handles to manager objects. 
    manager_bus_object = bus.get_object("org.freedesktop.NetworkManager", 
             "/org/freedesktop/NetworkManager") 
    manager = dbus.Interface(manager_bus_object, 
          "org.freedesktop.NetworkManager") 
    manager_props = dbus.Interface(manager_bus_object, 
            "org.freedesktop.DBus.Properties") 

    # Enable Wireless. If Wireless is already enabled, this does nothing. 
    was_wifi_enabled = manager_props.Get("org.freedesktop.NetworkManager", 
             "WirelessEnabled") 
    if not was_wifi_enabled: 
     print "Enabling WiFi and sleeping for 10 seconds ..." 
     manager_props.Set("org.freedesktop.NetworkManager", "WirelessEnabled", 
          True) 
     # Give the WiFi adapter some time to scan for APs. This is absolutely 
     # the wrong way to do it, and the program should listen for 
     # AccessPointAdded() signals, but it will do. 
     time.sleep(10) 

    # Get path to the 'wlan0' device. If you're uncertain whether your WiFi 
    # device is wlan0 or something else, you may utilize manager.GetDevices() 
    # method to obtain a list of all devices, and then iterate over these 
    # devices to check if DeviceType property equals NM_DEVICE_TYPE_WIFI (2). 
    device_path = manager.GetDeviceByIpIface("wlan0") 
    print "wlan0 path: ", device_path 

    # Connect to the device's Wireless interface and obtain list of access 
    # points. 
    device = dbus.Interface(bus.get_object("org.freedesktop.NetworkManager", 
              device_path), 
          "org.freedesktop.NetworkManager.Device.Wireless") 
    accesspoints_paths_list = device.GetAccessPoints() 

    # Identify our access point. We do this by comparing our desired SSID 
    # to the SSID reported by the AP. 
    our_ap_path = None 
    for ap_path in accesspoints_paths_list: 
     ap_props = dbus.Interface(
      bus.get_object("org.freedesktop.NetworkManager", ap_path), 
      "org.freedesktop.DBus.Properties") 
     ap_ssid = ap_props.Get("org.freedesktop.NetworkManager.AccessPoint", 
           "Ssid") 
     # Returned SSID is a list of ASCII values. Let's convert it to a proper 
     # string. 
     str_ap_ssid = "".join(chr(i) for i in ap_ssid) 
     print ap_path, ": SSID =", str_ap_ssid 
     if str_ap_ssid == SEEKED_SSID: 
      our_ap_path = ap_path 
      break 

    if not our_ap_path: 
     print "AP not found :(" 
     exit(2) 
    print "Our AP: ", our_ap_path 

    # At this point we have all the data we need. Let's prepare our connection 
    # parameters so that we can tell the NetworkManager what is the passphrase. 
    connection_params = { 
     "802-11-wireless": { 
      "security": "802-11-wireless-security", 
     }, 
     "802-11-wireless-security": { 
      "key-mgmt": "wpa-psk", 
      "psk": SEEKED_PASSPHRASE 
     }, 
    } 

    # Establish the connection. 
    settings_path, connection_path = manager.AddAndActivateConnection(
     connection_params, device_path, our_ap_path) 
    print "settings_path =", settings_path 
    print "connection_path =", connection_path 

    # Wait until connection is established. This may take a few seconds. 
    NM_ACTIVE_CONNECTION_STATE_ACTIVATED = 2 
    print """Waiting for connection to reach """ \ 
      """NM_ACTIVE_CONNECTION_STATE_ACTIVATED state ...""" 
    connection_props = dbus.Interface(
     bus.get_object("org.freedesktop.NetworkManager", connection_path), 
     "org.freedesktop.DBus.Properties") 
    state = 0 
    while True: 
     # Loop forever until desired state is detected. 
     # 
     # A timeout should be implemented here, otherwise the program will 
     # get stuck if connection fails. 
     # 
     # IF PASSWORD IS BAD, NETWORK MANAGER WILL DISPLAY A QUERY DIALOG! 
     # This is something that should be avoided, but I don't know how, yet. 
     # 
     # Also, if connection is disconnected at this point, the Get() 
     # method will raise an org.freedesktop.DBus.Error.UnknownMethod 
     # exception. This should also be anticipated. 
     state = connection_props.Get(
      "org.freedesktop.NetworkManager.Connection.Active", "State") 
     if state == NM_ACTIVE_CONNECTION_STATE_ACTIVATED: 
      break 
     time.sleep(0.001) 
    print "Connection established!" 

    # 
    # Connection is established. Do whatever is necessary. 
    # ... 
    # 
    print "Sleeping for 5 seconds ..." 
    time.sleep(5) 
    print "Disconnecting ..." 

    # Clean up: disconnect and delete connection settings. If program crashes 
    # before this point is reached then connection settings will be stored 
    # forever. 
    # Some pre-init cleanup feature should be devised to deal with this problem, 
    # but this is an issue for another topic. 
    manager.DeactivateConnection(connection_path) 
    settings = dbus.Interface(
     bus.get_object("org.freedesktop.NetworkManager", settings_path), 
     "org.freedesktop.NetworkManager.Settings.Connection") 
    settings.Delete() 

    # Disable Wireless (optional step) 
    if not was_wifi_enabled: 
     manager_props.Set("org.freedesktop.NetworkManager", "WirelessEnabled", 
          False) 
    print "DONE!" 
    exit(0) 
+0

क्या आप मुझे बता सकते हैं कि WEP के लिए सुरक्षा निर्देश क्या होगा? – Natim

+0

क्षमा करें, कभी भी WEP के साथ ऐसा करने की जांच करने की आवश्यकता नहीं है, इसलिए मैं यहां आपकी सहायता नहीं कर सकता। मुझे कल्पना नहीं है कि यह बहुत अलग होगा, हालांकि। – ZalewaPL

+3

@ZalewaPL मुझे पता है कि यह अंतिम उत्तर के बाद से लगभग एक वर्ष है, लेकिन इस [लिंक] के अनुसार (https://developer.gnome.org/NetworkManager/unstable/spec.html#org.freedesktop.NetworkManager) आप नहीं करते यहां तक ​​कि सुरक्षा के प्रकार (WEP, WPA2, आदि) दर्ज करने की आवश्यकता है, आपको बस पासवर्ड (Psk) प्रदान करने की आवश्यकता है। AddAndActivateConnection के पहले पैरामीटर के लिए उद्धरण: 'कनेक्शन सेटिंग्स और गुण; अगर अपूर्ण गायब सेटिंग्स स्वचालित रूप से दिए गए डिवाइस और विशिष्ट ऑब्जेक्ट का उपयोग करके पूरी हो जाएंगी। इसलिए मैंने ** कनेक्शन_परैम ** से ** सुरक्षा ** और ** कुंजी-एमजीएमटी ** हटा दिया ** और यह सफलतापूर्वक कनेक्ट हो गया। :) – kv1dr

 संबंधित मुद्दे

  • कोई संबंधित समस्या नहीं^_^